#include<iostream>
using namespace std;
int main()
{
    short n1,n2,k,b;
    char c1,c2;
    cin>>n1>>c1>>n2>>c2>>k;

    if(c1=='A')
    {
        if(k>n1)
        {
            cout<<n1;
        }
        else if(k<=n1)
        {
            cout<<k;
        }
    }

    if(c1=='B')
    {
        if(n1>=k)
        {
            cout<<0;
        }
        else if(k>n1)
        {
            b=k-n1;
            if(b>n2)
            {
                cout<<n2;
                return 0;
            }
            cout<<k-n1;
        }

   }
    return 0;
}
