#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, t;
char a, b, c, d, e;
string s, str;
bool can(int a, int b) {
  while (a && b) {
    int da, db;
    da = a % 10;
    db = b % 10;
    if (a != 0 && b != 0) return false;
    a /= 10;
    b /= 10;
  }
  return true;
}
void dF(char pp, char pepa) {
  long long n1, n2;
  if (pp == 'A')
    n1 = 20;
  else if (pp == 'K')
    n1 = 15;
  else if (pp == 'Q')
    n1 = 14;
  else if (pp == 'J')
    n1 = 13;
  else if (pp == 'T')
    n1 = 12;
  else
    n1 = pp - '0';
  if (pepa == 'A')
    n2 = 20;
  else if (pepa == 'K')
    n2 = 15;
  else if (pepa == 'Q')
    n2 = 14;
  else if (pepa == 'J')
    n2 = 13;
  else if (pepa == 'T')
    n2 = 12;
  else
    n2 = pepa - '0';
  if (n1 > n2)
    cout << "YES";
  else
    cout << "NO";
}
int main() {
  cin >> e;
  cin >> a >> b >> c >> d;
  if (b == e) {
    if (d == e) {
      dF(a, c);
    } else {
      cout << "YES";
      return 0;
    }
  } else if (b == d) {
    dF(a, c);
  } else {
    cout << "NO";
  }
}
