#include<bits/stdc++.h>
using namespace std;
int main(){
cin.tie(NULL);
ios_base::sync_with_stdio(false);
long long n1,n2,k;
char c1,c2;
cin>>n1>>c1>>n2>>c2>>k;
if(c1=='A'&&k>=n1){
cout<<n1<<endl;
}else{
if(c1=='A'&&k<n1){
cout<<k<<endl;
}else{
if(c1=='B'&&k>=n1){
cout<<k-n1<<endl;
}else{
if(c1=='B'&&k<n1){
cout<<"0"<<endl;
}
}
}
}
return 0;
}