#include<stdio.h>
#include<stdlib.h>
#include <string.h>
struct Node{
    int key;
    struct Node *left, *right;
    int height;
};
int height(struct Node *n){
    if(n == NULL)
    return 0;
    return n->height;
}
int max(int a,int b){
    return (a>b)?a:b;
}
struct Node* newNode(int key){
    struct Node* node=(struct Node *)malloc(sizeof(struct Node));
    node->key = key;
    node->left = node->right = NULL;
    node->height = 1;
    return node;
}
struct Node* rightRotate(struct Node *y){
    struct Node *x = y->left;
    struct Node *T2 = x->right;
    x->right = y;
    y->left = T2;
    y->height = max(height(y->left),height(y->right))+1;
    x->height = max(height(x->left),height(x->right))+1;
    return x;
}
struct Node* leftRotate(struct Node *x){
    struct Node *y = x->right;
    struct Node *T2 = y->left;
    y->left = x;
    x->right = T2;
    x->height=max(height(x->left),height(x->right))+1;
    y->height=max(height(y->left),height(y->right))+1;
    return y;
}
int getBalance(struct Node *n){
    if(n == NULL)
    return 0;
    return height(n->left)-height(n->right);
}
struct Node*insert(struct Node *node,int key){
    if(node == NULL)
    return newNode(key);
    
    if(key < node->key)
    node->left = insert(node->left, key);
    else if(key >node->key)
    node->right=insert(node->right, key);
    else
    return node; 
    
    node->height=1+max(height(node->left),height(node->right));
    
    int bal=getBalance(node);
     
     if(bal>1&&key<node->left->key)
       return rightRotate(node);
    
    if(bal<-1&&key>node->right->key)
       return leftRotate(node);
    
    if(bal>1&&key>node->left->key){
        node->left=leftRotate(node->left);
        return rightRotate(node); 
    }
    if(bal<-1&&key<node->right->key){
        node->right=rightRotate(node->right);
        return leftRotate(node);
    }
    return node;
}
struct Node*minNode(struct Node*node){
    struct Node* current = node;
    while(current && current->left !=NULL)
        current = current->left
    return current;
}

struct Node*deleteNode(struct Node*root,int 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)|| (root->right == NULL)){
            struct Node *temp=root->left?root->left:root->right;
            if(temp == NULL){
                temp=root;
                root=NULL;
            }else{
                *root=*temp;
            }
            free(temp);
        }else{
            struct Node*temp=minNode(root->right);
            root->key=temp->key;
            root->right=deleteNode(root->right,temp->key);
        }
    }
    if(root == NULL)
    return root;
    
    root->height=1+max(height(root->left),height(root->right));
    
    int bal=getBalance(root);
    
    if(bal>1&&getBalance(root->left)>=0)
        return rightRotate(root);
    
    if(bal>1&&getBalance(root->left)<0){
        root->left=leftRotate(root->left);
        return rightRotate(root);
    }
    if(bal<-1&&getBalance(root->right)<=0)
        return leftRotate(root);
    
    if(bal<-1&&getBalance(root->right)>0){
        root->right=rightRotate(root->right);
        return leftRotate(root);
        }
        return root;
   }
   void preOrder(struct Node* root,int *first){
       if(root!=NULL){
           if(*first){
           printf("%d ",root->key);
            *first = 0;
           }else{
               printf(" %d",root->key);
           }
           preOrder(root->left, first);
           preOrder(root->right, first);
       }
   }
   int main(){
       int n,x;
        
        if(scanf("%d",&n)!=1){
            printf("Invalid input");
            return 0;
        } 
        
        if(scanf("%d",&x)!=1){
            printf("Invalid input");
            return 0;
        }
        
        if(n < -10 || n > 20){
            printf("Invalid input");
            return 0;
        }
        
        if(n<0){
            printf("Invalid input");
            return;
        }
       struct Node *root=NULL;
       
       for(int i=0;i<n;i++){
           int val;
           if(scanf("%d",&val)!=1){
               printf("Invalid input");
               return 0;
           }
       
           if(val<0||val>1000){
               printf("Invalid input");
               return 0;
           }
           root=insert(root,val);
       }
       root=deleteNode(root,x);
       int first = 1;
       preOrder(root,&first);
       return 0;
   }