#include<stdio.h>
#include<stdlib.h>
void display();

typedef struct Node{
    int data;
    struct Node *prev;
    struct Node *next;
}node;

node *head=NULL;
node *tail;
node *newnode;
node *temp;
node *temp1;

void create(int num){
    newnode=(node*)malloc(sizeof(node));
    newnode->data=num;
    newnode->prev=NULL;
    newnode->next=NULL;
    if(head==NULL){
        head=newnode;
        tail=newnode;
    }
    else{
        newnode->prev=tail;
        tail->next=newnode;
        tail=newnode;
    }
}

void swap(int v){
    int  cnt=1;
    int t;
    temp=head;
    head=head->next;
    if(cnt==temp){
        newnode=create(num);
        newnode->num=v;
        head->prev=v;
        head=v;
    }
    temp=temp->next;
    temp->next=temp1;
    temp1=temp1->next
    while(temp1->next!=NULL){
    if(temp<temp1){
      t=temp;
      temp=temp1;
      temp1=t;
    }
    }
}
void display(){
    node *itr;
    for(itr=head;itr!=NULL;itr=itr->next){
        printf("%d",itr->data);
    }
}
int main(){
    int size;
    int num,v,ind;
    scanf("%d",&size);
    if(size<0 || size==0){
        printf("Invalid input");
        return 0;
    }
    for(ind=0;ind<size;ind++){
        scanf("%d",&num);
        create(num);
    }
    scanf("%d",&v);
    swap(v);
    display();
    return 0;
}