#include <iostream>
using namespace std;
int main ()
{
  int n1, n2, k;
  string c1, c2;
  cin>>n1>>c1>>n2>>c2>>k;
  if(k>=n1 && c1 == "A")
  {
      cout<<n1<<endl;
  }
  if(k<n1 && c1 == "B")
  {
      cout<<0<<endl;
  }
  if(k>n1 && c1 == "B")
  {
      cout<<k - n1<<endl;
  }
  if(k<n1 && c1 == "A")
  {
      cout<<k<<endl;
  }
  return 0;

}
