#include <bits/stdc++.h>
using namespace std;
long long str2num(string s) {
  long long ans = 0;
  for (int i = 0; i < s.size(); i++) {
    ans *= 10;
    ans += s[i] - '0';
  }
  return ans;
}
bool teste(int x) {
  while (x > 0) {
    if (x % 10 == 7) return true;
    x /= 10;
  }
  return false;
}
int main() {
  int x, c, a, b, ans = 0;
  string s;
  cin >> x >> s;
  a = str2num(s);
  cin >> s;
  b = str2num(s);
  while (!teste(a) && !teste(b)) {
    ans++;
    if (b - x >= 0)
      b -= x;
    else {
      a--;
      b = (b - x) + 60;
    }
    if (a < 0) a = 23;
  }
  cout << ans;
  return 0;
}
