#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define MAX 100
typedef struct Node {
    int key;
    struct Node *left, *right;
} Node;
Node* newNode(int item){
    Node* temp = (node*)malloc(sizeof(Node));
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
    
}
Node* sortedArrayToBST(int arr[],int start, int end){
    if (start > end)
    return NULL;
    int mid =(start + end)/2;
    Node* root = newNode(arr[mid]);
    root->left = sortedArrayToBST(arr, start,mid - 1);
    root->right = sortedArrayToBST(arr,mid + 1,end);
    return root;
    
}
Node*minValueNode(Node* node)
{
    Node* current = node;
    while (current && current->left !=NULL)
    current = current->left;
    return current;
    
}
Node* deleteNode(Node* root, in key)
{
    if(root == NULL)return root;
    if(key < root->key)
    root->left = deleteNode(root->left, key);
    else if (key > root->key)
    root->right = deleteNode(root->right, key);
    else{
        if (root->left ==NULL){
            Node* temp = root->right;
            free(root);
            return temp;
        }
        else if (root->right == NULL){
            Node* temp = root->left;
            return temp;
        }
        Node*temp = minValueNode(root->right);
        root->key = temp->key;
        root->right = deleteNode(root->right, temp->key);
    }
    return root;
}
void preorder(Node* root){
    if(root != NULL){
        printf("%d", root->key);
        preorder(root->left);
        preorder(root->right);
    }
}
int cmp(const void*a, const void *b){
    return (*(int*)a -*(int*)b);
}
int main(){
    char line1[MAX],line2[MAX];
    int n,x;
    if (fgets(line1,sizeof(line1), stdin) == NULL){
        printf("Invalid input\n");
        return 0;
    }
    char *token = strtok(line1, "\n");
    if (!token || !isNumber(token)){
        printf("Invalid input\n");
        return 0;
        
    }
    n =atoi(token);
    
    token = strtok(NULL, "\n");
    if (!token || !isNumber(token)){
        printf("Invalid input\n");
        return 0;
    }
    x = atoi(token);
    if(n < 0 || n > 20){
        printf("Invalid input\n");
        return 0;
    }
    if (fgets(line2,sizeof(line2),stdin) == NULL){
        printf("Invalid input\n");
        return 0;
    }
    int values[20], count = 0;
    token = strtok (line2,"\n");
    while (token != NULL){
        if (!isNumber(token)){
            printf("Invalid input\n");
            return 0;
        }
        int val = atoi(token);
        if (val < 0 || val > 1000){
            
            printf("Invalid input\n");
            return 0;
        }
        values[count++] = val;
        token = strtok(NULL, "\n");
        
    }
    if (count != n){
        printf("Invalid input\n");
        return 0;
    }
    qsort(values, n, sizeof(int),cmp);
    Node* root =sortedArrayToBST(values,0,n-1);
    root = deleteNode(root,x);
    preorder(root);
    printf("\n");
    return 0;
}