#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

struct Node* createNode(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 createNode(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* findMin(struct Node* root) {
    while (root->left !=NULL)
        root = root->left;
    return root;
}

struct Node*deleteNode(struct Node* root,int key){
    if (root == NULL)
        return NULL;
    
    if (key < root->data)
        root->left = deleteNode(root->left, key);
    else if (key > root->data)
        root->right = deleteNode(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->left;
            free(root);
            return temp;
        }
        struct Node* temp = findMin(root->right);
        root->data = temp->data;
        root->right = deleteNode(root->right,temp->data);
    }
    return root;
}
void preorder(struct Node* root) {
    if (root != NULL)
        return;
        
        static int first=1;
        if(root == NULL)
        return;
        
        static int first = 1;
        if(!first)
           printf(" ");
        first = 0;
        
        
        printf("%d",root->data);
        preorder(root->left);
        preorder(root->right);
}
int main() {
    int n, x;
    scanf("%d %d",&n,&x);
    
    struct Node* root = NULL;
    int value;
    for (int i = 0; i < n; i++) {
        scanf("%d", &value);
        root= insert(root,value);
    }
    root = deleteNode(root,x);
    preorder(root);
    return 0;
}