#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  char t, a1, b1, a2, b2;
  cin >> t >> a1 >> b1 >> a2 >> b2;
  if (b1 == t && b2 != t)
    cout << "YES";
  else if (b1 == b2) {
    char a[10] = "6789TJQKA";
    int i1, i2;
    for (int i = 0; i < 9; i++) {
      if (a[i] == a1)
        i1 = i;
      else if (a[i] == a2)
        i2 = i;
    }
    if (i1 > i2)
      cout << "YES";
    else
      cout << "NO";
  } else
    cout << "NO";
}
