#include #define FASTIO using namespace std; using ll = long long; using Vi = std::vector; using Vl = std::vector; using Pii = std::pair; using Pll = std::pair; constexpr int I_INF = std::numeric_limits::max(); constexpr ll L_INF = std::numeric_limits::max(); template inline bool chmin(T1& a, const T2& b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T1& a, const T2& b) { if (a < b) { a = b; return true; } return false; } template class Prints { private: class __Prints { public: __Prints(const char* sep, const char* term) : sep(sep), term(term) {} template #if __cplusplus >= 201703L auto operator()(const Args&... args) const -> decltype((os << ... << std::declval()), void()) { #else void operator()(const Args&... args) const { #endif print(args...); } template #if __cplusplus >= 201703L auto pvec(const T& vec, size_t sz) const -> decltype(os << std::declval()[0])>(), void()) { #else void pvec(const T& vec, size_t sz) const { #endif for (size_t i = 0; i < sz; i++) os << vec[i] << (i == sz - 1 ? term : sep); } template #if __cplusplus >= 201703L auto pmat(const T& mat, size_t h, size_t w) const -> decltype(os << std::declval()[0][0])>(), void()) { #else void pmat(const T& mat, size_t h, size_t w) const { #endif for (size_t i = 0; i < h; i++) for (size_t j = 0; j < w; j++) os << mat[i][j] << (j == w - 1 ? term : sep); } private: const char *sep, *term; void print() const { os << term; } void print_rest() const { os << term; } template void print(const T& head, const Tail&... tail) const { os << head, print_rest(tail...); } template void print_rest(const T& head, const Tail&... tail) const { os << sep << head, print_rest(tail...); } }; public: Prints() {} __Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); } }; Prints<> prints; Prints prints_err; //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% template constexpr std::pair extended_euclidean(const T& a, const T& b) { T x = 1, y = 0, u = 0, v = 1, s = a, t = b; T k = 0, tmp = 0; while (t) { k = s / t, s -= k * t; tmp = s, s = t, t = tmp; x -= k * u; tmp = x, x = u, u = tmp; y -= k * v; tmp = y, y = v, v = tmp; } return {x, y}; } void solve() { ll N, M; cin >> N >> M; Vl A(N), B(M); for (ll i = 0; i < N; i++) { cin >> A[i]; } for (ll i = 0; i < M; i++) { cin >> B[i]; } auto [p, q] = extended_euclidean(N, M); ll d = N * p + M * q; ll l = N * M / d; ll ans = L_INF; for (ll i = 0; i < N; i++) { for (ll j = 0; j < M; j++) { if (A[i] == B[j] && (j - i) % d == 0) { ll s = (j - i) / d; ll x = i + s * N * p; x %= l; if (x < 0) x += l; chmin(ans, x); } } } if (ans == L_INF) { prints()(-1); } else { prints()(ans + 1); } } //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% int main() { #ifdef FASTIO std::cin.tie(nullptr), std::cout.tie(nullptr); std::ios::sync_with_stdio(false); #endif #ifdef FILEINPUT std::ifstream ifs("./in_out/input.txt"); std::cin.rdbuf(ifs.rdbuf()); #endif #ifdef FILEOUTPUT std::ofstream ofs("./in_out/output.txt"); std::cout.rdbuf(ofs.rdbuf()); #endif std::cout << std::setprecision(18) << std::fixed; solve(); std::cout << std::flush; return 0; }