#include <iostream>
using namespace std;

int main()
{
    int k, n1, n2, i;
    char c1, c2;
    cin >> n1 >> c1 >> n2 >> c2 >> k;

    if (c1=='A')
    {
        if (k<=n1)
        {
            i=k;
        }
        else
        {
            i=n1;
        }
    }

    if (c1=='B')
    {
        if (k<=n1)
        {
            i=0;
        }
        else
        {
            i=k-n1;
        }
    }

    cout << i << endl;
}
