#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, hh, mm, c = 0, p, q;
  cin >> x;
  string h, m;
  cin >> h >> m;
  hh = (h[0] - 48) * 10 + (h[1] - 48);
  mm = (m[0] - 48) * 10 + (m[1] - 48);
  for (;;) {
    if ((h[0] == '7' || h[1] == '7' || m[0] == '7' || m[1] == '7') &&
        c % x == 0) {
      cout << c / x;
      return 0;
    }
    hh = (h[0] - 48) * 10 + (h[1] - 48);
    mm = (m[0] - 48) * 10 + (m[1] - 48);
    if (m == "00") {
      m = "59";
      if (h == "00")
        h = "23";
      else {
        --hh;
        p = hh / 10;
        q = hh % 10;
        h[0] = p + 48;
        h[1] = q + 48;
      }
    } else {
      --mm;
      p = mm / 10;
      q = mm % 10;
      m[0] = p + 48;
      m[1] = q + 48;
    }
    c++;
    if ((h[0] == '7' || h[1] == '7' || m[0] == '7' || m[1] == '7') &&
        c % x == 0) {
      cout << c / x;
      return 0;
    }
  }
}
