#include using namespace std; typedef long long i64; typedef unsigned long long ui64; typedef vector vi; typedef vector vvi; typedef pair pi; #define pb push_back #define sz(a) i64((a).size()) #define all(c) (c).begin(), (c).end() #define REP(s, e, i) for(i=(s); i < (e); ++i) inline void RI(i64 &i) {scanf("%lld", &(i));} inline void RVI(vi &v) { for(i64 i=0;i inline bool IN(T &S, const typename T::key_type &key) { return S.find(key) != S.end(); } template inline bool ON(const T &b, i64 idx) { return ((T(1) << idx) & b) != 0; } int main(int argc, char *argv[]) { i64 i, j, k; i64 N, M; cin >> N >> M; vi A(N), B(M); RVI(A); RVI(B); i = 0; REP(0, N*M, i) { if(A[i%N] == B[i%M]) { WI(i+1); return 0; } } WI(-1); return 0; }