#ifdef _MSC_VER
  #ifndef __GNUC__
    #pragma warning(disable: 4996)
  #endif
  #define main main0
#endif
#include <iostream>
using namespace std;
typedef long long           ll;
typedef unsigned long long ull;
typedef unsigned int      uint;

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);

  int x, d, h, m, wynik = 0;

  cin >> x >> d >>h >> m;

  // całe dni
  if(x == 5)
    x = 6;
  wynik = (x + 23 - d) * 24 * 60;
  if(x == 6 && wynik > 0)
    wynik -= 60;
  // do północy
  wynik += (24 - h) * 60 - m;
  if(d == 29 && h < 3)
    wynik -= 60;

  cout << wynik;

  return 0;
}
