import strutils, sequtils, sets when isMainModule: var tmp = stdin.readLine.split.map(parseInt) (n, m) = (tmp[0], tmp[1]) a = newSeqWith(m, newSeqWith(n, newSeq[int](n))) for i in 0..m-1: for j in 0..n-1: a[i][j] = stdin.readLine.split.map(parseInt) var min = 1 shl 30 for i in 0..m-1: for j in i+1..m-1: # yoko * yoko for li in 0..n-1: for lj in 0..n-1: var cnt = initSet[int]() for ci in 0..n-1: cnt.incl(a[i][li][ci]) for cj in 0..n-1: cnt.incl(a[j][lj][cj]) min = min(min, cnt.len) # tate * tate for ci in 0..n-1: for cj in 0..n-1: var cnt = initSet[int]() for li in 0..n-1: cnt.incl(a[i][li][ci]) for lj in 0..n-1: cnt.incl(a[j][lj][cj]) min = min(min, cnt.len) # yoko * tate for li in 0..n-1: for cj in 0..n-1: var cnt = initSet[int]() for ci in 0..n-1: cnt.incl(a[i][li][ci]) for lj in 0..n-1: cnt.incl(a[j][lj][cj]) min = min(min, cnt.len) # tate * yoko for ci in 0..n-1: for lj in 0..n-1: var cnt = initSet[int]() for li in 0..n-1: cnt.incl(a[i][li][ci]) for cj in 0..n-1: cnt.incl(a[j][lj][cj]) min = min(min, cnt.len) # naname * naname for si in 0..1: for sj in 0..1: var cnt = initSet[int]() for k in 0..n-1: if si == 0: cnt.incl(a[i][k][k]) else: cnt.incl(a[i][k][n-k-1]) for k in 0..n-1: if sj == 0: cnt.incl(a[j][k][k]) else: cnt.incl(a[j][k][n-k-1]) min = min(min, cnt.len) # yoko * naname for li in 0..n-1: for sj in 0..1: var cnt = initSet[int]() for ci in 0..n-1: cnt.incl(a[i][li][ci]) for k in 0..n-1: if sj == 0: cnt.incl(a[j][k][k]) else: cnt.incl(a[j][k][n-k-1]) min = min(min, cnt.len) # naname * yoko for si in 0..1: for lj in 0..n-1: var cnt = initSet[int]() for k in 0..n-1: if si == 0: cnt.incl(a[i][k][k]) else: cnt.incl(a[i][k][n-k-1]) for cj in 0..n-1: cnt.incl(a[j][lj][cj]) min = min(min, cnt.len) # tate * naname for ci in 0..n-1: for sj in 0..1: var cnt = initSet[int]() for li in 0..n-1: cnt.incl(a[i][li][ci]) for k in 0..n-1: if sj == 0: cnt.incl(a[j][k][k]) else: cnt.incl(a[j][k][n-k-1]) min = min(min, cnt.len) # naname * tate for si in 0..1: for cj in 0..n-1: var cnt = initSet[int]() for k in 0..n-1: if si == 0: cnt.incl(a[i][k][k]) else: cnt.incl(a[i][k][n-k-1]) for lj in 0..n-1: cnt.incl(a[j][lj][cj]) min = min(min, cnt.len) echo min - 1