#include <bits/stdc++.h>
using namespace std;
char c;
string a, b;
int main() {
  cin >> c;
  cin >> a;
  cin >> b;
  if (b[0] == 'A') {
    if (a[1] == c && b[1] != c)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else if (b[0] == 'K') {
    if ((a[0] == 'A' && a[1] == b[1]) || a[1] == c && b[1] != c)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else if (b[0] == 'Q') {
    if ((a[0] == 'A' || a[0] == 'K') && a[1] == b[1] || a[1] == c && b[1] != c)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else if (b[0] == 'J') {
    if ((a[0] == 'A' || a[0] == 'K' || a[0] == 'Q') && a[1] == b[1] ||
        a[1] == c && b[1] != c)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else if (b[0] == 'T') {
    if ((a[0] == 'A' || a[0] == 'K' || a[0] == 'Q' || a[0] == 'J') &&
            a[1] == b[1] ||
        a[1] == c && b[1] != c)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else if (b[0] == '9') {
    if ((a[0] == 'A' || a[0] == 'K' || a[0] == 'Q' || a[0] == 'J' ||
         a[0] == 'T') &&
            a[1] == b[1] ||
        a[1] == c && b[1] != c)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else if (b[0] == '8') {
    if (a[0] != '6' && a[0] != '7' && a[0] != '8' && a[1] == b[1] ||
        a[1] == c && b[1] != c)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else if (b[0] == '7') {
    if ((a[0] != '6' && a[0] != '7') && a[1] == b[1] || a[1] == c && b[1] != c)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else {
    if (a[0] != '6' && a[1] == b[1] || a[1] == c && b[1] != c)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}
