// editor1
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
    int data;
    struct Node *left,*right;
}nd;
nd *root=NULL;
nd *create(int v){
    nd *newnode=(nd*)malloc(sizeof(nd));
    newnode->data=v;
    newnode->left=newnode->right=NULL;
    return newnode;
    
}
nd* insert(nd *root,int v){
    if(root==NULL)
      return create(v);
    if(v<root->data)
      root->left=insert(root->left,v);
    else
      root->right=insert(root->right,v);
      return root;
}
nd* findMin(nd* root){
    while(root->left!=NULL)
    root=root->left;
    return root;
}
nd* deleteNode(nd* 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){
    nd *temp=root->right;
    free(root);
    return temp;
}
else if(root->right==NULL){
    nd *temp=root->left;
    free(root);
    return temp;
}
nd* temp=findMin(root->right);
root->data=temp->data;
root->right=deleteNode(root->right,temp->data);
}
return root;
}
int search(nd* root,int key){
    if(root!=NULL){
      inorder(root->left);
      printf("%d",root->data);
      inorder(root->right);
}
}
int main(){
    int n;
    scanf("%d",&n);
    if(n<=0){
        printf("Invalid input");
        return 0;
    }
    nd* root=NULL;
    int x;
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        root=insert(root,x);
    }
    int del;
    scanf("%d",&del);
    if(!search(root,del)){
        printf("-1");
    return 0;
    }
    root=deleteNode(root,del);
    inorder(root);
    return 0;
}