#include <bits/stdc++.h>
int N = 0;
int X = 0;
int main() {
  scanf("%d", &N);
  scanf("%d", &X);
  long int temp = X;
  long int answer = 0;
  int t = 0;
  do {
    answer += temp;
    temp = N - temp;
    t++;
  } while ((answer % N == 0 && t % 3 == 0) == 0);
  printf("%d\n", answer);
  return 0;
}
