#include <bits/stdc++.h>
using namespace std;
int date[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int dd, mm, yy;
int bd[6], bm[6], by[6];
char s[20];
void solve() {
  for (int i = 0; i < 6; i++) {
    if (bm[i] > 12) continue;
    if (bd[i] > date[bm[i]] && (!(bd[i] == 29 && bm[i] == 2 && by[i] % 4 == 0)))
      continue;
    if (yy - by[i] > 18) {
      printf("YES\n");
      return;
    }
    if (yy - by[i] < 18) continue;
    if (mm > bm[i]) {
      printf("YES\n");
      return;
    }
    if (mm < bm[i]) continue;
    if (dd >= bd[i]) {
      printf("YES\n");
      return;
    }
  }
  printf("NO\n");
}
int main() {
  while (scanf("%s", s) != EOF) {
    dd = s[1] - '0' + 10 * (s[0] - '0');
    mm = s[4] - '0' + 10 * (s[3] - '0');
    yy = s[7] - '0' + 10 * (s[6] - '0');
    scanf("%s", s);
    bd[0] = s[1] - '0' + 10 * (s[0] - '0');
    bm[0] = s[4] - '0' + 10 * (s[3] - '0');
    by[0] = s[7] - '0' + 10 * (s[6] - '0');
    by[5] = bm[4] = by[3] = bm[2] = bd[1] = bd[0];
    bm[5] = by[4] = bd[3] = bd[2] = by[1] = bm[0];
    bd[5] = bd[4] = bm[3] = by[2] = bm[1] = by[0];
    solve();
  }
  return 0;
}
