n,k=map(int,input().split()) a=list(map(int,input().split())) c=list(map(lambda x:int(x)-1,input().split())) dp=[[[0]*n for i in range(n)] for j in range(50)] for d in range(n): for l in range(n): r=l+d if l==r: dp[l][r][c[l]]=1 continue for m in range(l,r): for j in range(50): if dp[l][m%n][j]: for j2 in range(j-k,j+k+1): if 0<=j2<50 and dp[(m+1)%n][r%n][j2]: dp[l][r%n][j]=1 dp[l][r%n][j2]=1 ans=0 for i in range(n): for j in range(n): flag=False for x in range(50): if dp[i][j][x]: flag=True if flag: res=0 if r