#include<stdio.h>
#include<stdlib.h>
 typedef struct Tree{
     int data;
     struct Tree *left,*right;
     
 }tree
 
 tree *root=NULL;
 tree *bran;
 
 tree *create(int num){
     bran=(tree*)malloc(1 * sizeof(tree));
     bran->data=num;
     bran->left=NULL:
     bran->right=NULL;
     return bran;
 }
 
 tree *insert(tree *root,int num){
     if(root == NULL){
         return create(num);
     }
     if(root->data>num){
         root->left=insert(root->left,num);
         
     }
     else if(root->data<num){
         root->right=insert(root->right,num);
     }
     return root;
 }
 
 tree *del(tree *root,int num){
     if(root == NULL){
         return root;
     }
     if(root->data>num){
         root->left=del(root->left,num);
     }
     else if(root->data<num){
         root->right=del(root->right,num);
     }
     else{
         if(root->left==NULL && root->right==NULL){
             return NULL;
         }
         if(root->left==NULL && root->right!=NULL){
             return root->right;
         }
         else if(root->right==NULL && root->left!=NULL){
             return root->left;
         }
     }
     tree *temp=del(root->right,num);
     root->data=temp->data;
     root->right=del(root->right,temp->data);
 }
 
 void inorder(tree *root){
     if(root!=NULL){
         inorder(root->left);
         printf("%d",root->data);
         inorder(root->right);
     }
 }
 
 int main(){
     int size,num,k;
     int ind;
     int arr[size];
     scanf("%d",&size);
     for(ind=0;ind<size;ind++){
         scanf("%d",&num);
         root=insert(root,num);
         
     }
     scanf("%d",&k);
     if(arr[i]==k){
         c++;
     }
     else{
         printf("-1");
        
     }
     del(root,num);
     inorder(root);
     return 0;
 }