#include <bits/stdc++.h>
using namespace std;
bool correct(int bd, int bm, int by) {
  bool can = true;
  if (bm > 12) can = false;
  if (bm == 1 || bm == 3 || bm == 5 || bm == 7 || bm == 8 || bm == 10 ||
      bm == 12) {
    if (bd > 31) can = false;
  } else if (bm == 2) {
    if (by % 4 == 0) {
      if (bd > 29) can = false;
    } else {
      if (bd > 28) can = false;
    }
  } else {
    if (bd > 30) can = false;
  }
  return can;
}
int main() {
  ios_base::sync_with_stdio(false);
  string fin, bir;
  cin >> fin >> bir;
  int dd = 0, mm = 0, yy = 0, bd = 0, bm = 0, by = 0;
  dd += (fin[0] - '0') * 10;
  dd += (fin[1] - '0');
  mm += (fin[3] - '0') * 10;
  mm += (fin[4] - '0');
  yy += (fin[6] - '0') * 10;
  yy += (fin[7] - '0');
  bd += (bir[0] - '0') * 10;
  bd += (bir[1] - '0');
  bm += (bir[3] - '0') * 10;
  bm += (bir[4] - '0');
  by += (bir[6] - '0') * 10;
  by += (bir[7] - '0');
  bool can = false;
  {
    if (yy - by > 18)
      can = true;
    else if (yy - by == 18) {
      if (mm > bm)
        can = true;
      else if (mm == bm) {
        if (dd >= bd) can = true;
      }
    }
  }
  {
    if (correct(bd, by, bm)) {
      int d = bd;
      int m = by;
      int y = bm;
      if (yy - y > 18)
        can = true;
      else if (yy - y == 18) {
        if (mm > m)
          can = true;
        else if (mm == m) {
          if (dd >= d) can = true;
        }
      }
    }
  }
  {
    if (correct(bm, bd, by)) {
      int d = bm;
      int m = bd;
      int y = by;
      if (yy - y > 18)
        can = true;
      else if (yy - y == 18) {
        if (mm > m)
          can = true;
        else if (mm == m) {
          if (dd >= d) can = true;
        }
      }
    }
  }
  {
    if (correct(bm, by, bd)) {
      int d = bm;
      int m = by;
      int y = bd;
      if (yy - y > 18)
        can = true;
      else if (yy - y == 18) {
        if (mm > m)
          can = true;
        else if (mm == m) {
          if (dd >= d) can = true;
        }
      }
    }
  }
  {
    if (correct(by, bd, bm)) {
      int d = by;
      int m = bd;
      int y = bm;
      if (yy - y > 18)
        can = true;
      else if (yy - y == 18) {
        if (mm > m)
          can = true;
        else if (mm == m) {
          if (dd >= d) can = true;
        }
      }
    }
  }
  {
    if (correct(by, bm, bd)) {
      int d = by;
      int m = bm;
      int y = bd;
      if (yy - y > 18)
        can = true;
      else if (yy - y == 18) {
        if (mm > m)
          can = true;
        else if (mm == m) {
          if (dd >= d) can = true;
        }
      }
    }
  }
  if (can)
    cout << "YES\n";
  else
    cout << "NO\n";
  return 0;
}
