#include using namespace std; #define ALL(obj) (obj).begin(),(obj).end() #define RALL(obj) (obj).rbegin(),(obj).rend() #define REP(i, n) for(int i=0;i<(n);++i) #define RANGE(i, a, b) for(int i=(a);i<(b);++i) #define RREP(i, n) for(int i=(n)-1;i>= 0;--i) #define endl '\n' #define pb push_back #define eb emplace_back #define pque priority_queue #define umap unordered_map #define uset unordered_set #define BIG 2000000000 #define VERYBIG 1000000000000000ll #define PI 3.141592653589793238 #define dcout cout< inline T GCD(T a,T b){T c;while(b!=0){c=a%b;a=b;b=c;}return a;} template inline T LCM(T a,T b){T c=GCD(a,b);a/=c;return a*b;} template inline T nCr(T a,T b){T i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;} template inline T nHr(T a,T b){return nCr(a+b-1,b);} template inline bool chmax(T& a,T b){if(a inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} typedef pair INTP; typedef vector INTV; struct edge2 {int to, cost;}; struct edge3 {int from, to, cost;}; signed main(void){ cin.tie(0); ios::sync_with_stdio(false); int A, B; cin >> A >> B; if(GCD(A, B) != 1){ cout << -1 << endl; return 0; } if(A > B) swap(A, B); int ans = 0; for(int i = 1; i < B; i++){ int num = A * i; // cerr << num % B << endl; ans += (num-1) / B; } cout << ans << endl; }