#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<sstream>
#include<vector>
#include<algorithm>
#include <fstream>

#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define SIZE(s) ((int)(s.size()))
#define SIZEP(x) (int)(sizeof(x)/sizeof(typeof(*(x))))

// AND a b   ~~~  a &= b
// OR a b   ~~~  a |= b
// NOT a     ~~~  a ~= a
// XOR a b   ~~~  a ^= b
// SHL a     ~~~  a << 1
// SHR a     ~~~  a >> 1
// MOV a b   ~~~  a := b

/// {{{ operations
#define AND 0
#define OR 1
#define XOR 2
#define NOT 3
#define SHL 4
#define SHR 5
#define MOV 6
// }}}

#define TESTING 1
#define EASY 0
#define HARD 1
#define NO_REG 47 // indikuje, ze to nie je index do registra

// {{{ errors
#define EXIT_WRONG_CHARACTER 1   // zly znak na vstupe
#define EXIT_INSUFFICIENT_INPUT 2   // vstup nedocitany
#define EXIT_INTERNAL 3   // nasa chyba
#define EXIT_TO_LONG 4    // v easy casti je program prilis dlhy
#define EXIT_WA 5         // nespravna odpoved
#define EXIT_OK 0         // spravna odpoved
// }}}

using namespace std;

string pwd_easy_in = "./easy.in";
string pwd_hard_in = "./hard.in";

int LEVEL;
unsigned long long reg[26];  
string oper[7] = {"and","or","xor","not","shl","shr","mov"};
vector<vector<unsigned long long> > COMND; // postupnost ich operacii: operator, to, from, konstanta

void fail(int error_code,string s) {
  if (TESTING) cerr << s << endl;
  exit(error_code);
}

// {{{ parsing input
void print_registers() {
  for(int i=0;i<26;++i) cout << reg[i] << " "; cout << endl;
}

bool is_register(string s) {
  REP(i,26) if (s == string(1,i+'A')) return true;
  return false;
}

int get_register(string s) {
  REP(i,26) if (s == string(1,i+'A')) return i;
  return -1;
}

bool is_operator(string s) {
  REP(i,SIZEP(oper)) if (s == oper[i]) return true;
  return false;
}

bool get_constant(string s, unsigned long long &K) {
  K=0;
  REP(i,SIZE(s)) {
    if (!isdigit(s[i])) return false;
    K*=10;
    K+=s[i]-'0';
  }
  return true;
}

//}}}

// {{{ reading input
int read_line(unsigned long long &op, unsigned long long &to, unsigned long long &from, unsigned long long &konst) {   // 0 je dobre nacitane, 1 je koniec citania, 2 chyba, 3 koniec riadku
  string s,line;
  vector<string> vstup;

  if (!getline(cin,line)) return 1;   // koniec vstupu
  if (line=="") return 3;    // prazdny riadok
  op = from = to = NO_REG;

  stringstream SS(line);
  while(SS >> s) {
    vstup.push_back(s);
    if (SIZE(vstup) >= 3) break;
  }

  if (SIZE(vstup) == 0) return 3;  // riadok plny whitespacov

  // kontrolujem prvy string, ma byt operacia
  if (!is_operator(vstup[0])) {
    cout << "Syntax error (instruction expected, but '" << vstup[0] << "' found)" << endl;
    fail(EXIT_WRONG_CHARACTER,"prvy string ma byt operacia ale je \""+vstup[0]+"\"\n");
  }
  else REP(j,SIZEP(oper)) if (oper[j] == vstup[0]) { op=j; }

  // kontrolujem velkost vstupu
  if (SIZE(vstup)==1 || (SIZE(vstup)==2 && op!=NOT)) {
    cout << "Syntax error (unexpected end of line)" << endl;
    fail(EXIT_INSUFFICIENT_INPUT,"Nedocitali sme korektny riadok\n");
  }

  // kontrolujem druhy string (vzdy register)
  if (!is_register(vstup[1])) {
    cout << "Syntax error (register name expected, but '" << vstup[1] << "' found)" << endl;
    fail(EXIT_WRONG_CHARACTER,"druhy string ma byt register ale je \""+vstup[1]+"\"\n");
  }
  else {
    to = get_register(vstup[1]);
  }

  if (op == NOT) return 0;  // ak sme v tomto mieste, tak sme dobre nacitali aj operaciu aj register

  // kontrolujem treti string (register alebo konstanta)

  if (is_register(vstup[2])) {
    from = get_register(vstup[2]);
  }
  else if (!get_constant(vstup[2],konst)) {
    cout << "Syntax error (register name or number expected, but '" << vstup[2] << "' found)" << endl;
    fail(EXIT_WRONG_CHARACTER,"treti string nie je ani register a ani konstanta, ale je \""+vstup[2]+"\"\n");
  }

  return 0;
}

void read_input() {
  unsigned long long operand,from,to,konst;
  COMND.resize(0);
  while(1) {
    int result = read_line(operand, to, from, konst); // 0 ok nacitane, 1 koniec vstupu, 3 prazdny riadok
    if (result == 1) break;
    if (result == 0) {
      vector<unsigned long long> v(4);
      v[0] = operand;
      v[1] = to;
      v[2] = from;
      v[3] = konst;
      COMND.push_back(v);
    }
    if (result == 3) continue;
  }

  if (LEVEL == EASY && SIZE(COMND) >= 64) {
    cout << "The code is too long" << endl;
    fail(EXIT_TO_LONG,"Program je prilis dlhy\n");
  }
  if (LEVEL == HARD && SIZE(COMND) >= 300) {
    cout << "The code is too long" << endl;
    fail(EXIT_TO_LONG,"Program je prilis dlhy\n");
  }
}

// }}}

unsigned long long interpret(unsigned long long W) {
  memset(reg,0,sizeof(reg));
  reg[0] = W;
  REP(i,SIZE(COMND)) {
    unsigned long long operand = COMND[i][0];
    unsigned long long to = COMND[i][1];
    unsigned long long from = COMND[i][2];
    unsigned long long konst = COMND[i][3];
    unsigned long long value;
    if (from != NO_REG) value = reg[from];
    else value = konst;
    switch(operand) {
      case AND: reg[to]&= value; break;
      case OR:  reg[to]|= value; break;
      case XOR: reg[to]^= value; break;
      case NOT: reg[to] = ~reg[to]; break;
      case SHL: if (value >= 64) reg[to] = 0;
                else reg[to] = reg[to] << value; break;
      case SHR: if (value >= 64) reg[to] = 0;
                else reg[to] = reg[to] >> value; break;
      case MOV: reg[to] = value; break;
    }
  }
  return reg[0];
}

unsigned long long next_perm(unsigned long long W) {
  vector<int> v;
  REP(i,64) if (W&(1ULL<<i)) v.push_back(1); else v.push_back(0);
  //Tom: nechceme to takto: ??
  reverse(v.begin(),v.end()); //added by Tom, nech su najvyznamnejsie bity vlavo (mensie indexy)
  next_permutation(v.begin(),v.end());
  reverse(v.begin(),v.end()); //added by Tom
  unsigned long long x=0;
  REP(i,64) if (v[i]==1) x|=(1ULL<<i);
  return x;
}

// {{{ easy cast
int hra_easy() {
  int ret=EXIT_OK;
  unsigned long long W;
  ifstream F;
  F.open(pwd_easy_in.c_str(),ios_base::in);
  if (!F.is_open()) fail(EXIT_INTERNAL,"Nepodarilo sa otvorit vstupny subor "+pwd_easy_in+"\n");
  while(F >> W) {
    if (interpret(W) != (W+8ULL)) {
      ret = EXIT_WA;
      break;
    }
  }
  if (ret == EXIT_WA) cout << "Wrong answer" << endl;
  else cout << "OK" << endl;
  
  F.close();
  if (F.is_open()) fail(EXIT_INTERNAL,"Nepodarilo sa uzavriet subor "+pwd_easy_in+"\n");

  return ret;
}
// }}}

// {{{ hard cast
int hra_hard() {
  int ret=EXIT_OK;
  unsigned long long W;
  ifstream F;
  F.open(pwd_hard_in.c_str(),ios_base::in);
  if (!F.is_open()) fail(EXIT_INTERNAL,"Nepodarilo sa otvorit vstupny subor "+pwd_hard_in+"\n");
  while(F >> W) {
    if (interpret(W) != (next_perm(W))) {
      ret = EXIT_WA;
      break;
    }
  }
  if (ret == EXIT_WA) cout << "Wrong answer" << endl;
  else cout << "OK" << endl;

  F.close();
  if (F.is_open()) fail(EXIT_INTERNAL,"Nepodarilo sa uzavriet subor "+pwd_hard_in+"\n");

  return ret;
}
// }}}

int main (int argc, char** argv) {
  if (argc != 2) fail(EXIT_INTERNAL,"Zly pocet parametrov. Chcem lahky/tazky vstup = 0/1\n");
  LEVEL = atoi(argv[1]);
  if (LEVEL != EASY && LEVEL != HARD) fail(EXIT_INTERNAL,"Zly parameter. Chcem pocut lahky = 0 /tazky = 1 vstup.\n");

  read_input();

  if (LEVEL == EASY) return hra_easy();
  else return hra_hard();
  return 0;
}
  

