#include <bits/stdc++.h>
using namespace std;
map<char, int> ooxx;
int main() {
  ooxx['6'] = 6;
  ooxx['7'] = 7;
  ooxx['8'] = 8;
  ooxx['9'] = 9;
  ooxx['T'] = 10;
  ooxx['J'] = 11;
  ooxx['Q'] = 12;
  ooxx['K'] = 13;
  ooxx['A'] = 14;
  string a, b, c;
  cin >> a >> b >> c;
  if (b[1] == c[1]) {
    if (ooxx[b[0]] > ooxx[c[0]])
      printf("YES\n");
    else
      printf("NO\n");
  } else {
    if (b[1] == a[0])
      printf("YES\n");
    else
      printf("NO\n");
  }
  return 0;
}
