#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n1, n2, k;
    char c1, c2;
    cin>>n1>>c1>>n2>>c2>>k;
    if(c1=='A')
    {
        if(n1>k)
        {
            cout<<k;
        }
        else
        {
            cout<<n1;
        }
    }
    else
    {
        if(n1>=k)
        {
            cout<<0;
        }
        else if(n2>k-n1)
        {
            cout<<k-n1;
        }
        else
        {
            cout<<n2;
        }
    }
    cout<<endl;
    return 0;
}