/* Peter Atechian
   Mass Academy
   Senior 3 Division

   ACSL 3
 */

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <locale>
using namespace std;

template <class T>
inline string tostring (const T& t) {
      stringstream ss;
      ss << t;
      return ss.str();
}

template <class T>
inline double toreal (const T& t) {
      stringstream ss;
      ss << t;
      double d;
      ss >> d;
      return d;
}

template <class T>
inline int toint (const T& t) {
      stringstream ss;
      ss << t;
      int d;
      ss >> d;
      return d;
}

inline string cleanstring(const string str) {
      string n = "";
      for(int i=0;i<str.size();i++){
            if(isalnum(str[i])){
                  n.push_back(str[i]);
            }
      }
      return n;
}

int main(){
      bool player = true;//True for A, false for B
      int field[14];//0 is A, 13 is B
      field[0] = 0;
      field[13] = 0;
      for(int i=1; i<13;i++){
            field[i] = 4;
      }
      for(int count=0;count<5;count++){
            string str1, str2;
            cin >> str1 >> str2;
            str1 = cleanstring(str1);
            str2 = cleanstring(str2);
            int c = toint(str1);
            bool done = false;
            while(!done){
                  int s = field[c];
                  field[c] = 0;
                  while(s>0){
                        if(c==12){
                              if(player){
                                    c = 1;
                              } else {
                                    c = 13;
                              }
                        } else if(c==13){
                              c = 1;
                        } else if(c==6){
                              if(player){
                                    c = 0;
                              } else {
                                    c = 7;
                              }
                        } else if(c==0){
                              c = 7;
                        } else {
                              c++;
                        }
                        field[c]++;
                        s--;
                  }
                  if(c==0 || c==13 || field[c]==1 || field[c]==4){
                        done = true;
                  }
            }
            if(str2=="A"){
                  cout << field[0] << endl;
            } else if(str2=="B"){
                  cout << field[13] << endl;
            } else {
                  cout << field[toint(str2)] << endl;
            }
            player = !player;
      }
      while(true){
            cin.get();
      }
      return 0;
}