import java.util.*;
class Node{
int data;
Node next;
Node(int data){
this.data = data;
}
}
class Main{
public static Scanner scn = new Scanner(System.in);
public static void removeCycle(Node head){
Node slow = head;
Node fast = head;
while(fast.next!=null && fast.next.next!=null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast){
break; //cycle detected
}
}
if(slow == fast){
removeLoop(slow,head);
}
}
public static void removeLoop(Node loop, Node head){
Node ptr1 = loop;
Node ptr2 = loop;
//Step1 Count the no of nodes in loop(counter)
int counter = 1;
while(ptr1.next != ptr2){
ptr1 = ptr1.next;
counter++;
}
ptr1 = head;
ptr2 = head;
while(counter-->0){
ptr2 = ptr2.next; //move other pointer counter times ahead
}
while(ptr1 != ptr2){ //both of them will meet at starting point of loop
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
while(ptr2.next!=ptr1){ //finding the end node of loop to break
ptr2 = ptr2.next;
}
ptr2.next = null; //breaking the loop
}
public static Node buildList(){
HashMap <Integer, Node> map = new HashMap<>();
int x = scn.nextInt();
Node head = new Node(x);
Node curr = head;
while(x!=-1){
x = scn.nextInt();
if(x==-1){
break;
}
if(map.containsKey(x)){
Node temp = map.get(x);
curr.next = temp;
curr = temp;
}else{
Node nn = new Node(x);
map.put(x,nn);
curr.next = nn;
curr = nn;
}
}
return head;
}
public static void display(Node head){
Node curr = head;
while(curr != null){
System.out.print(curr.data+" ");
}
System.out.println();
}
public static void main(String []args){
Node l1 = buildList();
removeCycle(l1);
display(l1);
}
}