#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct Node
{
    char *name;
    struct Node *next;
    struct Node *prev;
};
static struct Node *head = NULL; 
static struct Node *tail = NULL; 
static int currentSize = 0;
static void freeList(void) 
{
    struct Node *cur = head;
    while (cur) 
    {
        struct Node *tmp = cur;
        cur = cur->next;
        free(tmp->name);
        free(tmp);
    }
    head = tail = NULL;
    currentSize = 0;
}
static int isAlphaString(const char *s) 
{
    if (!s || *s == '\0') return 0;
    for (; *s; ++s) {
        if (!isalpha((unsigned char)*s)) return 0; 
    }
    return 1;
}
static char *trim(char *s) 
{
    if (!s) return s;
    while (*s && (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')) s++;
    size_t len = strlen(s);
    while (len && (s[len-1] == ' ' || s[len-1] == '\t' || s[len-1] == '\r' || s[len-1] == '\n'))
    {
        s[--len] = '\0';
    }
    return s;
}

static void addVisitor(const char *name, int limit) 
{
    struct Node node = (struct Node)malloc(sizeof(struct Node));
    node->name = (char*)malloc(strlen(name) + 1);
    strcpy(node->name, name);
    node->prev = NULL;
    node->next = head;
    if (head) head->prev = node;
    head = node;
    if (tail == NULL) tail = node; 
    currentSize++;
    if (currentSize > limit) 
    {
        struct Node *del = tail;
        tail = tail->prev;
        if (tail) tail->next = NULL;
        else head = NULL; 
        free(del->name);
        free(del);
        currentSize--;
    }
}
int main(void) 
{
    int n, limit;
    if (scanf("%d %d", &n, &limit) != 2 || n < 1 || n > 1000 || limit < 1 || limit > 1000) {
        printf("Invalid input\n");
        return 0;
    }
    int ch;
    while ((ch = getchar()) != '\n' && ch != EOF) { }
    char line[1024];
    for (int i = 0; i < n; i++) 
    {
        if (!fgets(line, sizeof(line), stdin)) 
        {
            printf("Invalid input\n");
            freeList();
            return 0;
        }
        char *name = trim(line);
        if (!isAlphaString(name)) 
        {
            printf("Invalid input\n");
            freeList();
            return 0;
        }
        addVisitor(name, limit);
    }
    struct Node *cur = head;
    while (cur) 
    {
        printf("%s", cur->name);
        if (cur->next) printf(" ");
        cur = cur->next;
    }
    printf("\n");
    freeList();
return 0;
}