#include<iostream>
using namespace std;
int main()
{
    int n1, n2, k;
    string c1, c2;
    cin>>n1>>c1;
    cin>>n2>>c2;
    cin>>k;
    if(n1 <= k && n2 <= k)
    {
        if(c1 == "A")
        {
            cout<<n1;
        }
        if(c1 == "B")
        {
            cout<<k-n1;
        }
    }
    if(n1 > k && n2 > k)
    {
        if(c1 == "A")
        {
            cout<<k;
        }
        if(c1 == "B")
        {
            cout<<"0";
        }
    }
    return 0;
}
