#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h> // For isalnum

// Function to check if a string contains special characters
int containsSpecialCharacters(const char *name) {
    for (int i = 0; name[i] != '\0'; i++) {
        if (!isalnum(name[i])) { // Check if character is not alphanumeric
            return 1; // Contains special character
        }
    }
    return 0; // No special characters
}

int main() {
    int n, limit;
    if (scanf("%d %d", &n, &limit) != 2) {
        return 1; // Error reading n and limit
    }

    // Using a dynamic array of strings (char pointers) to store names
    char **visitorLog = (char **)malloc(limit * sizeof(char *));
    if (visitorLog == NULL) {
        return 1; // Memory allocation failed
    }

    int currentSize = 0; // Current number of names in the log

    // Read visitor names
    for (int i = 0; i < n; i++) {
        char nameBuffer[101]; // Assuming max name length is 100 characters
        if (scanf("%100s", nameBuffer) != 1) { // Read name
            // Handle error or end of input
            break;
        }

        if (containsSpecialCharacters(nameBuffer)) {
            printf("Invalid input\n");
            // Free allocated memory before exiting on invalid input
            for (int j = 0; j < currentSize; j++) {
                free(visitorLog[j]);
            }
            free(visitorLog);
            return 0; // Exit as per problem statement
        }

        // Add name to the log
        if (currentSize < limit) {
            // Log is not full, add to the end
            visitorLog[currentSize] = strdup(nameBuffer);
            if (visitorLog[currentSize] == NULL) {
                // Handle memory allocation error for string duplication
                for (int j = 0; j < currentSize; j++) {
                    free(visitorLog[j]);
                }
                free(visitorLog);
                return 1;
            }
            currentSize++;
        } else {
            // Log is full, discard oldest (first element) and shift
            free(visitorLog[0]); // Free the oldest name
            for (int j = 0; j < limit - 1; j++) {
                visitorLog[j] = visitorLog[j + 1];
            }
            visitorLog[limit - 1] = strdup(nameBuffer); // Add new name at the end
            if (visitorLog[limit - 1] == NULL) {
                // Handle memory allocation error for string duplication
                for (int j = 0; j < currentSize - 1; j++) { // Adjust loop for shifted elements
                    free(visitorLog[j]);
                }
                free(visitorLog);
                return 1;
            }
        }
    }

    // Print the current visitor log from newest to oldest
    for (int i = currentSize - 1; i >= 0; i--) {
        printf("%s%s", visitorLog[i], (i == 0) ? "" : " ");
    }
    printf("\n");

    // Free allocated memory
    for (int i = 0; i < currentSize; i++) {
        free(visitorLog[i]);
    }
    free(visitorLog);

    return 0;
}