#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline int sz(const T& x) {
  return (int)x.size();
}
int dd, mm, yy;
int days[13] = {-1, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool yes(int a[]) {
  int bd = a[0], bm = a[1], by = a[2];
  if (bm < 1 || bm > 12) return false;
  if (bd < 1 || bd > days[bm]) return false;
  if (bm == 2 && bd == 29 && (by % 4)) return false;
  if (yy - by > 18) return true;
  if (yy - by == 18 && make_pair(mm, dd) >= make_pair(bm, bd)) return true;
  return false;
}
int main() {
  cin.sync_with_stdio(false);
  cout.sync_with_stdio(false);
  string s;
  cin >> s;
  dd = (s[0] - '0') * 10 + (s[1] - '0');
  mm = (s[3] - '0') * 10 + (s[4] - '0');
  yy = (s[6] - '0') * 10 + (s[7] - '0');
  int a[3];
  cin >> s;
  a[0] = (s[0] - '0') * 10 + (s[1] - '0');
  a[1] = (s[3] - '0') * 10 + (s[4] - '0');
  a[2] = (s[6] - '0') * 10 + (s[7] - '0');
  sort(a, a + 3);
  do {
    if (yes(a)) {
      cout << "YES\n";
      return 0;
    }
  } while (next_permutation(a, a + 3));
  cout << "NO\n";
  return 0;
}
