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