#include <bits/stdc++.h>
using namespace std;
int const mod = 1000000007;
int main() {
  string r = "6789TJQKA";
  char t, x, y, a, b;
  cin >> t >> y >> x >> b >> a;
  if ((x == a && r.find(y) > r.find(b)) || (x == t && a != t))
    cout << "YES";
  else
    cout << "NO";
  return 0;
}
