#include<iostream>
using namespace std;
int main(){
long long n1, n2, k, oldbenzine;
char c1, c2;
cin >> n1 >> c1 >> n2 >> c2 >> k;
if(c1=='A'){
    oldbenzine=n1;
}
else{
    oldbenzine=n2;
}
while(n1>0&&k>0){
    n1=n1-1;
    k=k-1;
}
while(n2>0&&k>0){
    n2=n2-1;
    k=k-1;
}
if(c1=='A'){
    cout << oldbenzine-n1;
}
else{
    cout << oldbenzine-n2;
}

return 0;}
