#include #include using namespace std; int parse(string s) { int hh = stoi(s.substr(0, 2)); int mm = stoi(s.substr(3, 2)); int ss = stoi(s.substr(6, 2)); return 3600 * hh + 60 * mm + ss; } int main() { int n, m; cin >> n >> m; vector a(n), b(m); for(auto& x: a) { string s; cin >> s; x = parse(s); } for(auto& x: b) { string s; cin >> s; x = parse(s); } int thresh; cin >> thresh; int ret = 1e9; for(auto x: a) for(auto y: b) { if(y-x >= thresh) ret = min(ret, y - x); } if(ret == 1e9) ret = -1; cout << ret << "\n"; }