N, M = map(int,input().split()) A = list(map(int,input().split())) A.reverse() for i in range(M): A[i] -= 1 # 二次元表現の頂点を一次元にする def vnum(i, j): return j * N + i # N * (M + 1) のグリッドを作る adj = [[] for _ in range(N * (M + 1))] # 横の双方向の辺を張る for i in range(N - 1): for j in range(M + 1): adj[vnum(i, j)].append((vnum(i + 1, j), 1)) adj[vnum(i + 1, j)].append((vnum(i, j), 1)) # 縦 / クロスの辺を張る for j in range(M): a = A[j] for i in range(N): if a == i: adj[vnum(i, j)].append((vnum(i + 1, j + 1), 0)) continue if a == i-1: adj[vnum(i, j)].append((vnum(i - 1, j + 1), 0)) continue adj[vnum(i, j)].append((vnum(i, j + 1), 0)) # ダイクストラする from heapq import heappush, heappop def dijkstra(s, adj): n = len(adj) dist = [float('inf')] * n dist[s] = 0 que = [(0, s)] while que: d, v = heappop(que) if dist[v] != d: continue for w, cost in adj[v]: if dist[v] + cost < dist[w]: dist[w] = dist[v] + cost heappush(que, (dist[w], w)) return dist dist = dijkstra(vnum(0, 0), adj) ans = [] for i in range(1, N): ans.append(dist[vnum(i, M)]) print(*ans)