#include <bits/stdc++.h>
using namespace std;

int main(){
    int n1, n2, k;
    string c1, c2;
    cin >> n1 >> c1 >> n2 >> c2 >> k;
    if (c1 == "A" and k <= n1){
        cout << k;
    }
    else {
        if (c1 == "A" and k > n1){
            cout << n1;
        }
        else{
            if (c2 == "A" and k > n1){
                cout << k - n1;
            }
            else{
                cout << k - k;
            }
        }
    }

    return 0;
}
