#!/usr/bin/env python3
import sys
import heapq


def solve(N: int, M: int, A: "list[int]"):
    last_index = [0 for _ in range(M + 1)]
    for i in range(N):
        last_index[A[i]] = i
    partitions = []
    for i in range(1, M + 1):
        partitions.append(last_index[i])
    partitions.sort(reverse=True)

    l = -1
    is_used = [False] * (M + 1)
    is_called = [False] * N
    ans = []
    que: list = []
    while partitions:
        partition = partitions.pop()
        minimum_index = -1
        for i in range(l + 1, partition + 1):
            heapq.heappush(que, A[i] * N + i)
            is_called[i] = True
        if is_used[A[partition]]:
            continue
        while que:
            minimum = heapq.heappop(que)
            v, index = minimum // N, minimum % N
            if (minimum_index < index) and (not is_used[v]):
                minimum_index = index
                is_used[v] = True
                ans.append(v)
                if v == A[partition]:
                    break
        l = partition
    for i in range(1, M + 1):
        assert is_used[i]
    for i in range(N):
        assert is_called[i]
    print(*ans)


# Generated by 2.12.0 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, M, A)


if __name__ == "__main__":
    main()
