#include<bits/stdc++.h>
using namespace std;
int a,b,k;
char c,d;
bool af;
int  main()
{
    cin>>a>>c;
    if(c=='A')af=1;
    cin>>b>>d;
    cin>>k;
    if(af==1)cout<<min(a,k);
    else if(k<=a)cout<<'0';
    else cout<<k-a;
    cout<<endl;

    return 0;
}
