import sys
from collections import Counter
N, M = map(int, input().split())
words = [sys.stdin.readline().strip() for _ in range(N)]
filtered_words = [word for word in words if len(word) >= M]
word_counts = Counter(filtered_words)
sorted_words = sorted(word_counts.keys(), key=lambda x: (-word_counts[x], -len(x), x))
for word in sorted_words:
print(word)