#include using namespace std; #include using namespace atcoder; void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } int main() { fast_io(); int n, m; cin >> n >> m; vector a(n), b(m); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < m; i++) { cin >> b[i]; } const int INF = 1e9; int ans = INF; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i] == b[j]) { auto [y, z] = crt({i, j}, {n, m}); ans = min(ans, (int)y + 1); } } } cout << (ans == INF ? -1 : ans) << endl; }