#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; int main() { int32_t n, m; cin >> n >> m; vector as(n), bs(m); for (auto &&a : as) { cin >> a; } for (auto &&b : bs) { cin >> b; } int32_t ans = -1; for (auto i = 0; i < n * m; ++i) { if (as[i % n] == bs[i % m]) { ans = i + 1; break; } } cout << ans << endl; return 0; }