#include<stdio.h>
#include<stdlib.h>
typedef struct dbcr
{
    int data;
    struct dbcr *next,*prev;
}node;
node *head=NULL,*tail=NULL;
void create(int n)
{
    node *nd=(node*)malloc(sizeof(node));
    nd->data=n;
    nd->next=NULL;
    nd->prev=NULL;
    if(head==NULL)
    {
        head=tail=nd;
    }
    else
    {
        nd->prev=tail;
        tail->next=nd;
        tail=nd;
    }
    tail->next=head;
    head->prev=tail;
}void print()
{
    node *temp=head;
    do
    {
        printf("%d ",temp->data);
        temp=temp->next;
    }
    while(temp!=head);
}
void del(int n,int val)
{
    node*temp=head,*temp2=NULL,*ptr=NULL;
    while(n!=0)
    {
        if(temp->data==val)
        {
            if(temp->prev=tail)
            {
                ptr=temp;
                head=head->next;
                tail-<next=head;
                free(temp);
                print();
                break;
            }
            else
            {
                ptr=temp;
                temp2->next=temp->next;
                temp->next->prev=temp2;
                print();
                break;
            }
        }
        temp2=temp;
        temp=temp->next;
        n--;
    }
    if(ptr==NULL)
    {
        printf("Node not found");
    }
}
int main()
{
    int n,data, val;
    scanf("%d",&n);
    if(n<0)
    {
        printf("Node not found");
        return 0;
    }
    for(int i=0;i<n;i++)
    {
        scanf("%d",&data);
        create(data);
    }
    scanf("%d",&val);
    del(n,val);
}