import java.util.;
import java.util.;
public class LinkedList {
private class node {
int data;
node next;
public int size;
}
private node head;
private node tail;
private int size;
public void display() {
node temp = this.head;
while (temp != null) {
System.out.print(temp.data + ", ");
temp = temp.next;
}
}
public void addlast(int item) {
node nn = new node();
nn.data = item;
nn.next = null;
if (this.size >= 1) {
this.tail.next = nn;
}
if (this.size == 0) {
this.head = nn;
this.tail = nn;
this.size++;
} else {
this.tail = nn;
this.size++;
}
}
public void addfirst(int item) {
node nn = new node();
nn.data = item;
nn.next = null;
if (this.size >= 1) {
nn.next = head;
}
if (this.size == 0) {
this.head = nn;
this.tail = nn;
this.size++;
} else {
this.head = nn;
this.size++;
}
}
public int getfirst() throws Exception {
if (this.size == 0) {
throw new Exception("ll is empty");
}
return this.head.data;
}
public int getlast() throws Exception {
if (this.size == 0) {
throw new Exception("ll is empty");
}
return this.tail.data;
}
public int getat(int idx) throws Exception {
if (this.size == 0) {
throw new Exception("ll is empty");
}
if (idx == 0 || idx >= this.size) {
throw new Exception("Invalid index");
}
node temp = this.head;
for (int i = 1; i <= idx; i++) {
temp = temp.next;
}
return temp.data;
}
private node getnodeat(int idx) throws Exception {
if (this.size == 0) {
throw new Exception("ll is empty");
}
if (idx < 0 || idx >= this.size) {
throw new Exception("Invalid index");
}
node temp = this.head;
for (int i = 1; i <= idx; i++) {
temp = temp.next;
}
return temp;
}
public void addAt(int item, int idx) throws Exception {
if (idx < 0 || idx > size) {
throw new Exception("Invalid index");
}
if (idx == 0) {
addfirst(item);
}
if (idx == this.size) {
addfirst(item);
} else {
// create a new node
node nn = new node();
nn.data = item;
nn.next = null;
// attach
node nm1 = getnodeat(idx - 1);
node np1 = nm1.next;
nm1.next = nn;
nn.next = np1;
// summary update
this.size++;
}
}
public int removefirst() throws Exception {
if (this.size == 0) {
throw new Exception("ll is empty");
}
int rv = this.head.data;
if (this.size == 1) {
this.head = null;
this.tail = null;
this.size--;
} else {
this.head = this.head.next;
this.size--;
}
return rv;
}
//complexity =o(n)
public int removelast() throws Exception {
if (this.size == 0) {
throw new Exception("ll is empty");
}
int rv = this.tail.data;
if (this.size == 1) {
this.head = null;
this.tail = null;
this.size=0;
}
else {
node sizem1=getnodeat(this.size-2);
this.tail=sizem1;
this.tail.next=null;
this.size--;
}
return rv;
}
public int removeAt(int idx) throws Exception {
if (this.size == 0) {
throw new Exception("ll is empty");
}
if (idx == 0 || idx >= this.size) {
throw new Exception("Invalid index");
}
if(idx==0) {
return removefirst();
}
if(idx==this.size) {
return removelast();
}
else {
node nm1=getnodeat(idx-1);
node n=nm1.next;
node np1=n.next;
nm1.next=np1;
this.size--;
return n.data;
}
}
public static void main(String args[]) throws Exception{
Scanner scn=new Scanner(System.in);
LinkedList ll=new LinkedList();
for (int i=0; ;i++) {
int term=scn.nextInt();
if(term==-1) {
ll.addLast(term);
break;
}
else {
ll.addLast(term);
}
}
circularLL(ll);
}
public static void circularLL(LinkedList ll) throws Exception {
node one =ll.head;
node two=one.next;
while(one !=null && two!=null) {
if (one.data==two.data) {
while(two !=null) {
ll.removeAt(two.data);
two=two.next;
}
ll.display();
}
else {
one=one.next;
two=two.next;
}
}
}
}