#include #define rep(i,n) for (int i = 0; i < int(n); ++i) #define all(v) v.begin(),v.end() using namespace std; using ull = unsigned long long; void yes(){ printf("Yes\n"); } void no(){ printf("No\n"); } struct RollingHash { RollingHash(const string &s = ""){ build(s);} ull get(int l, int r){ assert(0 <= l && l <= r && r <= n); return cal_mod(inner_hash[r] + POSITIVISER - mul_mod(inner_hash[l], pow_base[r-l])); } static ull get_hash(const string &s){ int len = s.size(); set_hash(); extend_pow_base(len); ull res = 0; for (int i = 0; i < len; i++) res = cal_mod(mul_mod(res,BASE) + s[i]); return res; } private: static constexpr ull MASK30 = (1ULL << 30) - 1; static constexpr ull MASK31 = (1ULL << 31) - 1; static constexpr ull MASK61 = (1ULL << 61) - 1; static constexpr ull MOD = (1ULL << 61) - 1; static constexpr ull POSITIVISER = MOD * 4; static ull BASE; static vector pow_base; static ull mul_mod(ull a, ull b){ ull au = a >> 31, ad = a & MASK31; ull bu = b >> 31, bd = b & MASK31; ull mid = ad * bu + au * bd; ull midu = mid >> 30, midd = mid & MASK30; return (au * bu * 2 + midu + (midd << 31) + ad * bd); } static ull cal_mod(ull x){ ull xu = x >> 61; ull xd = x & MASK61; ull res = xu + xd; if (res >= MOD) res -= MOD; return res; } static void set_hash(){ if (BASE == 0) BASE = (1UL<<31) + (random_device()() & MASK31); } static void extend_pow_base(int len){ int nlen = pow_base.size(); if (nlen > len) return ; pow_base.resize(len+1); for (int i = nlen; i <= len; i++){ pow_base[i] = cal_mod(mul_mod(pow_base[i-1],BASE)); } } string str; int n; vector inner_hash; void build(const string &s){ set_hash(); str = s; n = s.size(); extend_pow_base(n); inner_hash.resize(n+1); inner_hash[0] = 0; for (int i = 0; i < n; i++) inner_hash[i+1] = cal_mod(mul_mod(inner_hash[i],BASE) + s[i]); } }; using ull = unsigned long long; ull RollingHash::BASE = 0; vector RollingHash::pow_base = vector(1,1); using roriha = RollingHash; int main(){ int n, k; cin >> n >> k; vector a(n), b(n); rep(i,n) cin >> a[i]; rep(i,n) cin >> b[i]; if (a == b){ yes(); return 0; } if (k > n){ no(); return 0; } if (k == n){ reverse(all(b)); a == b ? yes() : no(); return 0; } string as = ""; rep(i,n) as += 'a' + a[i]; as += as; string bs = ""; rep(i,n) bs += 'a' + b[i]; string cs = bs; reverse(all(cs)); sort(all(a)), sort(all(b)); if (a != b){ no(); return 0; } if (k <= n-2){ yes(); return 0; } ull hb = roriha::get_hash(bs); ull hc = roriha::get_hash(cs); roriha ha(as); rep(i,n){ ull h = ha.get(i,i+n); if (h == hb || h == hc){ yes(); return 0; } } no(); return 0; }