#include <bits/stdc++.h>
using namespace std;
template <class To, class From>
To castToFrom(From arg) {
  To ret;
  std::stringstream ss("");
  ss << arg;
  ss >> ret;
  return ret;
}
vector<string> split(string& str) {
  vector<string> ret;
  string::iterator from(str.begin());
  int i = 0;
  while (true) {
    string::iterator to = find(from, str.end(), '.');
    if (to == str.end()) {
      string temp(from, to);
      ret.push_back(temp);
      break;
    } else {
      string temp(from, to);
      ret.push_back(temp);
      from = ++to;
    }
    i++;
  }
  return ret;
}
bool check(int year, int month, int day) {
  int table0[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  int table1[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  if (month > 12) return false;
  if ((2000 + year) % 4 == 0) {
    if (day > table1[month - 1]) return false;
  } else {
    if (day > table0[month - 1]) return false;
  }
  return true;
}
int main() {
  vector<string> com, bob;
  string a, b;
  bool flag;
  int dc, mc, yc, db, mb, yb, d, m, y;
  cin >> a;
  cin >> b;
  com = split(a);
  bob = split(b);
  flag = false;
  dc = castToFrom<int, string>(com[0]);
  mc = castToFrom<int, string>(com[1]);
  yc = castToFrom<int, string>(com[2]);
  db = castToFrom<int, string>(bob[0]);
  mb = castToFrom<int, string>(bob[1]);
  yb = castToFrom<int, string>(bob[2]);
  d = db;
  m = mb;
  y = yb;
  if (check(y, m, d)) {
    if ((yc - y > 18) || ((yc - y == 18) && (mc > m)) ||
        ((yc - y == 18) && (mc == m) && (dc >= d))) {
      flag = true;
    }
  }
  d = db;
  m = yb;
  y = mb;
  if (check(y, m, d)) {
    if ((yc - y > 18) || ((yc - y == 18) && (mc > m)) ||
        ((yc - y == 18) && (mc == m) && (dc >= d))) {
      flag = true;
    }
  }
  d = mb;
  m = db;
  y = yb;
  if (check(y, m, d)) {
    if ((yc - y > 18) || ((yc - y == 18) && (mc > m)) ||
        ((yc - y == 18) && (mc == m) && (dc >= d))) {
      flag = true;
    }
  }
  d = mb;
  m = yb;
  y = db;
  if (check(y, m, d)) {
    if ((yc - y > 18) || ((yc - y == 18) && (mc > m)) ||
        ((yc - y == 18) && (mc == m) && (dc >= d))) {
      flag = true;
    }
  }
  d = yb;
  m = db;
  y = mb;
  if (check(y, m, d)) {
    if ((yc - y > 18) || ((yc - y == 18) && (mc > m)) ||
        ((yc - y == 18) && (mc == m) && (dc >= d))) {
      flag = true;
    }
  }
  d = yb;
  m = mb;
  y = db;
  if (check(y, m, d)) {
    if ((yc - y > 18) || ((yc - y == 18) && (mc > m)) ||
        ((yc - y == 18) && (mc == m) && (dc >= d))) {
      flag = true;
    }
  }
  if (flag) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}
