#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    char a,b;
    int A,B;
    cin>>A>>a>>B>>b;
    int k;
    cin>>k;
    if(a=='A')
    {
        cout<<min(A,k);
    }
    else
    {
        cout <<min(B,k-min(A,k));
    }
    return 0;
}
