#include<iostream>
using namespace std;

int main(){
  int gcd(int a,int b){
    if(a%b==0) return(b);
    else return(gcd(b,a%b));
  }
  
  int N,K;
  cin>>N>>K;
  cout<<((N%gcd(N,K))*3-3)*gcd(N,K)<<endl;
  
}