// editor5
#include<stdio.h>
#include<stdlib.h>
struct Node{
  int data;
  struct node*next,*prev;
};
int main(){
    int n,x,found=0;
    scanf("%d",&n);
    struct node*head=NULL,*tail=NULL,*t;
    for(int i=0;i<n;i++){
        t=(struct node*)malloc(sizeof(struct node));
        scanf("%d",&t->data);
        if(!head){
            head=tail=t;
            t->next=t->prev=t;
        }
        else{
            t->prev=tail;
            t->next=head;
            tail->next=t;
            head->prev=t;
            tail=t;
        }
    }
    scanf("%d",&x);
    
    if(!head){
        printf("Node not found");
        return 0;
    }
    t=head;
    do{
        if(t->data==x){
            found=1;
            if(t->next==t){
                free(t);
                head=NULL;
            }
            else{
                t->prev->next=t->next;
                t->next->prev=t->prev;
                if(t==head)head=t->next;
            }
            free(t);
            break;
        }
        t=t->next;
    }
    while(t!=head);
    if(!found){
        printf("Node not found");
        return 0;
    }
    t=head;
    do{
        printf("%d",t->data);
        t=t->next;
        if(t!=head)printf(" ");
        
    }
    while(t!=head);
    return 0;
    
}