/* Problem 2--M*A*S*H
   This was emulated simply by building a circular linked list and just
   counting off and deleting elements from the linked list when
   appropriate. */

#include <stdio.h>
#include <stdlib.h>

typedef struct CLL { /* The circular linked list data structure */

  int i;
  struct CLL *next;
} CLL;

FILE *in, *out;

int main (int argc, char **argv);

int main (int argc, char **argv) {

  int l, i, j, a;
  CLL *head, *P, *T;

 in = fopen ("prob2.in","r");
 out = fopen ("prob2.out","w");
 while (fscanf (in,"%d",&l),l>0) {
  head = malloc (sizeof (CLL)); /* Build the initial CLL */
  head->i = 1;
  P = head;
  for (i = 2; i <= l; i++) {
   P->next = malloc (sizeof (CLL));
   P = P->next;
   P->i = i;
  }
  P->next = head;
  for (i = 0; i < l-1; i++) { /* Go through the cards one by one */
   fscanf (in,"%d",&a);       /* Count through the CLL */
   for (j = 0; j < a-1; j++) P=P->next;
   T = P->next;   /* Delete the appropriate element. */
   P->next = T->next;
   free (T);
  }  /* Print the only one left. */
  fprintf (out,"Klinger should be in position #%d.\n",P->i);
  free (P);
 }
 fclose (in);
 fclose (out);
 return EXIT_SUCCESS;
}
