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