package Josephu;
import java.util.Scanner;
public class JosephuTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
CircleLinkList link = new CircleLinkList();
String str = scanner.next();
link.add(Integer.valueOf(str.split(",")[0]));
link.show();
link.josephuTest(Integer.valueOf(str.split(",")[1]));
}
}
class CircleLinkList{
LinkNode firstNode = new LinkNode(0);
public void add(int n) {
if (n < 1) {
System.out.println(" 1, ");
return;
}
LinkNode pre = firstNode;
LinkNode p = null;
for (int i = 1; i <= n; i++) {
LinkNode tmp = new LinkNode(i);
pre.next = tmp;
pre = pre.next;
p = tmp;
}
p.next = firstNode.next;
}
public void show() {
if (firstNode.next == null) {
System.out.println(" ");
return;
}
LinkNode tmp = firstNode.next;
int count = 0;
while (true) {
if (tmp == firstNode.next && count > 0) {
return;
}
System.out.println(" : " + tmp.num);
count++;
tmp = tmp.next;
}
}
public void josephuTest(int m) {
LinkNode tmp = firstNode.next;
while (true) {
for (int i = 1; i <= m - 2; i++) {
tmp = tmp.next;
}
if (tmp.next == tmp) {
System.out.print(tmp.num);
return;
} else {
System.out.print(tmp.next.num + "->");
tmp.next = tmp.next.next;
tmp = tmp.next;
}
}
}
}
class LinkNode {
int num;
LinkNode next;
LinkNode(int num) {
this.num = num;
}
}