#include<iostream>
using namespace std;

int main(  ){
    long long a,b,k;
    string c,d;
    cin >> a >> c >> b >> d >> k;
    if(c == "A" && a <= k)
    {
        cout << a;
    }
    else
    {
        if(c == "A" && a > k)
            cout << k;
        else{
            if(c == "B" && k > a)
            cout << k - a;
            else
                cout << 0;
        }
    }
    return 0;
}
