#include <bits/stdc++.h>
using namespace std;
int mon[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int dd, mm, yy, bd, bm, by;
int check(int d, int m, int y) {
  if (m > 12) return 0;
  if (d > mon[m - 1]) return 0;
  if (m == 2 && y % 4 != 0 && d == 29) return 0;
  if (yy - y > 18) {
    cout << "YES";
    return 1;
  }
  if (yy - y == 18) {
    if (mm > m) {
      cout << "YES";
      return 1;
    }
    if (mm == m && dd >= d) {
      cout << "YES";
      return 1;
    }
  }
  return 0;
}
int main() {
  string b, c;
  cin >> c >> b;
  for (int i = 0; i < 2; i++) {
    bd *= 10;
    bd += b[i] - '0';
    dd *= 10;
    dd += c[i] - '0';
  }
  for (int i = 3; i < 5; i++) {
    bm *= 10;
    bm += b[i] - '0';
    mm *= 10;
    mm += c[i] - '0';
  }
  for (int i = 6; i < 8; i++) {
    by *= 10;
    by += b[i] - '0';
    yy *= 10;
    yy += c[i] - '0';
  }
  if (check(bd, bm, by)) return 0;
  if (check(bd, by, bm)) return 0;
  if (check(bm, bd, by)) return 0;
  if (check(bm, by, bd)) return 0;
  if (check(by, bd, bm)) return 0;
  if (check(by, bm, bd)) return 0;
  cout << "NO";
  return 0;
}
