N, M = map(int, input().split())
a = list(map(int, input().split()))

G: list = [[] for _ in range(N)]
for i in range(M):
    G[a[i] - 1].append(a[i])
    G[a[i]].append(a[i] - 1)

C: dict = {}

rest: set = set(range(N))

while rest:
    fst = rest.pop()
    stack: list = [fst]
    c = []
    m = fst
    while stack:
        cur = stack.pop()
        c.append(cur)
        m = min(m, cur)
        for nx in G[cur]:
            if nx in rest:
                stack.append(nx)
                rest.remove(nx)
    C[m] = c

ans: list = []
for i in sorted(C.keys()):
    for ci in sorted(C[i], reverse=True):
        ans.append(ci + 1)

print(*ans)
