#include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector a(n), kmp(n + 1); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 2; i <= n; i++) { int &u = kmp[i]; for (u = i - 1; u > 0 && a[u] != a[i - 1]; u = kmp[u]); u += (u == 0 && a[u] == a[i - 1]); } vector b(n); for (int i = 0; i < n; i++) { cin >> b[i]; } bool ok = false; if (k > n) { ok |= (a == b); } else if (k == n) { ok = (a == b); reverse(b.begin(), b.end()); ok |= (a == b); } else if (k == n - 1) { { int x = 0; for (int i = 0; i < n; i++) { while (x == n || (x > 0 && a[x] != b[i])) { x = kmp[x]; } x += (a[x] == b[i]); } for (int i = 0; i < n; i++) { while (x == n || (x > 0 && a[x] != b[i])) { x = kmp[x]; } x += (a[x] == b[i]); ok |= (x == n); } } reverse(a.begin(), a.end()); { int x = 0; for (int i = 0; i < n; i++) { while (x == n || (x > 0 && a[x] != b[i])) { x = kmp[x]; } x += (a[x] == b[i]); } for (int i = 0; i < n; i++) { while (x == n || (x > 0 && a[x] != b[i])) { x = kmp[x]; } x += (a[x] == b[i]); ok |= (x == n); } } } else { sort(a.begin(), a.end()); sort(b.begin(), b.end()); ok |= (a == b); } cout << (ok ? "Yes\n" : "No\n"); }