class Rolling_Hash: def __init__(self,lst,base,mod=0,e_sum=0,e_prod=1): self.len=len(lst) self.base=base self.mod=mod self.e_sum=e_sum self.e_prod=e_prod self.rolling_hash=[lst[i] for i in range(self.len)]+[self.e_sum] self.pow_base=[self.e_prod] for i in range(self.len-1,-1,-1): self.rolling_hash[i]+=self.rolling_hash[i+1]*self.base self.pow_base.append(self.pow_base[-1]*self.base) if self.mod: self.rolling_hash[i]%=self.mod self.pow_base[-1]%=self.mod def __getitem__(self,i): if type(i)==int: a,b=i,i+1 else: a,b=i.start,i.stop if a==None or a<-self.len: a=0 elif self.len<=a: a=self.len elif a<0: a+=self.len if b==None or self.len<=b: b=self.len elif b<-self.len: b=0 elif b<0: b+=self.len h=self.rolling_hash[a]-self.rolling_hash[b]*self.pow_base[b-a] if self.mod: h%=self.mod return h def __len__(self): return self.len N,K=map(int,input().split()) A=list(map(int,input().split())) B=list(map(int,input().split())) ans="No" if K==N: if A==B or A==B[::-1]: ans="Yes" elif K==N-1: base=10 mod=(1<<61)-1 A0=Rolling_Hash(A,base,mod)[:] A1=Rolling_Hash(A[::-1],base,mod)[:] B=Rolling_Hash(B*2,base,mod) for i in range(N): if A0==B[i:i+N] or A1==B[i:i+N]: ans="Yes" else: if sorted(A)==sorted(B): ans="Yes" print(ans)