#include <bits/stdc++.h>
using namespace std;
int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool comp(int d, int m, int y, int d1, int m1, int y1) {
  if (y1 % 4 == 0)
    months[1] = 29;
  else
    months[1] = 28;
  if (!(d1 <= months[m1 - 1] && m1 <= 12 && y1 <= 99)) return false;
  if (y1 + 18 < y) {
    return true;
  } else if (y1 + 18 == y) {
    if (m1 < m) {
      return true;
    } else if (m1 == m) {
      if (d1 <= d) {
        return true;
      }
    }
  }
  return false;
}
int main() {
  int d[3], bd[3];
  char s;
  cin >> d[0];
  cin.get(s);
  cin >> d[1];
  cin.get(s);
  cin >> d[2];
  cin >> bd[0];
  cin.get(s);
  cin >> bd[1];
  cin.get(s);
  cin >> bd[2];
  int b0 = 0, b1 = 0, b2 = 0;
  for (int i = 0; i < 26; i++) {
    if (b0 == 3) {
      b1++;
      b0 = 0;
    }
    if (b1 == 3) {
      b2++;
      b1 = 0;
    }
    if (b1 == b2 || b2 == b0 || b0 == b1) {
      b0++;
      continue;
    }
    if (comp(d[0], d[1], d[2], bd[(b2) % 3], bd[(b1) % 3], bd[(b0++) % 3])) {
      cout << "YES";
      return 0;
    }
  }
  cout << "NO";
}
