# ABC115C - Christmas Eve

def main():

    n, k = list(map(int, input().rstrip().split()))

    lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))

    ans = lst[k - 1] - lst[0]

    for i, j in zip(lst, lst[k - 1 :]):

        ans = min(ans, j - i)

    print(ans)





if __name__ == "__main__":

    main()
# ABC115C - Christmas Eve

import sys

input = sys.stdin.readline



def main():

    n, k = list(map(int, input().rstrip().split()))

    lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))

    ans = lst[k - 1] - lst[0]

    for i, j in zip(lst, lst[k - 1 :]):

        ans = min(ans, j - i)

    print(ans)





if __name__ == "__main__":

    main()
# ABC115C - Christmas Eve
def main():
    n, k = list(map(int, input().rstrip().split()))
    lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
    ans = lst[k - 1] - lst[0]
    for i, j in zip(lst, lst[k - 1 :]):
        ans = min(ans, j - i)
    print(ans)


if __name__ == "__main__":
    main()

# ABC115C - Christmas Eve
import sys

input = sys.stdin.readline


def main():
    n, k = list(map(int, input().rstrip().split()))
    lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
    ans = lst[k - 1] - lst[0]
    for i, j in zip(lst, lst[k - 1 :]):
        ans = min(ans, j - i)
    print(ans)


if __name__ == "__main__":
    main()

+import sys + +input = sys.stdin.readline + +