#include <iostream>
using namespace std;

int main ()
{
    int k = 0;
    int n1;
    char c1;
    int n2;
    char c2;

    cin >> n1 >> c1;
    cout << endl;
    cin >> n2 >> c2;
    cout << endl;
    cin >> k;

    if (k > n1 + n2)
    {
        return 0;
    }
    
    if (k >= n1 && c1 == 'A')
    {
        cout << n1;
    }

    if (k > n1 && c1 == 'B')
    {
    cout << k - n1;
    }

    if (k == n1 && c1 == 'B')
    {
        cout << 0;
    }

    if (k < n1 && c1 == 'A')
    {
    cout << k;
    }
}
