#include <bits/stdc++.h>
using namespace std;
#define iter(v) v.begin(), v.end()
#define SZ(v) int(v.size())
#define pb emplace_back
#define ff first
#define ss second
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#ifdef zisk
void debug(){cerr << "\n";}
template<class T, class ... U>
void debug(T a, U ... b){ cerr << a << " ", debug(b...); }
template<class T> void pary(T l, T r){
while(l != r) cerr << *l << " ", l++;
cerr << "\n";
}
#else
#define debug(..) void()
#define pary(...) void()
#endif
template<class A, class B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.ff << ',' << p.ss << ')';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int m,n;
cin >> n >> m;
vector<int> v(n);
for(int i=0;i<n;i++){
cin >> v[i];
}
vector<int> g(m);
for(int i=0;i<m;i++){
cin >> g[i];
}
for(int i=0;i<n;i++){
if(v[i]==g[0]){
int flag=1;
for(int j=0;j<m;j++){
if(v[(i+j)%n]!=g[j]){
flag=0;
}
}
if(flag){
cout << 1 << "\n";
return 0;
}
flag=1;
for(int j=0;j<m;j++){
if(v[(i-j+n)%n]!=g[j]){
flag=0;
}
}
if(flag){
cout << 1 << "\n";
return 0;
}
break;
}
}
cout << 0 << "\n";
}