#include<stdio.h>
#include<stdlib.h>
struct Node{
    int data;
    struct Node *left;
    struct Node *right;
};
 struct Node*create(int value){
    struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
    newNode->data=value;
    newNode->left=newNode->right=NULL;
    return newNode;
}
struct node*insert(struct Node*root,int value){
    if(root==NULL)
    return create(value);
    if(value < root->data)
    root->left=insert(root->left,value);
    else if(value > root->data)
    root->right=insert(root->right,value);
    return root;
}
struct Node*min(struct Node*root){
    while(root->left!=NULL)
    root=root->left;
    return root;
}
struct Node*delete(struct Node*root,int key){
    if(root==NULL)
    return NULL;
    if(key<root->data)
    root->left=delete(root->left,key);
    else if(key>root->data)
    root->right=delete(root->right,key);
    else{
        if(root->left==NULL){
            struct Node* temp=root->right;
            free(root);
            return temp;
        }
        else if(root->right==NULL){
            struct Node* temp=root->right;
            free(root);
            return temp;
        }
        struct Node* temp=min(root->right);
        root->data=temp->data;
        root->right=delete(root->right,temp->data);
    }
    return root;
}
void preorder(struct Node* root){
    if(root!=NULL){
        printf("%d ",root->data);
        preorder(root->left);
        preorder(root->right);
    }
}
int valid(int n){
    return(n>=0 && n<=20;)
}
int main(){
    int n,x;
    if(scanf("%d %d",&n,&x)!=2 || !valid(n)){
        printf("Invalid input");
        return 0;
    }
    struct Node*root=NULL;
    int value;
    for(int i=0;i<n;i++){
        if(scanf("%d ",&value)!=1 || value<0 || value>1000){
            printf("Invalid input");
            return 0;
        }
        root=insert(root,value);
    }
    root=delete(root,x);
    preorder(root);
    printf("\n");
    return 0;
}