#include<iostream>
using namespace std;
int main()
{
    long long n1, n2, k;
    char gorivo1, gorivo2;
    cin>>n1>>gorivo1>>n2>>gorivo2>>k;
    if(gorivo1 == 'A' && k > n1)
    {
        cout<<n1;
    }
    else if(gorivo1 == 'A' && k < n1)
    {
        cout<<k;
    }
    else if(gorivo1 == 'B')
    {
        if(k - n1 >= 0 && k - n1 < n2)
        {
            cout<<k - n1;
        }
        else if(k - n1 >= 0 && k - n1 > n2)
        {
            cout<<n2;
        }
        else
        {
            cout<<"0";
        }
    }
    return 0;
}
