#include<bits/stdc++.h>
using namespace std;
int main()
{
    int bareli_ot_lavo,bareli_ot_dasno,k;
    char vid_lavo,vid_dasno;
    cin>>bareli_ot_lavo>>vid_lavo>>bareli_ot_dasno>>vid_dasno>>k;
    if(bareli_ot_lavo>k && vid_lavo=='A')
    {
        cout<<k;
    }
    if(bareli_ot_lavo>k && vid_lavo=='B')
    {
        cout<<0;
    }


    if(bareli_ot_lavo<k && vid_lavo=='A')
    {
        cout<<bareli_ot_lavo;
    }
    if(bareli_ot_lavo<k && vid_lavo=='B')
    {
        cout<<k-bareli_ot_lavo;
    }

    return 0;
}
