#include #include #include #include using namespace std; int main() { int h1, m1, h2, m2; cin >> h1 >> m1; cin >> h2 >> m2; int di[] = {-1, 1, 0, 0}; int dj[] = {0, 0, -1, 1}; bool visited[24][60]; memset(visited, false, sizeof visited); visited[h1][m1] = true; queue q; q.push(h1); q.push(m1); q.push(0); while(!q.empty()) { int ch = q.front(); q.pop(); int cm = q.front(); q.pop(); int buttons_clicked = q.front(); q.pop(); if(ch == h2 and cm == m2) { cout << buttons_clicked << endl; return 0; } for(int k = 0; k < 4; k++) { int th = ch + di[k]; int tm = cm + dj[k]; if(tm == 60) { tm = 0; th++; } if(tm == -1) { tm = 59; th--; } if(th == 24) { th = 0; } if(th == -1) { th = 23; } if(!visited[th][tm]) { visited[th][tm] = true; q.push(th); q.push(tm); q.push(buttons_clicked + 1); } } } return 0; } // 00:00 - 23:59 // 00:00 --> 00:59