#include <bits/stdc++.h>
int main() {
  char *s = "6789TJQKA1", c, a, x, b, y;
  scanf("%c\n%c%c %c%c", &c, &a, &x, &b, &y);
  puts(x == c && y != c || x == y && strchr(s, a) > strchr(s, b) ? "YES"
                                                                 : "NO");
}
