#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
struct Node {
    int data;
    struct Node* next;
};
struct Node* createNode(int val) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data= val;
    newNode->next=NULL;
    return newNode;
}
 void insertEnd(struct Node** head,int val) {
     struct Node* newNode = createNode(val);
     if (*head==NULL){
         *head=newNode;
         newNode->next = *head;
     } else {
         struct Node* temp =*head;
         while (temp->next !=*head)
         temp=temp->next;
         temp->next = newNode;
         newNode->next=*head;
     }
     }
     void sortCircular(struct Node* head) {
         if (head == NULL) return ;
         struct Node* current=head;
         struct Node* index =NULL;
         int temp;
         do {
             index = current->next;
             while (index != head) {
                 if(current->data>index->data) {
                     temp = current->data;
                     current->data=index->data;
                     index->data=temp;
             }
             index=index->next;
             }
             current = current->next;
         } while (current->next !=head);
         }
         void displayAndFindExtremes(struct Node* head) {
             if (head == NULL) return ;
             struct Node* temp=head;
             int smallest=head->data;
             int largest=head->data;
              do {
                  printf("%d ",temp->data);
                  if(temp->data<smallest) smallest=temp->data;
                  if(temp->data>largest)largest=temp->data;
                  temp=temp->next;
              }
              while (temp !=head);
              printf("\nSmallest: %d\nLargest:%d\n",smallest,largest);
         }
         int isvalidNumber(char* str) {
             for (int i=0;str[i]!='\0';i++){
                 if (!isdigit(str[i]) && str[i] !='-') return 0;
             }
             return 1;
         }
         int main(){
             int n;
             if (scanf("%d",&n)!=1||n<1||n>100){
                 printf("Invalid input\n");
                 return 0;
                 
             }
             struct Node*head=NULL;
             char buffer[101];
             if (scanf("%s",buffer) !=1||!isvalidNumber(buffer)) {
                 printf("Invalid input\n");
                 return 0;
             }
             insertEnd(&head,atoi(buffer));
         }
         SortCircular(head);
         displayAndFindExtremes(head);
         return 0;
         }