#include <iostream>
using namespace std;
int func(int n,int m) {
int Q[100] ;
int tail = 0 ;
for (int i = 1; i <= n; i++ ) {
Q[ tail ] = i ;
tail = tail + 1;
}
int count = 0 ;
int head = -1 ;
while ( head+1 < tail ) {
count = count + 1 ;
head = head + 1 ;
if ( count == m ) {
count = 0 ;
Q[ tail ] = Q[ head ] ;
tail = tail + 1 ;
}
}
return Q[head] ;
}
int main( ) {
printf("%d", func(30, 4) ) ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwppbnQgZnVuYyhpbnQgbixpbnQgbSkgewogICAgaW50IFFbMTAwXSA7CiAgICBpbnQgdGFpbCA9IDAgOwogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrICkgewogICAgICAgIFFbIHRhaWwgXSA9IGkgOwogICAgICAgIHRhaWwgPSB0YWlsICsgMTsKICAgIH0KICAgIGludCBjb3VudCA9IDAgOwogICAgaW50IGhlYWQgPSAtMSA7CiAgICB3aGlsZSAoIGhlYWQrMSA8IHRhaWwgKSB7CiAgICAgICAgY291bnQgPSBjb3VudCArIDEgOwogICAgICAgIGhlYWQgPSBoZWFkICsgMSA7CiAgICAgICAgaWYgKCBjb3VudCA9PSBtICkgewogICAgICAgICAgICBjb3VudCA9IDAgOwogICAgICAgICAgICBRWyB0YWlsIF0gPSBRWyBoZWFkIF0gOwogICAgICAgICAgICB0YWlsID0gdGFpbCArIDEgOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiBRW2hlYWRdIDsKfQoKaW50IG1haW4oICkgewogICAgcHJpbnRmKCIlZCIsIGZ1bmMoMzAsIDQpICkgOwp9Cg==