#include <bits/stdc++.h>
using namespace std;
typedef struct Node{
int data;
Node *next;
}Node,*LinkList;
void init(LinkList &L){
L=(Node *)malloc(sizeof(Node));
L->next=L;
}
void tailCreate(LinkList &L){
Node *s,*r=L;
int n;cin>>n;
for(int i=1;i<=n;++i){
s=(Node *)malloc(sizeof(Node));
s->data=i;
r->next=s;
r=s;
}
r->next=L->next;
}
int main() {
LinkList L;
init(L);
tailCreate(L);
Node *p=L->next;
int cnt=1;
while(1){
p=p->next;
cnt++;
if(p->next==p){
cout<<p->data;
return 0;
}
if((cnt+1)%3==0){
Node *q=p->next;
p->next=q->next;
free(q);
cnt=1;
p=p->next;
}
}
return 0;
}