#include <bits/stdc++.h>
using namespace std;
long long n1, n2, n3, day, month, year, m[105];
bool ok;
string s;
bool check(long long da, long long mont, long long yea) {
  if (yea % 4 == 0)
    m[2] = 29;
  else
    m[2] = 28;
  if (mont < 13 && da <= m[mont] &&
      (year - yea > 18 || year - yea == 18 && mont < month ||
       year - yea == 18 && mont == month && da <= day))
    return 1;
  else
    return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  m[1] = 31;
  m[3] = 31;
  m[4] = 30;
  m[5] = 31;
  m[6] = 30;
  m[7] = 31;
  m[8] = 31;
  m[9] = 30;
  m[10] = 31;
  m[11] = 30;
  m[12] = 31;
  cin >> s;
  day = (s[0] - '0') * 10 + s[1] - '0';
  month = (s[3] - '0') * 10 + s[4] - '0';
  year = (s[6] - '0') * 10 + s[7] - '0';
  cin >> s;
  n1 = (s[0] - '0') * 10 + s[1] - '0';
  n2 = (s[3] - '0') * 10 + s[4] - '0';
  n3 = (s[6] - '0') * 10 + s[7] - '0';
  if (check(n1, n2, n3))
    cout << "YES";
  else if (check(n1, n3, n2))
    cout << "YES";
  else if (check(n2, n3, n1))
    cout << "YES";
  else if (check(n2, n1, n3))
    cout << "YES";
  else if (check(n3, n2, n1))
    cout << "YES";
  else if (check(n3, n1, n2))
    cout << "YES";
  else
    cout << "NO";
  return 0;
}
