Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value.However, all Mike has is lots of identical resistors with unit resistance R0 = 1. Elements with other resistance can be constructed from these resistors. In this problem, we will consider the following as elements:   one resistor;  an element and one resistor plugged in sequence;  an element and one resistor plugged in parallel.   With the consecutive connection the resistance of the new element equals R = Re + R0. With the parallel connection the resistance of the new element equals . In this case Re equals the resistance of the element being connected.Mike needs to assemble an element with a resistance equal to the fraction . Determine the smallest possible number of resistors he needs to make such an element.

The single input line contains two space-separated integers a and b (1 ≤ a, b ≤ 1018). It is guaranteed that the fraction  is irreducible. It is guaranteed that a solution always exists.

Print a single number — the answer to the problem. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams or the %I64d specifier.

NoteIn the first sample, one resistor is enough.In the second sample one can connect the resistors in parallel, take the resulting element and connect it to a third resistor consecutively. Then, we get an element with resistance . We cannot make this element using two resistors.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll oo=1000000000;
vector<ll> v,v1;

int main() {
      ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
      ll a,b,k=0;
      cin>>a>>b;
      while(b)
      {k+=a/b;
      a=a%b;
      swap(a,b);

      }
      cout<<k<<endl;
    return 0;
}

#include <iostream>
#include <algorithm>

typedef long long ll;
using namespace std;

int main (){
  ll a,b;
  cin >> a >> b;
  if (b==1) {
    cout << a << endl;
    return 0;
  }
  ll ans=a/b;
  a=a%b;
  while (a!=1){
    ll c=b;
    b=a;
    a=c;
    ans+=a/b;
    a=a%b;
  }
  cout << ans+b << endl;
  return 0;
}

#include <iostream>

using namespace std;
long long a, b;
int main()
{
	cin >> a >> b;
	if (a == 1 || b == 1) {
		cout << a*b << endl;
		return 0;
	}
	long long count = 0;
	while (1)
	{
		if (a < b)
		{
			long long temp = a;
			a = b;
			b = temp;
		}
		if (a == 1 || b == 1) {
			count += a*b;
			break;
		}
		else
		{
			count += a / b;
			a = a%b;
		}
	}
	cout << count << endl;
	return 0;
}

#include<iostream>
using namespace std;
long long a,b,rez,sol;

long long cmmdc(long long a, long long b){
     long long r=1;
     while (r!=0) {
           r=a%b;
           a=b; b=r;
           }
     return(a);
}

int main(void) {
     cin>>a>>b;
     if (a==1) cout<<b;
     else
     while (a>1) {
      rez=a/b;
        if (rez==0) { long long aux=a; a=b; b=aux; }
         else {
              a-=rez*b; sol+=rez;
               if (a>1) {
                          long long aux=cmmdc(a,b);
                          a/=aux; b/=aux;
                          }
              if (a==0) { cout<<sol; return(0); }
               else if (a==1) { cout<<sol+b; return(0); }
               }
        }
     return(0);
}

#include <bits/stdc++.h>
using namespace std;

int main()
{
 long long a,b,ans=0;
    cin>>a>>b;
    while(a!=0&&b!=0)
    {
        if(a>b)
        {
            ans+=a/b;
            a%=b;
        }
        else
        {
          ans+=b/a;
            b%=a;
        }
    }
    cout<<ans<<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
	int a,b;
	cin>>a>>b;
	int res=0;
	while(a&&b)
	{
		if(a>b) res+=a/b,a%=b;
		else res+=b/a,b%=a;
	}
	cout<<res;
}

#pragma GCC optimize(2)
#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define rush() int Case = 0; int T; scanf("%d", &T);  while(T--)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define per(i, a, b) for(int i = a; i >= b; i --)
#define reps(i, a, b) for(int i = a; b; i ++)
#define clc(a, b) memset(a, b, sizeof(a))
#define Buff ios::sync_with_stdio(false); cin.tie(0);
#define readl(a) scanf("%lld", &a)
#define readd(a) scanf("%lf", &a)
#define readc(a) scanf("%c", &a)
#define reads(a) scanf("%s", a)
#define read(a) scanf("%d", &a)
#define lowbit(n) n & (-n)
#define pb push_back
#define sqr(x) x*x
#define rs x<<1|1
#define y second
#define ls x<<1
#define x first
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>PII;
const int mod = 1e9+7;
const double eps = 1e-6;
const int N = 1e6+7;
ll gcd(ll a, ll b)
{
    return b ? gcd(b, a%b) : a;
}
int main()
{
    ll a, b;
    cin >> a >> b;
    ll ans = 0;
    while(1)
    {
        a = a / gcd(a, b);
        b = b / gcd(a, b);
//        cout << a << " " << b << endl;
        if(a >= b)
        {
            ans += a / b;
            a = a % b;
        }
        else if(a < b)
        {
            ll k = b / a;
            b -= k * a;
            ans += k;
        }
        if(a == 0 || b == 0)
            break;
    }
    cout << ans << endl;
	return 0;
}

#include <iostream>
using namespace std;
long long count(long long a, long long b) {
	if (a == 0)
		return 0;
	if (a == b)
		return 1;
	if (a == 1)
		return b;
	long long r1, r2;
	if (a > b)
		return count(a % b, b) + a / b;
	long long c = b / a;
	long long d = b % a;
	long long cnt = 0;
	cnt += count(1, c);
	if (d > 0)
		cnt += count(a, d);
	return cnt;
}
int main() {
	long long a, b;
	cin >> a >> b;
	cout << count(a, b) << endl;
}

/*

    This Code and Template is my personal property,
    It's handcrafted by me (Siddharth Singh)

    You are not allowed to lift Macros/Functions or any thing
    without my permission.

    If you want to take any thing, let me know first.
    If i approve then only you can use it.

    I will mention everyone who copies without my permission

    List of uncreative/non-talented people copying macros/template

    ~ Updated 2nd June 2020

    1.
    2.
*/

#include <bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;

//methods: find_by_order(k); & order_of_key(k);
//To make it an ordered_multiset, use pairs of (value, time_of_insertion)
//to distinguish values which are similar

// Type Redefinitions
#define int long long
#define double long double
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define ADJ map<int,set<int>>
#define vpii vector<pii>
#define fmap gp_hash_table
#define LONG LLONG

// Shorthand Macros
#define pb push_back
#define ed end()
#define bg begin()
#define sz size()
#define ln length()
#define all(x) x.begin(),x.end()
#define endl "\n"
#define NL cout<<'\n';
#define F first
#define S second
#define disp(x) for(auto t : x )cout<<t<<" ";
#define for0(n) for(int i=0;i<n;i++)
#define for0j(n) for(int j=0;j<n;j++)
#define oldfor(v) for(auto it = v.begin(); it != v.end(); ++it)
#define SS <<" "<<
#define acum accumulate
#define arrin(a,n) vi a(n);for0(n){cin>>a[i];}
#define pf(x) cout<<x<<endl;
#define Check pf("Check")
#define gridin(num,n,m) for0(n){for0j(m) cin>>num[i][j];}
#define dispgrid(num,n,m) for0(n){for0j(m) cout<<num[i][j]<<" "; cout<<endl;}

// IO Helpers
#define tks(x) string x;cin >> x;
#define tk(a) int a;cin>>a;
#define tk2(a,b)tk(a)tk(b)
#define tk3(a,b,c)tk2(a,b)tk(c)
#define tk4(a,b,c,d)tk2(a,b)tk2(c,d)
#define tk5(a,b,c,d,e)tk4(a,b,c,d)tk(e)
#define tk6(a,b,c,d,e,f)tk3(a,b,c)tk3(d,e,f)
#define tk7(a,b,c,d,e,f,g)tk6(a,b,c,d,e,f)tk(g)
#define tk8(a,b,c,d,e,f,g,h)tk7(a,b,c,d,e,f,g)tk(h)
#define addEdge(adj) tk2(u,v) adj[u].insert(v);adj[v].insert(u);
#define test tk(test)while(test--)

// Common Affirmations
#define YES cout<<"YES"<<endl;
#define Yes cout<<"Yes"<<endl;
#define NO cout<<"NO"<<endl;
#define No cout<<"No"<<endl;
#define yes cout<<"yes"<<endl;
#define no cout<<"no"<<endl;

// Common Constants
const int MOD=1000000007;
const int N=100000;

// Overloaded Ostream Classes
template < typename T >
ostream &operator<<(ostream &os,const vector<T> &v){os<< "{";oldfor(v){if(it!=v.bg)os<<", ";os<<*it;}return os << "}";}

template<typename F, typename S>
ostream& operator<<(ostream& os,const pair<F,S> &p){return os << "(" << p.F << ", " << p.S << ")";}

template < typename T >
ostream &operator << (ostream &os, const set<T> &v){os << "[";oldfor(v){if(it!=v.bg)os<< ", ";os << *it;}return os << "]";}

template < typename T >
ostream &operator << (ostream &os, const multiset<T> &v){os << "[";oldfor(v){if(it!=v.bg) os << ", ";os<<*it;}return os << "]";}

template < typename F, typename S >
ostream &operator <<(ostream & os, const map<F,S> &v){os << "[";oldfor(v){if(it!=v.bg)os<< ", ";os<< it->F<<" = "<<it->S;}return os << "]";}

/*
    Common Theory :

    1. To find number of elements less than k in array a
       answer would be int res = upper_bound(all(a),k) - a.bg

    2. To find number of elements greater than k in array a
       answer would be int res = a.ed - upper_bound(all(a),k)

    3. Min Heap = priority_queue<int,vector<int>, greater<int>> que;
       Max Heap = priority_queue<int> que;

    4. Is Function Monotonus, Can you solve for a given res?
       Think about Binary Searching !

    5. Can't be sure if greedy is optimal? Constraints are low?
       Think about Dynamic Programming !

    6. Don't forget a grid is a graph.

    7. Watch out for function overhead

    8. Confused ? Think against these algos

       1. Prefix,Suffix
       2. Difference Array
       3. Nx32 Bit Frame
       4. DP ~ Can you create a state transition from dp[i][j] to dp[i-1][j] or dp[i][j+1]
       5. Sliding Window
       6. Hashing

    9. Guess from Solution's Expected Time Complexity

*/

int solve(int a,int b){
	if(a==1)return b;
	if(b==1)return a;
	if(a>b){
		return (a/b) + solve(a-(a/b)*b,b);
	}else{
		return (b/a) + solve(a,b-a*(b/a));
	}
}

int32_t main(){
	ios_base::sync_with_stdio(false);cin.tie(0);
	tk2(a,b)
	pf(solve(a,b))
	return 0;
}

									/*ba yade oo */
//Mehrdad AP

//#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <sstream>
#include <queue>
#include <vector>
#include <stack>
#include <set>
#include <map>

using namespace std;

#define absol(x) ((x)>(0) ? (x):(-1)*(x))
#define pow2(x) ((x)*(x))
#define EPS 1e-9
#define MAX 30000
#define MOD 1000000007
#define Left(x) (2*x)
#define Right(x) ((2*(x)+1)
#define mP make_pair
#define pB push_back
#define X first
#define Y second

//#define inRange(x,y) (x>=0 && x<N && y>=0 && y<M)

typedef long long int LL;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int,pii> piii;

const double PI = 2.0*acos(0.0);
const int INF = 1000*1000*1000;

#define assert(x) { cerr  << #x << ": "<< (x) << endl;}

LL solve(LL a,LL b)
{

	LL ret=0;
	if (a==b) return 1LL;
	if (b==1) return a;
	if (a==1) return b;
	if (a>b){
		ret = (a/b);
		return ret + solve(a%b,b);

	}else{
		return (b/a)+solve(a,b%a);
	}
}

int main ()
{
	//freopen("input.in",r,stdin);
	//freopen("output.out",w,stdout);
	//ios_base::sync_with_stdio(false);
	LL A,B;
	while ( cin >> A >> B){

		cout << solve(A,B) << endl;
	}
	return 0;
}

#include <iostream>
#include <string>
using namespace std;

int main()
{
    long long a = 0;
    long long b = 0;
    long long r = 0;

    cin >> a >> b;
    long long ans = 0;
    while (b != 0)
    {
        ans += a / b;
        r = a % b;
        a = b;
        b = r;
    }

    cout << ans << endl;
}

// The main idea behind this problem
//
// first, we have a/b (a > b, a % b = r), thus we need at least
// a/b resistors in sequence, thus left r/b resistance to be constructed
// we need two elements in parallel, i.e.
// 1 / ( (1 / RA) + (1 / RB) ) = r / b
// 1 / RA + 1 / RB = b / r  (where b > r)
// 1 / RA + 1 / RB = c + r2 / r
// so we need c in parallel to construct RA, so
// 1 / RB = r2 / r   OR RB = r / r2
// so return back to our orignal problem, construct a r / r2 (where r > r2 )
// what we need is
// a / b = c1 .... r
// b / r = c2 .... r2
// r / r2 is our new problem, c1 + c2 is our current resistors we need
//
// so
// Loop:
//      sum += a / b;
//      a % b = r;
//      a = b;
//      b = r
// This is typecially GCD Algorithm

#include <iostream>

using namespace std;
using ll = long long;

ll process(ll a, ll b) {
   ll rv = 0;

    while (b != 0) {
        ll c = a % b;
        rv += a / b;
        a = b;
        b = c;
    }

   return rv;
}

int main() {
    ll a, b;
    cin >> a >> b;

    cout << process(a, b) << endl;

    return 0;
}

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <math.h>
#include <time.h>

#define vi vector<int>
#define vpii vector< pair<int,int> >
#define pii pair<int,int>
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define sqr(x) ((x)*(x))
#define FOR(i,n) for (int i = 0; i < int(n); i++)
#define READ(v,n) { FOR(i,n){int x;cin>>x;v.pb(x);} }
#define FREAD(v,n) { FOR(i,n){int x;in>>x;v.pb(x);} }
#define WRITE(v) "";FOR(i,sz(v))cout<<v[i]<<" ";cout
#define FWRITE(v) "";FOR(i,sz(v))out<<v[i]<<" ";out
#define gmin(a,b) { if (b < a) a = b; }
#define gmax(a,b) { if (b > a) a = b; }
#define pb push_back
#define ppb pop_back
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int main(void){
  ll a,b;
  cin>>a>>b;
  ll p=0;
  while(true){
    if(a==b){p++;break;}
    if(a==1){p+=b;break;}
    if(b==1){p+=a;break;}
    if(a>b){ll x=a/b; p+=x;a-=x*b; if(a==0)break;}else
    if(b>a){ll x=b/a; p+=x;b-=x*a; if(b==0)break;}
  }
  cout<<p;
  end:

  return 0;
}

#include <bits/stdc++.h>

using namespace std;

long long int ans, a, b;

void solve()
{
    while(a > 0 && b > 0)
    {
        if(a > b)
            ans += a / b,
            a %= b;
        else
            ans += b / a,
            b %= a;
    }
}

int main()
{
    cin >> a >> b;

    solve();

    cout << ans << endl;

    return 0;
}

#include <stdio.h>
#include <iostream>
using namespace std;
typedef __int64 LL;

int main()
{
    LL a, b;
    scanf("%I64d%I64d",&a,&b);
    LL cnt = 0;
    cnt += a / b;
    a -= b*cnt;
    while(b - a != a && a!=0)
    {
        LL s = (b-a)/a;
        cnt += s;
        cnt+=1;
        LL t = a;
        a = (b-a)-s*a;
        b = t;
    }
    if(a!=0)
        cnt += 2*a;
    printf("%I64d\n",cnt);

}

#include <iostream>
#include <cstring>
#include <cstdio>
#include <ctime>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <utility>
#include <bitset>
#include <stdio.h>
#include <iomanip>
#include <climits>
#include <math.h>
#include <functional>
#define FOR(i,n) for(int i=0; i<n ; i++)
#define pi 3.14159265358979323846
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double EPS = 1e-9;
const int MOD = 1000 * 1000 * 1000 + 7;
const int INF = 2000 * 1000 * 1000;
using namespace std;

int popcount(int n) { bitset<32> b(n); return b.count(); }
long long gcd(long long a, long long b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll solve(ll a,ll b)
{
    ll ret = 0;
    if (a == 0) return 0;
    if (a < b) swap(a, b);
    ret = a / b + solve(a%b, b);
    return ret;
}

int main()
{
    ll a, b; cin >> a >> b;
    cout << solve(a,b) << endl;
    return 0;
}

//By the Name of Allah
//Written By Mo'men AbdelRazek
#include <bits/stdc++.h>
#include <ext/numeric>
using namespace std;
using namespace __gnu_cxx;

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    #endif

	unsigned long long int x,y,xx,yy;
	//cin>>xx>>yy;
	while(cin>>xx>>yy)
	{
		x=max(xx,yy);y=min(xx,yy);
		unsigned long long int ans=0;
		while(y!=1&&x!=y)
		{
			//cout<<x<<" "<<y<<endl;
			ans+=x/y;
			if(x%y==1)break;
			unsigned long long int temp=x;
			x=y;

			y=temp%y;
		}
		if(y!=1)ans+=y;else ans+=x;
		cout<<ans<<endl;
	}
	return 0;
}

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <iostream>
#include <stack>
using namespace std;

int main()
{
    long long a, b;
    cin >> a >> b;

    long long ans = 0;
    while(b > 0)
    {
        ans += a / b;
        a %= b;
        swap(a, b);
    }

    cout << ans << endl;

    return 0;
}

#include <iostream>
#include <time.h>
using namespace std;
long long a,b,res;
int main() {
	cin >> a >> b;
	//cout << b-1+a/b+((a!=b && b!=1) ? 1 : 0);
	if (a==b) res++;
	while (a!=b) {
		res+=max(a,b)/min(a,b);
		if (a==1 || b==1) break;
		if (a>b) a%=b; else b%=a;
	}
	cout << res;
	return 0;
}

/*#include <bits/stdc++.h>
#include <stdint.h>
using namespace std;
#define ll  long long int
#define ss second
#define ff first
ll mod= 1000000000;

int main()
{  int n, k;
   cin>>n>>k;
   vector <int> a(n+1), b(n+1);
   for(int i=1; i<=n; i++)
    { cin>>a[i]; b[i]=a[i];
	}
     int i=1, j=1+k;
     while(j<=n&&b[i]==b[j])
     {
     	i++; j++;
	 }
	 if(j==n)
	 {for(int i=1; i<=n; i++)
	  cout<<b[i];
	  return 0;
	 }
	 if(b[i]>b[j])
	 {  j=1+k;
	 	while(j<=n)
	 	{
	 	 b[j]=b[i];
	 	 j++; i++;
		 }
		 for(int i=1; i<=n; i++)
	  cout<<b[i];
	  return 0;
	 }
	 else
	 {  int f=0;
	 	for(int h=k; h>=0; h--)
	 	{ if(b[h]!=9)
	 	  {b[h]++;
	 	   break;
		   }
		 }
		 if(f)
		 { int j=k+1;
		 	while(j<=n)
	 	{
	 	 b[j]=b[i];
	 	 j++; i++;
		 }
		 for(int i=1; i<=n; i++)
	  cout<<b[i];
	  return 0;
		 }
		 else
		 {   vector <int> c(n+2, 0);
		    c[1]=1; c[1+k]=1;
		    for(int i=1; i<=n+1; i++)
		    cout<<c[i];
		    return 0;

		 }
	 }
	return 0;
}*/
#include <bits/stdc++.h>
#include <stdint.h>
using namespace std;
#define ll  long long
#define ss second
#define ff first
ll mod= 1000000000+7;
ll c;
ll gcd(ll a, ll b)
{  // cout<<"A "<<i<<' '<<A<<" "<<B<<" B\n";
	if(a==1||b==1)
	{ c+=(a+b-1);
	  return 0;
	}
	/*if(a==0)
	{
	  return b;
	}
	if(b==0)
	{

	  return a;
	}
*/
	c+=(a/b);
	return gcd(b,a%b);
}

int main()
{
    ll A, B;
	cin>>A>>B;
	c=0;
	ll a=max(A, B), b=min(A, B);
	ll gcd_=gcd(a,b);

	cout<<c;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define fast ios_base::sync_with_stdio(false);cin.tie(0)
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define snd sd.ft
#define td sd.sd
#define debug(x) cout<<"### x is: "<<x<<"###"<<endl
#define all(a) a.begin(),a.end()
#define tc int t;scanf("%d",&t);while (t-->0)

#define ninf -1000000000
#define pinf 1000000000

typedef unsigned long long ll;
const ll MOD= 1e9+7;
typedef vector<pair<ll,ll> > vecpair;
typedef vector<pair<ll, pair<ll,ll> > > vecpair3;

ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll invmod(ll x) {return power(x,MOD-2);}

ll cell(ll a, ll b)
{
	return ceil((long double)a / (long double)b);
}

int main()
{

    //freopen("TASK.in","r",stdin);freopen("TASK.out","w",stdout);
	ll a,b;

	cin>>a>>b;

	ll ans=0,sm=0;

	while(a!=1)
	{
		if(a>b)
		sm = cell(a,b)-1 , a -= (b*sm);
		else
		sm = cell(b,a)-1 , b -= (a*sm);

		//debug(sm);
		ans += sm;
	}

	ans += b;

	cout<< ans;
	return 0;
}

#include <iostream>

using namespace std;

typedef long long lld;

lld Solve(lld a, lld b) {
	lld ans = 0;

	while (a != 0 && b != 0) {
		ans += a/b;
		a %= b;
		swap(a, b);
	}

	return ans;
}

int main() {
	lld a;
	lld b;
	cin >> a >> b;

	cout << Solve(a, b) << endl;

	return 0;
}

#include <fstream>
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <list>
#include <queue>
#include <string>
#include <cmath>
#include <map>
#include <stack>

using namespace std;
typedef long long ll;
const ll mod = 1000000007, INF = 2000000001;
int dx[] = {0,1,0,-1, 1,1,-1,-1, 0}, dy[] = {-1,0,1,0, -1,1,1,-1, 0};
const double EPS = 1e-9;

ll gcd(ll a, ll b)
{
    return (b != 0) ? gcd(b, a % b) : a;
}

ll modpow(ll a, ll x, ll m = mod)
{
	int p = 1;
	while (x > 0)
	{
		if (x & 1 == 1)
		{
			p = p * a % m;
			--x;
		}
		else
		{
			x >>= 1;
			a = a * a % m;
		}
	}
	return p;
}

void z(const string & s, vector<int> & v)
{
	int n = s.length();
	for (int i = 1, l = 0, r = 0; i < n; ++i)
	{
		if (i <= r)
			v[i] = min(v[i-l],r-i+1);
		while (i + v[i] < n && s[v[i]] == s[i+v[i]])
			++v[i];
		if (i + v[i] - 1 > r)
			r = (l = i) + v[i] - 1;
	}
}

bool dfs(vector< vector<string> > & states, int sx, int sy, int n)
{
	bool ans = false;
	if (n == 8)
		ans = true;
	else if (states[n][sy][sx] == 'S')
		ans = false;
	else
		for (int d = 0; d < 9 && !ans; ++d)
		{
			int x = sx + dx[d], y = sy + dy[d];
			if (min(x,y) >= 0 && max(x,y) < 8)
			{
				if (states[n][y][x] == '.')
				{
					ans = dfs(states,x,y,n+1);
				}
			}
		}
	return ans;
}

ll g(ll a, ll b)
{
	return (a != 0) ? b / a + g(b%a,a) : a;
}

void _a()
{
	ll a, b;
	cin >> a >> b;
	/*ll ans;
	if (a == 0)
		ans = 0;
	else if (b == 1)
		ans = a;
	else
	{
		if (a > b)
			ans = g(b,a%b) + a / b;
		else
			ans = g(b,a);
	}
	cout << ans;*/
	cout << g (a,b);
}

void _b()
{
	string s;
	cin >> s;
	vector<char> v(100000,'?');
	int i = 0;
	int l = 0;
	while (l < s.length())
	{
		int r = l;
		for (r = l; r < s.length() && s[r+1] == s[l]; ++r)
			;
		if ((r - l + 1) % 2 == 1)
		{
			v[i] = s[r];
			if (i > 0 && v[i] == v[i-1])
				--i;
			else
				++i;
		}
		l = r + 1;
	}
	cout << (i == 0 ? "Yes" : "No");
}

void _c()
{
	string s, a = "No such line.";
	ll k, n;
	cin >> s >> k;
	n = s.length();
	if (k <= n * (n + 1) / 2)
	{
		a = "";
		map< char, set<int> > m;
		for (int i = 0; i < s.length(); ++i)
			m[s[i]].insert(i+1);
		int cnt = 0;
		while (cnt != k)
		{
			char ch = '?';
			for (char c = 'a'; c <= 'z'; ++c)
			{
				int d = 0;
				for (set<int>::iterator it = m[c].begin(); it != m[c].end(); ++it)
					d += (n - *it + 1);
				if (cnt + d >= k)
				{
					a += c;
					ch = c;
					if (cnt + m[c].size() < k)
						cnt += m[c].size();
					else
						cnt = k;
					break;
				}
				else
					cnt += d;
			}
			if (cnt == k)
				break;
			for (char c = 'a'; c <= 'z'; ++c)
				if (c != ch)
					m[c] = set<int>();
			set<int> ss;
			for (set<int>::iterator it = m[ch].begin(); it != m[ch].end(); ++it)
				if (*it < n)
				{
					if (s[*it] != ch)
						m[s[*it]].insert(*it+1);
					else
						ss.insert(*it+1);
				}
			m[ch] = ss;
		}
	}
	cout << a;
}

void _d()
{
	int n;
	cin >> n;
	vector<int> a(n);
	set< pair<int,int> > s;
	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
		set< pair<int,int> >::iterator it = s.lower_bound(make_pair(a[i],0));
		if (it != s.end() && it->first == a[i])
		{
			s.insert(make_pair(a[i],it->second+1));
			s.erase(it);
		}
		else
			s.insert(make_pair(a[i],1));
	}
	bool ans = true;
	sort(a.begin(),a.end());
	for (int i = 1; i < n && ans; ++i)
		ans = (a[i] - a[i-1] <= 1);
	if (ans)
	{
		set< pair<int,int> >::iterator fit = s.begin(), sit = fit;
		++sit;
		for ( ; sit != s.end() && ans; )
		{
			int d = sit->second - fit->second;
			if (d > 0)
			{
				pair<int,int> p = make_pair(sit->first,d);
				s.erase(fit);
				s.erase(sit);
				s.insert(p);
				fit = s.find(p);
				sit = fit;
				++sit;
			}
			else if (d == 0)
			{
				if (++sit != s.end())
					ans = false;
				else
					--sit;
				break;
			}
			else
				ans = false;
		}
		if (fit != s.end() && sit == s.end())
			ans = false;
	}
	cout << (ans ? "YES" : "NO");
}

void build(vector< vector< pair<int,ll> > > & t, const vector<ll> & a, int v, int tl, int tr)
{
	if (tl == tr)
	{
		for (int i = 0; i < 20; ++i)
			t[i][v].second = (a[tl] & (1 << i)) >> i;
	}
	else
	{
		int tm = (tl + tr) / 2;
		build(t,a,2*v,tl,tm);
		build(t,a,2*v+1,tm+1,tr);
		for (int i = 0; i < 20; ++i)
			t[i][v].second = t[i][2*v].second + t[i][2*v+1].second;
	}
}

void push(vector< vector< pair<int,ll> > > & t, int v, int tl, int tr)
{
	for (int i = 0; i < 20; ++i)
		if (t[i][v].first == 1)
		{
			t[i][2*v].first = (t[i][2*v].first + 1) % 2;
			t[i][2*v+1].first = (t[i][2*v+1].first + 1) % 2;
			t[i][v].first = 0;
			ll tm = (tl + tr) / 2;
			t[i][2*v].second = tm - tl + 1 - t[i][2*v].second;
			t[i][2*v+1].second = tr - tm - t[i][2*v+1].second;
		}
}

ll get_sum(vector< vector< pair<int,ll> > > & t, int v, int tl, int tr, int l, int r)
{
	ll a = 0;
	if (l <= r)
	{
		if (tl == l && tr == r)
		{
			for (int i = 0; i < 20; ++i)
				a += t[i][v].second * (1 << i);
		}
		else
		{
			push(t,v,tl,tr);
			int tm = (tl + tr) / 2;
			a = get_sum(t,2*v,tl,tm,l,min(r,tm)) + get_sum(t,2*v+1,tm+1,tr,max(l,tm+1),r);
		}
	}
	return a;
}

void update(vector< vector< pair<int,ll> > > & t, int v, int tl, int tr, int l, int r, ll x)
{
	if (l <= r)
	{
		if (tl == l && tr == r)
		{
			//push(t,v,tl,tr);
			for (int i = 0; i < 20; ++i)
			{
				t[i][v].first = (t[i][v].first + 1) % 2; //(x & (1 << i)) >> i;//,x^t[v].second);
				if (t[i][v].first == 1)
					t[i][v].second = tr - tl + 1 - t[i][v].second;
			}
		}
		else
		{
			push(t,v,tl,tr);
			int tm = (tl + tr) / 2;
			update(t,2*v,tl,tm,l,min(r,tm),x);
			update(t,2*v+1,tm+1,tr,max(l,tm+1),r,x);
			for (int i = 0; i < 20; ++i)
				t[i][v].second = t[i][2*v].second + t[i][2*v+1].second;
		}
	}
}

void _e()
{
	int n;
	scanf("%d",&n);
	vector<ll> a(n);
	for (int i = 0; i < n; ++i)
		scanf("%I64d",&a[i]);
	vector< vector< pair<int,ll> > > tree(20,vector< pair<int,ll> >(5*n,make_pair(0,0)));
	build(tree,a,1,0,n-1);
	int m;
	scanf("%d",&m);
	while (m--)
	{
		int t;
		scanf("%d",&t);
		if (t == 1)
		{
			int l, r;
			scanf("%d %d",&l,&r);
			/*ll s = 0;
			for (int i = l - 1; i < r; ++i)
				s += a[i];
			printf("%I64d\n",s);*/
			printf("%I64d\n",get_sum(tree,1,0,n-1,l-1,r-1));
		}
		else if (t == 2)
		{
			int l, r, x;
			scanf("%d %d %d",&l,&r,&x);
			/*for (int i = l - 1; i < r; ++i)
				a[i] ^= x;*/
			update(tree,1,0,n-1,l-1,r-1,x);
		}
	}
}

ll gcdex(ll a, ll b, ll & x, ll & y)
{
	if (a == 0)
	{
		x = 0;
		y = 1;
		return b;
	}
	else
	{
		ll x1, y1;
		ll g = gcdex(b%a,a,x1,y1);
		x = y1 - b / a * x1;
		y = x1;
		return g;
	}
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif
	_a();
	//_b();
	//_c();
	//_d();
	//_e();
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main(){
    long long t,a,b,cnt;
    cin>>a>>b;
    cnt=0;
    while(b){
        cnt+=a/b;
        t=a%b;
        a=b;
        b=t;
    }
    cout<<cnt<<endl;
    return 0;
}

#include<stdio.h>
#define ll long long
ll cnt=0;
ll a,b;
main(){
    scanf("%I64d %I64d",&a,&b);
    ll tmo;
    if(b>a){
        tmo=a;
        a=b;
        b=tmo;
    }
    while(a%b!=0){
        cnt+=(a/b);
        ll r=a%b;
        a=b;
        b=r;
    }
    cnt+=(a/b);
    printf("%I64d",cnt);
}

#include <cstdio>
#include <queue>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
const long long inf = 1e18;
const int MAXN = 20+5;

int main()
{
    long long a,b,ans=0;
    scanf("%I64d%I64d",&a,&b);
    while(a > 0 && b > 0)
    {
        if(a > b)
        {
            ans += a/b;
            a%=b;
        }
        else
        {
            ans += b/a;
            b%=a;
        }
    }
    cout << ans <<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#include<cstdio>
typedef long long ll;
main()
{
	ll a,b,s=0,i=0;
	cin>>a>>b;

	if(b==1)
	cout<<a,exit(0);
	ll k=a/b;

	a=a-k*b;
	ll x=a,y=b,z;
	while(x!=1)
	{
		if(x>y)
		{
			z=x/y;
			x=x-z*y;
			a=x;
			k+=z;
		}
		else{

		i++;
		x=b-a;
		y=a;
		a=x;
		b=y;
	}

	}
	cout<<k+i+b;

}

#include<bits/stdc++.h>
using namespace std;
#define f(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#include<bitset>
typedef long long ll;
const ll mod = 1e9 + 7;

void input(){
  ll a, b; cin >> a >> b;
  ll ans = 0LL;
  while(b > 0){
    ll q = a / b;
    ll r = a % b;
    ans += q;
    a = b;
    b = r;
  }
  cout << ans << endl;
}
int main(){
    input();
    return 0;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;

ll ans(ll p,ll q)
{
    long long ret;
    if(p==1)
        return q;
    ret=p/q;
    p=p%q;
    ret+=ans(q,p);
    return ret;

}
int main()
{
    ll a,b;
    cin>>a>>b;
    ll m =ans(a,b);
    cout<<m;
}

#include <cstdio>
#include <iostream>
using namespace std;

long long  func(long long a, long long b)
{
	if (a == 0)
		return 0;
	return b /a + func(b%a, a);
}

int main()
{
	long long a,b;

	cin >> a >> b;

	if (a > b )
		cout << func(b, a) << endl;
        else
		cout << func(a, b) << endl;

	return 0;
}

#include <iostream>
#include <cmath>

using namespace std;

int main () {
    long long a, b, ans = 0;
    cin >> a >> b;
    while (min(a,b)) {
        if (a >= b) {
            ans += a / b;
            a %= b;
        } else {
            ans += b / a;
            b %= a;
        }
    }

    cout << ans;

    //system("pause");
    return 0;
}

#pragma GCC optimize ("O3")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
    ll n, m, ans = 0;
    cin >> n >> m;
    if (n % m == 0)
        ans = n / m, m = n = 0;
    while (m > 1)
        if (n > m)
            ans += n / m, n %= m;
        else
            ans += m / n, m %= n;
    if (n == 1)
        n--;
    cout << ans + n;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
#define ll long long int
//#define d double
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix std::fixed<<setprecision(10)
const int M=1e9+7;
#define f(n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define cl(n,m) (ceil(double(n)/double(m)))
#define T() int t; cin>>t; while(t--)
#define pi pair<ll,ll>
const int MOD=998244353;
//const int MOD=1e9+7;
#define endl "\n"
const double PI = 3.141592653589793238460;
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
//bitset<N>bs;
//bitset<512>bb[512];
//vector<vector<int>>arr;

ll power(ll a,ll b)
{
    ll res=1;
    if(a==0)return 0;
    if(a==1)return 1;
    for(;b>0;b>>=1)
    {
        if(b&1)res=(res*a);
        if(res>MOD)res%=MOD;
        a=(a*a);
        if(a>MOD)a=a%MOD;
    }
    return res;
}

bool isPrime[1000001];
vector<ll>prime;

void seive(int limit)
{
    isPrime[0]=isPrime[1]=1;

    for(int i=2;i*i<=limit;i++)
    {
        if(!isPrime[i])
        for(int j=i*i;j<=limit;j+=i)
        {
            isPrime[j]=1;
        }
    }
    for(int i=2;i<=limit;i++)if(!isPrime[i])prime.pb(i);
}

ll ans=0;

ll solve(ll a,ll b)
{
if(b==0)return 0;
return(solve(b,a%b)+a/b);
}

int main()
{
    ll a,b;
    cin>>a>>b;

    cout<<solve(a,b);

}

#include<bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int long long int

using namespace std;

int func(int a, int b)
{
    if(a==0 or b==0)
        return 0;

    if(a>=b)
        return a/b + func(a%b,b);

    return b/a + func(a,b%a);
}

void solve()
{int a,b;
    cin>>a>>b;

    cout<<func(a,b)<<endl;

}

signed main()

{
    FAST_IO;
    int t = 1;

    // cin>>t;

    while(t--)
    {
        solve();
    }

    return 0;
}

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<pii>
#define vl vector<ll>
#define vll vector<pll>
#define f(i,l,r) for(ll i=l;i<r;i++)
#define d(i,n,m) for(ll i=n;i>=m;i--)
#define P (ll)(1e9+7)
#define PI 3.14159265358979323846
#define mod 998244353
#define pb push_back
#define F first
#define S second
#define IP ios_base::sync_with_stdio(false);cin.tie(NULL);
#define fixed(n) cout<<std::fixed<<std::setprecision(n) //use for n digits precision of decimal
using namespace std;
int offset[]={1,0,-1,0,1};
const int MXN=1e5+1;
ll calc(ll a,ll b)
{
    if(b==1)
        return a;
    if(a==1)
        return b;
    if(a+1==b)
        return b;
    ll ans=0;
    ans+=(a/b);
    a=a%b;
    ans+=calc(b,a);
    return ans;
}
int main()
{
    IP
    ll a,b;
    cin>>a>>b;
    cout<<calc(a,b)<<"\n";
    return 0;
}

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <ctype.h>

using namespace std;

long long result;
void get(long long a,long long b)
{
    if(b==0) return;
    result=result+(a/b);
    get(b,a%b);
}

int main()
{
    long long a,b;
    scanf("%I64d%I64d",&a,&b);
    get(a,b);
    long long ans=result;
    printf("%I64d",ans);
    return 0;
}

// created by Marchecnko Vadim [midav7]
// I did it just for fun =)
// v 0.92

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <climits>
#include <queue>
#include <set>

#define foread freopen("input.txt","r",stdin)
#define fowrite freopen("output.txt","w",stdout)
#define pi 3.141592
#define sqr(a) ( (a)*(a))
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
typedef long long ll;
typedef double D;

using namespace std;

int main(){
ll a,b,ans=0;
cin >> a >> b;
while (a>0 && b>0){
  if (a>=b) {
    ans+=a/b;
    a%=b;
    } else swap(a,b);
}
cout << ans;
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<stdlib.h>
#include<sstream>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<fstream>
#include<vector>
#include<algorithm>
#include <queue>
#include<string>
#include<cstring>
#include <deque>
#include<stack>
using namespace std;

#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define PB push_back
#define PI acos(-1.0)
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
long long GCDFast(long long a, long long b){ while (b)b ^= a ^= b ^= a %= b;  return a; }
#define SET(a) memset(a,-1,sizeof(a))
#define ALL_BITS ((1 << 31) - 1)
#define NEG_BITS(mask) (mask ^= ALL_BITS)
#define TEST_BIT(mask, i) (mask & (1 << i))
#define ON_BIT(mask, i) (mask |= (1 << i))
#define OFF_BIT(mask, i) (mask &= NEG_BITS(1 << i))
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define MOD 1000000007
#define MX 100010
#define pii pair<int,int>
#define LL long long
// UP, RIGHT, DOWN, LEFT, UPPER-RIGHT, LOWER-RIGHT, LOWER-LEFT, UPPER-LEFT
int dx[8] = { -1, 0, 1, 0, -1, 1, 1, -1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };

#define mx 100005

int main() {
	//freopen ("d:/Codejam/B-small-attempt0.in","r",stdin);
	//freopen ("d:/Codejam/output1.txt","w",stdout);
	LL n, i, j, k, temp, m;
	LL x, y, z, sum, test_case = 0, length, ans, test = 0, l, w, num,t,index;
	string str, str1;

	ios::sync_with_stdio(false);
	LL a, b;

	while (cin >> a >> b){
		sum = 0;
		while (a != 0 && b != 0){
			if(a > b){
				sum += a / b;
				a %= b;
			}
			else{
				sum += b / a;
				b%= a;
			}
		}
		if (a == 1 && b == 1)
			sum++;
		cout << sum << "\n";
	}

	return 0;
}

#include <bits/stdc++.h>

#define lp(i,n) for(int i=0; i<n; i++)
#define ll long long
#define pb push_back
#define  mp make_pair
#define pii pair<int,int>
#define ff first
#define ss second
#define nl "\n"

#define EPS 1e-8
#define OO 100000000

#define on(i,n) i=i|(1<<n)
#define off(i,n) i=i& (~(1<<n))

using namespace std;

vector<pair<int,int>> items;

    ll a,b;
ll m(ll x , ll y){

    if(x==0 ||y==0) return 0;

    ll t;
    if(y>x){
        t=y/x;
        return t+m(y-x*t,x);

    }
    t=x/y;
    return t+m(x-y*t,y);

}

int main(){

    cin>>a>>b;

    cout<<m(a,b)<<endl;

}

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    //freopen("input.txt", "r", stdin);
    long long a, b, ans = 0;
    cin >> a >> b;
    while (b)
    {
        ans += a / b;
        a %= b;
        swap (a, b);
    }
    cout << ans;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <cstdio>

using namespace std;

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    long long a, b;
    cin >> a >> b;
    long long ans = 0;
    while (a != 0 && b != 0)
    {
        if (a > b)
        {
            ans += a / b;
            a = a % b;
        }
        else
        {
            ans += b / a;
            b = b % a;
        }
    }
    cout << ans;

    return 0;
}

#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;

ll A, B, res;

void go(ll, ll);

int main() {
  cin >> A >> B;
  go(A, B);
  cout << res << "\n";

  return 0;
}

void go(ll x, ll y) {
  if (y != 0) {
    res += x / y;
    go(y, x % y);
  }
}

#include <iostream>
#include <algorithm>
using namespace std;
long long int a,b,x;
int main()
{
    cin >> a >> b;
    while (b)
    {
           x+= a/b;
           a%= b;
           swap(a, b);
    }
           cout << x;
}

#include <cstdio>
#include <cstring>
#include <cstdlib>

long long A, B;

long long Get(long long X, long long Y)
{
	if (!X||!Y) return 0;
	if (X>=Y) return Get(X%Y, Y)+X/Y;
	return Get(X, Y%X)+Y/X;
}

int main()
{
	scanf("%lld %lld", &A, &B);
	printf("%lld\n", Get(A, B));
	return 0;
}

//ITNOG
#include <bits/stdc++.h>

using namespace std;

const int maxN = 200*1000 + 321;
long long int ans;
long long gcd(long long int a, long long  int b)
{
  if(a < b) swap (a, b);
  if(b == 0) return a;
  ans += a / b;
  return gcd(a % b , b);
}
int main()
{
  ios::sync_with_stdio(0);
  long long a, b; cin >> a >> b;
  long long k = gcd(a, b);
  cout << ans << endl;
  return 0;
}

#include <iostream>
using namespace std;

typedef long long ll;

ll gcd(ll x, ll y) {
	if (y == 0) return x;
	else return gcd(y, x % y);
}

ll solve(ll a, ll b) {
	ll x = gcd(a, b);
	a /= x;
	b /= x;
	if (b == 1) return a;
	if (b > a) return solve(b - a, a) + 1;
	else {
		ll x = (a / b);
		return solve(a - x * b, b) + x;
	}
}

int main() {
	ll a, b;
	cin >> a >> b;
	cout << solve(a, b);
	return 0;
}

#include <iostream>
using namespace std;
long long count(long long a, long long b) {
	if (a == 0)
		return 0;
	if (a == b)
		return 1;
	if (a == 1)
		return b;
	long long r1, r2;
	if (a > b)
		return count(a % b, b) + a / b;
	long long c = b / a;
	long long d = b % a;
	long long cnt = 0;
	cnt += count(1, c);
	if (d > 0)
		cnt += count(a, d);
	return cnt;
}
int main() {
	long long a, b;
	cin >> a >> b;
	cout << count(a, b) << endl;
}

#include <iostream>

using namespace std;

typedef long long ll;

int main() {
	ll a, b;
	cin >> a >> b;
	ll n = 1;
	while (a != b) {
		if (a > b) {
			n += a / b;
			a = a % b;
		} else {
			n += b / a;
			b = b % a;
		}
		if (a == 0 || b == 0) {
			n--;
			break;
		}
	}
	cout << n << endl;
}

#include<iostream>
#include<vector>
#include<bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
	if (a == 0)
		return 1;
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
int main() {
	long long a, b;
	cin >> a >> b;
	long long n, m;
	a = a / gcd(a, b);
	b = b / gcd(a, b);
	n = min(a, b);
	m = max(a, b);
	long long ans = 0;
	while (n != 1) {
		ans += m / n;
		a = m % n;
		b = n;
		n = min(a, b);
		m = max(a, b);

	}
	ans += m;
	cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define TR(i,v)         for(__typeof((v).begin())i=(v).begin();i!=(v).end();++i)
#define DEBUG(x)        cout << #x << " = " << x << endl;
#define SIZE(p)         (int)(p).size()
#define MP(a, b)        make_pair((a), (b))
#define ALL(p)          (p).begin(), (p).end()
#define rep(i, n)       for(int (i)=0; (i)<(int)(n); ++(i))
#define REP(i, a, n)    for(int (i)=(a); (i)<(int)(n); ++(i))
#define FOR(i, a, b)    for(int (i)=(int)(a); (i)<=(int)(b); ++(i))
#define FORD(i, b, a)   for(int (i)=(int)(b); (i)>=(int)(a); --(i))
#define CLR(x, y)       memset((x), (y), sizeof((x)))
typedef long long LL;
typedef pair<int, int> pii;

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("A.in", "r", stdin);
    // freopen("out", "w", stdout);
#endif
    LL a,b; scanf("%lld%lld",&a,&b);
    LL res=a/b;
    a%=b;
    if(a) {
        LL x=a,y=b;
        while(x && y) {
            res+=y/x;
            y%=x;
            if(!y)  break;
            res+=x/y,x%=y;
        }
    }
    printf("%lld\n",res);
    return 0;
}

#pragma warning (disable : 4996)
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <assert.h>
#include <stack>
#include <algorithm>
#include <ios>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <queue>
#include <set>
#include <functional>
#include <cmath>
#include <sstream>
#include <map>

#define forn(i , n) for (int i = 0; i < n; ++i)
#define down(i, n) for (int i = n - 1; i >= 0; --i)

using namespace std;

typedef unsigned long long int u64;
typedef long long int i64;
typedef vector<int> vint;
typedef pair<int, int> pint;

bool file = false;
#define fileName "unequal"
#define T long double

i64 getNod(i64 a, i64 b)
{
    if (b == 0)
        return (i64)0;

    i64 step = 0;
    step = a / b;
    a %= b;
    return getNod(b, a) + step;
}

int main()
{

    //ios_base::sync_with_stdio(false);
    if (file)
    {
        freopen(fileName ".in", "r", stdin);
        freopen(fileName ".out", "w", stdout);
    }
    i64 a, b;
    cin >> a >> b;
    i64 ans = 0;
    ans = getNod(a, b);
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(arr) arr.begin() , arr.end()
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define rep(i , n) for(int i = 0 ; i < n ; i++)
#define siz(temp) temp.size()
#define len(str) str.length()
vector<ll> prime(1000005 , 1);

void cal_primearray(ll n)
{

    for (int p=2; p*p<=n; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == 1)
        {
            // Update all multiples of p
            for (int i=p*2; i<=n; i += p)
                prime[i] = 0;
        }
    }
}
ll modexpo(ll a , ll b)
{
    ll res = 1;
    while(b > 0)
    {
        if(b&1)
            res = (res*a)%mod;
        a=(a*a)%mod;
        b >>=1;
    }
    return res;
}
ll expo(ll a , ll b)
{
    ll res = 1;
    while(b > 0)
    {
        if(b&1)
            res = (res*a);
        a=(a*a);
        b >>=1;
    }
    return res;
}
ll gcd(ll a , ll b)
{
    if (a == 0)
        return b;
    return gcd(b%a , a);
}
bool isPrime(ll n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
    if (n%2 == 0 || n%3 == 0)
        return false;
    for (int i = 5 ; i*i <= n ; i+=6)
    {
        if ((n%i==0) || (n%(i + 2) == 0))
            return false;
    }
    return true;
}

ll max_value(vector<ll> arr)
{
    ll m=0;
    for (int i = 0 ; i < arr.size() ; i++)
        m = max(m , arr[i]);
    return m;
}

ll nCr(ll n , ll k)
{
    if(k==0)
        return 1;
    return (n* nCr(n-1,k-1))/k;
}
int main()
{
    ll a,b;
    cin >> a >> b;
    if (a % b == 0)
    {
        cout << a/b << endl;
    }
    else
    {
        ll ans = 1000000000000000005;
        ll num = a,den=b;
        if (num*den >= 0)
            ans = min(num*den , ans);
        ll foo=0;
        ll series=0;
        while(1)
        {
            //cout << num  << " " << den << endl;
            foo = series;
            if (num == 0)
                break;
            if (num == 1)
            {
                foo = foo + den;
                //cout << ans << " " << foo << endl;
                ans = min(foo , ans);
                //cout << ans << endl;
                break;
            }
            if (num > den)
            {
                foo = foo + num/den;
                series = series + num/den;
                num = num % den;
            }
            //cout << num << " " << den << " " << series << endl;
            ll temp = num;
            num = den;
            den = temp;
            //cout << num << " " << den << " " << series << endl;
            foo = foo + num/den;
            series = series + num/den;
            num = num % den;
            //cout << num << " " << den << " " << series << endl;
            foo = foo + num*den;
            if (num*den >= 0)
            {
                //cout << foo << " " << ans << endl;
                    ans = min(ans , foo);
                //cout << foo << " " << ans << endl;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define FF first.first
#define FS first.second
#define pb push_back
using namespace std;
const ll N=1000006, INF=1e16, MOD=998244353;

ll n, q=1, a[N], f[N], m, x, y, z, o, h, w, mx, mn, ans=INF, Ans;
string s, S;
vector <ll> v[N];
pair<ll, ll> p[N];

main(){ios_base::sync_with_stdio(false), cin.tie(0);

    //cin>>q;

    while(q--){ans=0;

    	cin>>n>>m;

    	while(n>1||m>1){
	    	ans+=n/m;
	    	n%=m;
	    	swap(n, m);
		}

    	cout<<ans+m;
    }

}

#include <iostream>
using namespace std;

int main() {

    long long a, b;
    cin >> a >> b;

    long long res = 0;
    while(a > 0 && b > 0) {
        if(a > b) {
            res += a / b;
            a %= b;
        } else {
            res += b / a;
            b %= a;
        }
    }
    cout << res << endl;
    return 0;
}

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

__int64 ans ;

__int64 gcd ( __int64 a , __int64 b ) {
    if( b == 0 ) {
        return a ;
    }
    ans += a / b ;
    return gcd ( b , a % b ) ;
}

int main(){
    __int64 a , b ;
    ans = 0 ;
    scanf( "%I64d%I64d" , &a , &b ) ;
    gcd( a , b ) ;
    printf( "%I64d\n" , ans ) ;
    return 0 ;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b)
{if(a==1){return b;}
ll temp=b;ll ans=b/a;
b=a;
a=temp%a;
return ans+gcd(a,b);
}
int main()
{
    ll a,b;cin>>a>>b;
ll q=gcd(a,b);
cout<<q;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	#ifndef ONLINE_JUDGE
            freopen("input.txt", "rt", stdin);
            freopen("output.txt", "wt", stdout);
    #endif
	long long a , b , c;
	cin >> a >> b;
	long long ans = 0;
	while(b){
		ans += (a/b);
		c = a;
		a = b;
		b = c%b;
	}
	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define LOCAL 0
using namespace std;
#define endl "\n"
#define ll long long
#define oo 0x3f3f3f3fLL
#define sz(s) (int)(s.size())
#define RT(s) return cout<<s,0
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(),v.end()
#define watch(x) cout<<(#x)<<" = "<<x<<endl
const int dr[]{ -1, -1, 0, 1, 1, 1, 0, -1 };
const int dc[]{ 0, 1, 1, 1, 0, -1, -1, -1 };
template<typename T>
vector<T> create(size_t n) {
	return vector<T>(n);
}
template<typename T, typename ... Args>
auto create(size_t n, Args ... args) {
	return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
void run() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#else
#endif
}

ll solve(ll a, ll b) {
	if (b == 0 || a == 0)return 0;
	if (a > b) {
		return a / b + solve(a % b, b);
	}
	else {
		return b / a + solve(a, b % a);
	}
}

int main() {
	run();
	ll a, b;
	cin >> a >> b;
	cout << solve(a, b);
}

#include <bits/stdc++.h>

using namespace std;

typedef long long INT;

INT ans;

void clac(INT n,INT m){

	if(!m) return;

	if(m==1) { ans+=n; return ;}

	ans+=n/m;

	clac(m,n%m);

}

int main() {

#ifndef ONLINE_JUDGE

    freopen("in.in", "r", stdin);

//    freopen("out.out", "w", stdout);

#endif

    INT a,b;

    cin>>a>>b;

    clac(a,b);

    cout<<ans<<endl;

}

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

int main(void){
    __int64 x,y,m,t;
    scanf("%I64d %I64d",&x,&y);
    m=1;
    while(1){
        if(x==y)
            break;
        if(x>y){
            t=x/y;
            x=x-t*y;
            m+=t;
            if(x==0){
                x=1;
                m--;
            }
        }
        else{
            t=y/x;
            y=y-t*x;
            m+=t;
            if(y==0){
                y=1;
                m--;
            }
        }
    }
    printf("%I64d\n",m);
    //system("pause");
    return 0;
}

#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;

int main(){
	long long a,b;
	cin>>a>>b;
	long long ans=a/b;
	a%=b;
	while(a && b){
		ans+=b/a;
		b%=a;
		if(b==0){
			break;
		}
		ans+=a/b;
		a%=b;
		if(a==0){
			break;
		}
	}
	cout<<ans<<endl;
}

#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <complex>
#include <limits>
#include <list>
#include <stack>
#include <numeric>
#include <queue>
#include <algorithm>
#include <functional>
#include <stack>
#include <bitset>
#include <map>
#include <set>
#include <map>
#include <list>
#include <math.h>
#include <set>
#include <stdio.h>
#include <ctype.h>
#include <vector>
#include <sstream>

using namespace std;

long long solve(long long a, long long b) {
    long long res = 0;
    while (a > 1 && b > 1) {
        if (a > b) {
            res += a / b;
            a %= b;
        }
        else {
            res += b / a;
            b %= a;
        }
    }
    return max(a, b) + res;
}

int main() {
    long long a, b;
    cin >> a >> b;
    cout << solve(a, b) << endl;
    return 0;
}

#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>

using namespace std;

long long a, b, x, ans;

int main() {

    cin >> a >> b;
    if (a == 1 && b == 1) {
        cout << 1;
        return 0;
    }
    while (!(a == 1 && b == 1)) {
        if (a > b) {
            x = a - b;
            x = x / b + 1;
            a -= x * b;
            ans += x;
            if (a == 0)
                break;
        }
        else if (a < b) {
            x = b - a;
            x = x / a + 1;
            b -= x * a;
            ans += x;
            if (b == 0)
                break;
        }
        else {
            ans += a;
            a = 1;
            b = 1;
        }
    }
    cout << ans;

    return 0;
}

#include <bits/stdc++.h>
#define PI 3.14159265
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define ll long long
#define mod 1000000007
#define mem(v,x) memset(v,x,sizeof(v))
const ll inf = 0x3f3f3f3f3f3f3f3fll;
using namespace std;

ll gcd(ll a, ll b){ if (a == 0) return b; return gcd(b % a, a);}
ll lcm(ll a, ll b){ return (a*b)/gcd(a, b);}

int main() {
#ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
#endif
    ll a,b,ans=0,k,l=0;
    cin >> a >> b;
    a -= ans*b;
    while(1){
        // l++;
        k = a/b;
        a -= k*b;
        ans += k;
        if(a==0) break;
        k = gcd(a,b);
        a /= k;
        b /= k;
        if(a==1){
            ans += b;
            break;
        }
        swap(a,b);
        // cout << ans << " " << a << " " << b << endl;
        // if(l==6) break;
    }
    cout << ans << endl;
	return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#define MAXN 200005
#define ll long long
#define INF 0x3f3f3f3f
ll ans;
void gcd(ll x, ll y) {
	if (y == 0) return;
	ans = ans + x / y;
	gcd(y, x%y);
}
int main()
{
	//freopen("C:\\Users\\Administrator\\Desktop\\input.in", "r", stdin);
	//freopen("C:\\Users\\Administrator\\Desktop\\output.out", "w", stdout);
	ll a, b;
	cin >> a >> b;
	gcd(a, b);
	cout << ans << endl;
	return 0;
}

/*
    Author: Rishabh Saxena
    All the best!!
*/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define XLL __int128
#define pii pair<int,int>
#define pll pair<LL,LL>
#define vi vector<int>
#define MATRIX vector<vector<int>>
#define MATRIXB vector<vector<bool>>
#define MATRIXC vector<vector<char>>
#define MATRIXLL vector<vector<LL>>
#define MATRIX3D vector<vector<vector<int>>>
#define MAX(A,B,C) max(A,max(B,C))
#define MIN(A,B,C) min(A,min(B,C))
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL)
int main(){
    FASTIO;
    LL N,D;
    cin >> N >> D;
    if(N == 1 && D == 1){
        cout<<1<<'\n';
    }
    else{
        if(N < D)
            swap(N,D);
        LL count = 0;
        while(D != 1){
            count += N/D;
            N = N % D;
            swap(N,D);
        }
        count += N;
        cout<<count<<'\n';
    }
}

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl '\n'
#define debug(x) f << #x << " = " << x << endl << flush;
#define sz(x) (int)x.size()
typedef long long ll;
typedef pair<int, int> ii;
//	Table is the complete reverse of dp
//	Always think of the pattern (LCM, GCD)
//	Be careful of unsigned
//	Think of a solution that gets TLE and optimize it
//	Use binary search
//	Solve on paper first
//ofstream f("debug.txt");
ll a, b, ans;

int main()
{
    cin.tie(nullptr); ios_base::sync_with_stdio(0);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    clock_t START_TIME = clock();
#endif
    cin >> a >> b;
    while(a && b)
    {
    	if(a > b)
    	{
    		ans += a / b;
    		a %= b;
    	}
    	else
    	{
    		ans += b / a;
    		b %= a;
    	}
    }
    cout << ans;
#ifndef ONLINE_JUDGE
    printf("\n// Time taken = %.3f ms", (clock() - START_TIME) * 1e3 / CLOCKS_PER_SEC);
#endif
}

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <iostream>
using namespace std;

long long answ = 0;

int main()
{
    float aa;
 //   ifstream cin ("input.txt");
 //   ofstream cout ("output.txt");
    long long a,b,c,k;
    cin >> a >> b;
    c = a;
    a = max(a,b);
    b = min(b,c);
    if (a == 1){
        cout << 1;
        return 0;
    }
    while (a != 1){
        answ += a/b;
        a %= b;
        swap(a,b);
    }
    cout << answ;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100000+10;

ll a,b,s=1;

int main()
{
    cin>>a>>b;
    while(a!=1||b!=1)
    {
        if(a>=b)
        {
            s+=a/b;
            a-=(a/b)*b;
            if(a==0)
            {
                s--;
                a=1;
            }
        }
        else
        {
            s+=b/a;
            b-=(b/a)*a;
            if(b==0)
            {
                s--;
                b=1;
            }
        }
    }
    cout<<s;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main() {
  long long a, b;
  cin >> a >> b;
  long long ans = 0;
  while(a && b) {
    if(a > b) {
      ans += a / b;
      a %= b;
    }else {
      ans += b / a;
      b %= a;
      swap(a, b);
    }
    long long g = __gcd(a, b);
    a /= g; b /= g;
  }
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>
#define int         long long
#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define nl          '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)
using namespace std;

int k=1;

int func(int a, int b){
	if(a==0){
		return k-1;
	}
	if(a==b){
		return k;
	}
	if(a==1){
		return b;
	} else if(a>b){
		return (a/b + func(a%b, b));
	} else {
		return (b/a + func(b%a, a));
	}
}

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	int a,b;
	cin >> a >> b;

	cout << func(a,b) <<nl;
}

#include <stdio.h>
#include <string>
#include <algorithm>

using namespace std;

int main ()

{
#pragma warning(disable:4996)
#ifdef _DEBUG
	freopen ("input.txt", "rt",stdin);
	freopen ("output.txt", "wt",stdout);
#endif
	long long int a,b,w=0;
	scanf("%I64d%I64d", &a,&b);
	for (; (a>0)&&(b>0); )
	{
		if (a<b) w+=b/a;
		else w+=a/b;
		if (a>b) a%=b;
		else b%=a;
	}
	printf("%I64d", w);
	return 0;
}

// Problem : C. Rational Resistance
// Contest : Codeforces - Codeforces Round #200 (Div. 2)
// URL : https://codeforces.com/contest/344/problem/C
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll          long long
#define pb          push_back
#define eb 			emplace_back
#define mp 			make_pair
#define pii         pair<ll,ll>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define fi          first
#define si          second
#define sz(x)       (ll)x.size()
#define endl        "\n"
#define checkbit(n, b) ((n >> b) & 1)
#define mod        1000000007
#define inf        1000000000000000000
#define puts(n)		cout<<n;
#define um           unordered_map<ll,ll>
#define rep(i,a,b)  for(int i=a;i<b;i++)
#define setbits(x) __builtin_popcount(x)
#define mset(m,v) memset(m,v,sizeof(m))
#define int ll
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
using namespace std;
int power(int base, int exp);

//=======================

void solve() {
 int a,b;
 cin>>a>>b;
int cnt=0;
while(b>0)
{
	cnt+=a/b;
	a%=b;
	swap(a,b);
}
cout<<cnt<<endl;
}

int32_t main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

   int t = 1;
   // cin >> t;
    while(t--) {
      solve();
    }

    return 0;
}

int power(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}

// go away evil dogger
#include<bits/stdc++.h>

using namespace std;

void arquivo(){
	printf("ME TIRA DAQUI\n");
	freopen("in","r",stdin);
	//freopen("out","w",stdout);
}

long long a,b,t,ans=0;

int main(){
	//arquivo();
	scanf("%lld%lld",&a,&b);
	if(b > a) swap(a,b);
	while(b){
		t = b;
		ans += a/b;
		b = a % b;
		a = t;
	}
	printf("%lld\n",ans);
	return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <iomanip>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <set>
#include <map>
#include <sstream>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
#define x first
#define y second
#define mp make_pair
#define pb push_back
ll G(ll a,ll b){if(b==0)return a;return G(b,a%b);}
int main(){
	ios_base::sync_with_stdio(false);
	ll a,b,c,ans=0;
	cin>>a>>b;
	while(a>0){
		ans+=a/b;
		a%=b;
		if(a==0)break;
		c=a;
		a=b-a;
		b=c;
		ans++;
	}
	cout<<ans;
}

#include <bits/stdc++.h>
using namespace std;
#define ss second
#define ff first
#define pb push_back
#define eb emplace_back
#define loop(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define len(a) (int)(a).size()
#define allv(a) (a).begin(),(a).end()
using ll = long long;
using ii = pair<int,int>;
using vi = vector<int>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3fLL;
const double err = 1e-9;
const int mod = 1e9+7;
const int N = 1e5+10;

ll gdc(ll a,ll b){
	return b==0?a:gdc(b,a%b);}

ll reverse_gdc(ll a,ll b){
	//cout<<a<<' '<<b<<endl;
	if(b==1)return a;
	if(a>b){
		ll v = a/b;
		a-=v*b;
		return v + reverse_gdc(a,b);
	}else{
		b-=a;
		return 1 + reverse_gdc(b,a);
	}
}

int main(){
	ios::sync_with_stdio(0);cin.tie(NULL);

	ll a,b;
	cin>>a>>b;
	ll g = gdc(a,b);
	a/=g;b/=g;
	cout<<reverse_gdc(a,b)<<endl;
	return 0;
}

#include <iostream>
#include <vector>
#include <math.h>
using namespace std;

typedef long long ll;
ll cnt;

void gcd(ll a, ll b){
  if(a==0)return;
  cnt+=b/a;
  gcd(b%a,a);
}

int main(){
  ll a, b;
  cnt=0;
  cin>>a>>b;
  if(a > b)swap(a, b);
  gcd(a, b);
  cout<<cnt<<endl;
  return 0;
}

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <stack>
#include <queue>
#include  <map>
#include <set>
#include <utility>
#define pi 3.1415926535898
#define ll long long
#define lson rt<<1
#define rson rt<<1|1
#define eps 1e-6
#define ms(a,b) memset(a,b,sizeof(a))
#define legal(a,b) a&b
#define print1 printf("111\n")
using namespace std;
const int maxn = 1e6+10;
const int inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1000000007;
//998244353

int a[maxn];

int main()
{
    ll a,b;
    scanf("%lld%lld",&a,&b);
    ll ans=0;
    while(1)
    {
        if(b>a)swap(a,b);
        //if(a==0||b==0)break;
        if(a%b==0)
        {
            ans+=a/b;
            break;
        }else
        {
            ans+=a/b;
            a=a%b;
        }
    }
    printf("%lld\n",ans);
}

#include <iostream>

using namespace std;

long long totalres(long long numer, long long denom)
{
	if (denom == 1) return numer;
	else if (numer > denom) return numer/denom + totalres(numer%denom, denom);
	else if (numer == denom) return 1;
	else return denom/numer + totalres(denom%numer, numer);
}

int main()
{
	long long numer,denom;
	cin >> numer >> denom;
	cout << totalres(numer, denom) << '\n';
	return 0;
}

#include <iostream>

using namespace std;

int main()
{
	uint64_t a, b;

	cin>>a>>b;

	uint64_t r = 0;

	while(a != 0 && b != 0)
	{
		r += (a/b);
		a = a%b;

		if(a==0)
			break;

		r += (b/a);
		b = b%a;
	}

	cout<<r<<endl;

	return 0;
}

/*
 *  Created on: 24-December-2019
 *      Author: ankur314
 */
#include <bits/stdc++.h>
#define lli long long int
#define li long int
#define db double
#define ldb long double
#define vr vector
#define it iterator
#define fi(i,a,n) for(int i=a;i<n;i++)
#define fli(i,a,n) for(long int i=a;i<n;i++)
#define flli(i,a,n) for(long long int i=a;i<n;i++)
#define pb(n) push_back(n)
#define ppf() pop_front()
#define pii pair<int,int>
#define MOD 1000000007
#define INF 1000000005

#define debug1(x) cout<<x<<"\n"
#define debug2(x,y) cout<<x<<" "<<y<<"\n"
#define debug3(x,y,z) cout<<x<<" "<<y<<" "<<z<<"\n"
#define debug4(x,y,z,w) cout<<x<<" "<<y<<" "<<z<<" "<<w<<"\n"

using namespace std;

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    /*freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);*/
    lli a, b;
    cin>>a>>b;
    lli ans = 0;
    if(a < b) swap(a, b);
    while(b)
    {
    	ans += a/b;
    	a = a % b;
    	swap(a, b);
    }
    cout<<ans;
    return 0;
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define moduli 1000000007
#define int long long int
#define F first
#define S second
#define Pii pair<int,int>
#define Pdi pair<long double,int>

#define pb push_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define um unordered_map
#define R return
#define d long double

using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ord_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int maxn = 1001;
int Co[maxn + 1][maxn + 1];
void precompute()
{

  Co[0][0] = 1;
  for (int n = 1; n <= maxn; ++n) {
    Co[n][0] = Co[n][n] = 1;
    for (int k = 1; k < n; ++k)
      Co[n][k] = (Co[n - 1][k - 1] + Co[n - 1][k]) % moduli;

  }

}

// int stringtointer(string a)
// {

//   return stoi(a);
// }
// int countdigit(int a)
// {

//   return log10(a) + 1;
// }

// int factorial(int n)
// {

//   return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
// }

// int floorSqrt(int x)
// {

//   if (x == 0 || x == 1)
//     return x;
//   int start = 1, end = x, ans;
//   while (start <= end)
//   {
//     int mid = (start + end) / 2;
//     if (mid * mid == x)
//       return mid;
//     if (mid * mid < x)
//     {
//       start = mid + 1;
//       ans = mid;
//     }
//     else
//       end = mid - 1;
//   }

//   return ans;
// }

// bool checkpallindrome(string s)
// {

//   string ans = s;

//   reverse(ans.begin(), ans.end());

//   if (ans == s)
//     return true;

//   return false;

// }

// int kadane(int a[], int size)
// {
//   int max_so_far = INT_MIN, max_ending_here = 0;

//   for (int i = 0; i < size; i++)
//   {
//     max_ending_here = max_ending_here + a[i];
//     if (max_so_far < max_ending_here)
//       max_so_far = max_ending_here;

//     if (max_ending_here < 0)
//       max_ending_here = 0;
//   }
//   return max_so_far;
// }

// int calc(int x, int y, int m)
// {
//   int res = 1;

//   x = x  ;

//   if (x == 0) return 0;

//   while (y > 0)
//   {

//     if (y & 1)
//       res = (res * x % m ) % m ;

//     y = y >> 1;
//     x = (x % m * x % m) % m;
//   }

//   return res;

// }

// int gcdExtended(int a, int b, int *x, int *y)
// {
//   // Base Case
//   if (a == 0)
//   {
//     *x = 0, *y = 1;
//     return b;
//   }

//   int x1, y1;
//   int gcd = gcdExtended(b % a, a, &x1, &y1);

//   *x = y1 - (b / a) * x1;
//   *y = x1;

//   return gcd;
// }

// // Function to find modulo inverse of a
// int modInverse(int a, int m)
// {
//   int x, y;
//   int g = gcdExtended(a, m, &x, &y);
//   int res = (x % m + m) % m;
//   return  res;

// }

// vi divisor(int n)
// {

//   vi a;

//   for (int i = 1; i * i <= n; i++)
//   {
//     if (n % i == 0)
//     {
//       a.pb(i);

//       if (i * i != n)
//         a.pb(n / i);
//     }
//   }

//   return a;

// }

// int fenwick[1000001];
// void update(int index, int x, int n)
// {
//   while (index <= n)
//   {
//     fenwick[index] += x;
//     index += (index & (-index));
//   }

// }

// int query(int n, int index)
// {
//   int ans = 0;
//   while (index > 0)
//   {
//     ans += fenwick[index];
//     index -= (index & (-index));
//   }
//   return ans;
// }

void solve(int tc)
{

  int a, b;
  cin >> a >> b;
  int sum = 0;
  while (b)
  {
    sum += (a / b);
    a %= b;
    swap(a, b);
  }

  cout << sum;

}

int32_t main()
{

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  //cin >> t;
  int tc = 1;
// precompute();
  while (t--)
  {

    solve(tc);

    tc++;

  }
  return 0;
}

#include<iostream>
using namespace std;
long long gcd(long long n,long long m)
{
	if(n<m)
		swap(n,m);
	while(m)
	{
		long long int r=n%m;
		n=m;
		m=r;
	}
	return n;
}
int main()
{
	long long int a,b;
	while(cin>>a>>b)
	{
		long long int num=0;
		if(a>b)
		{
			num=a/b;
			a=a%b;
		}
		b=b/gcd(a,b);
		a=a/gcd(a,b);
		if(a!=0)
		{
			if(a!=1)
			{
				do
				{
					num+=b/a;
					long long int r=b%a;
					b=a;
					a=r;
					if(a==0)
					{
						b++;
						break;
					}
				}while(a!=1);
			}
			num+=b;
		}
		cout<<num<<endl;
	}
	return 0;
}

#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <string.h>
#include <cmath>
#include <memory.h>
#include <algorithm>
using namespace std;
typedef long long ll;
int main()
{
    ll a, b, c;
    cin >> a >> b;
    c = 0;
    while (a > 0){
        if (a < b)
            swap(a, b);
        c += a / b;
        a %= b;
    }
    cout << c;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main() {
    long long a, b, ans = 0;
    scanf("%lld %lld", &a, &b);

    while(b != 0) {
        ans += a/b ;
        swap(a, b) ;
        b %= a ;
    }

    std::cout<< ans << std::endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define start_routine() int begtime = clock();
#define end_routine() int endtime = clock(); cerr << endl << "Time elapsed: " << (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms"; return 0
#define speed() cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false)
// #define exit(a, b) return cout << a, b;

// debugger
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#define debug(stuff) cout << #stuff << ": " << stuff <<endl
#define debugc(stuff) cout << #stuff << ": "; for(auto x: stuff) cout << x << " "; cout << endl;
//

#define loop(i,a,b) for(ll i=a;i<b;i++)
#define all(v) v.begin(), v.end()

#define print(stuff) cout << stuff << endl
#define printc(stuff) for(auto x: stuff) cout << x << " "; cout << endl;
#define printPrec(stuff) cout << fixed << setprecision(15) << stuff << endl;
#define len length
#define ret0 return 0
#define ret return

#define ll long long
#define ld long double
#define fi first
#define endl '\n'
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fill(ar, x) memset(ar, x, sizeof ar)

#define vl vector<ll>
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define pq priority_queue<ll>

// typedef tree<ll,null_type,less<ll>,rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;

#define inf (long long int) 1e18
#define eps 0.000001
#define mod 1000000007
#define mod1 998244353
#define MAXN (ll)2e5+5
#define N (1ll<<10)

ll ceil(ll a,ll b){
    return a/b+(a%b!=0);
}

void solve(){
    ll a, b;
    cin>>a>>b;
    ll ans = 1;
    while(!(a == 1 && b == 1)){
        // trace(a, b, ans);
        if(a > b){
            ll k = ceil(a, b);
            k--;
            a = a - k*b;
            ll g = __gcd(a, b);
            a /= g, b /= g;
            ans += k;
        }
        else if(b > a) {
            ll k = ceil(b, a);
            k--;
            b = b - a*k;
            ll g = __gcd(a, b);
            a /= g, b /= g;
            ans += k;
        }
        // trace(a, b, ans);
    }
    print(ans);
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
        start_routine();
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    ll t = 1;
    // cin>>t;
    while(t--){
        solve();
    }

    #ifndef ONLINE_JUDGE
        end_routine();
    #endif
}

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
#define pb push_back
#define sz(i) (int)(i.size())
#define F first
#define S second
#define L long double
#define P pair<int, int>
const int inf = 0x3f3f3f3f3f3f3f3LL;
const int mod = (int)1e9 + 7;
using namespace std;
#if LOCAL
#include "../../tr.h"
#else
#define tr(...)
#define endl '\n'
#endif
template<class T> void smin(T& a, T val) {if (a > val) a = val;}template<class T> void smax(T& a, T val) {if (a < val) a = val;}
const int N = 5*(int)1e5 + 10;

int solve(int a, int b){
   if(b == 1){
      return a;
   }

   if(a == 1){
      return b;
   }

   if(a > b){
      return (a/b) + solve(a%b, b);
   }

   return (b/a) + solve(a, b%a);
}

int32_t main(){_

   int a, b;

   cin >> a >> b;

   int gc = __gcd(a, b);

   a /= gc;
   b /= gc;

   cout<<solve(a, b);

   return 0;
}

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
void main()
{
	long long a,b,o = 0;
    cin>>a>>b;
	while((a>1)&&(b>1))
	{
		o+=a/b+b/a;
		if(a>b)a%=b;else b%=a;
	}
	if(a==1) o+=b;else if(b==1)o+=a;
	cout<<o;
	//system("pause");
}

#include <stdio.h>

int main() {
    __int64 a, b, t, r=0;
//  freopen ("in.txt", "r", stdin);
    scanf("%I64d %I64d", &a, &b);
    if (a>b) t=a, a=b, b=t;
    while (a) {
        r+=b/a;
        t=b%a;
        b=a, a=t;
    }
    printf("%I64d\n", r);
}

#include <iostream>
#include <algorithm>
using namespace std;
long long a,b,x;
int main(){
cin>>a>>b;
    while (b){
    x+=a/b;
    a%=b;
    swap(a, b);
}
cout<<x;
}

#include <algorithm>
#include <bitset>
#include <deque>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <iomanip>
#include <vector>

#define fst first
#define snd second
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define sz size()
#define FORN( i , s , n ) for( int i = (s) ; i < (n) ; i++ )
#define FOR( i , n ) FORN( i , 0 , n )
#define FORIT( i , x ) for( typeof x.begin() i = x.begin() ; i != x.end() ; i++ )
#define trace(x)    cerr << #x << ": " << x << endl;
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define read ios::sync_with_stdio(false)

using namespace std;

typedef long long int64;
typedef vector <int> vi;
typedef pair <int,int> ii;
typedef vector <string> vs;
typedef vector <ii> vii;

int main(){
    int64 a,b,tmp;
    cin>>a>>b;
    int64 ans=0;
    while(1){
        if(b<a) swap(a,b);
        if(a==1) {
            ans+=b;
            break;
        }
        tmp=a;
        ans+=b/a;
        a = b%a;
        b=tmp;

    }
    cout<<ans<<endl;
}

#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;

ll gcd(ll a, ll b){
	if(b == 1) return a;
	return a/b + gcd(b, a%b);
}
int main(){
	ll a, b;
	rl(a); rl(b);
	ll ans = gcd(a, b);
	printf("%lld",ans);
	return 0;
}

#include <bits/stdc++.h>

#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define graph vector<vector<int>>
#define pii pair<int,int>
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define all(x) (x).begin(),(x).end()
#define sp(x,y) {ZaMeNa=(x);(x)=(y);(y)=ZaMeNa;}
#define inv(n) power((n), mod - 2)
#define ff(i,a,b) for (int i = a; i < b; i++)
#define bff(i,a,b) for (int i = b-1; i >= a; i--)
#define dif(x) ((x).second-(x).first)

using namespace std;
long long int typedef li;
unsigned long long int typedef ull;
const long double PI = 3.14159265359;
//const int mod = 998244353;
const int mod = 1000000007;
//const int mod = 2147483647;
//const li mod = 9223372036854775807
li ZaMeNa;

inline int power(li _a, int _n)
{
	li _c = 1;
	while (_n>0)
	{
		if (_n & 1) _c = _c * _a%mod;
		_a = _a * _a%mod;
		_n>>=1;
	}
	return _c;
}

inline int geom(li _a, int _n) //suma 1 + a + a^2 + ... + a^n
{
	if (_a == 1) return _n+1;
	return ((li)power(_a, _n + 1) - 1) * inv(_a - 1) % mod;
}

inline li __lcm(int a,int b)
{
    return (li)(a/__gcd(a,b))*(li)b;
}

inline li sum(int n) {return ((li)n*(n+1))>>1;}

inline li kv(int n) {return ((li)n*(n+1)*((n<<1)+1))/6;}

//Note to self: Check for overflow

int main()
{
	FAST;

	li a,b,k; cin>>a>>b;
	if (a<b) sp(a,b);
	for (k=0;b;)
    {
        k+=a/b;
        ZaMeNa = a; a = b;
        b = ZaMeNa%b;
    }
    cout<<k;
}

//Note to self: Check for overflow

#include <bits/stdc++.h>
using namespace std;
#define int long long
int solve(int a,int b){if(b==0)return 0;return (a/b)+solve(b,a%b);}
int32_t main(){
	int a,b;cin >> a >> b;
	cout << solve(a,b) << endl;
	return 0;
}

#include <bits/stdc++.h>
#define MOD 1000000007
#define Nmax 1000005
#define INF 2000000000
#define eps 0.000000000001

using namespace std;

int n;
int a[Nmax];

inline int Pow_Log(int x, int p)
{
    int sol=1;
    while(p)
    {
        if(p&1)
        {
            sol=(1LL*sol*x)%MOD;
            --p;
        }
        p>>=1;
        x=(1LL*x*x)%MOD;
    }
    return sol;
}

inline long long Solve(long long a, long long b)
{
    if(a<b)
    {
        if(a==1) return b;
        return 1+Solve(b-a,a);
    }
    long long sol= a/b;
    if(a%b) sol+=Solve(a%b,b);
    return sol;
}

int main()
{
    long long a,b;
    #ifndef ONLINE_JUDGE
        freopen ("date.in","r",stdin);
        freopen ("date.out","w",stdout);
    #endif
    cin.sync_with_stdio(0);
    cin>>a>>b;
    if(a==1 && b==1) cout<<1;
    else cout<<Solve(a,b);
    return 0;
}

/*
    ॐ नमः शिवाय
                    */

#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
#define MAX 100005
#define pi 2 * acos(0.0)
using namespace std;

#define vect vector<int>
#define vectp vector<pair<int,int>>
#define matrix vector<vector<int>>
#define mp make_pair
#define pb(i) push_back(i)
#define lb lower_bound
#define ub upper_bound
#define geti(n) int n; cin >> n;
#define gets(str) string str; cin >> str;
#define loop(i, j, k) for (int i = j; i < k; i++)
#define rloop(i,j,k) for(int i = j; i >= k; i--)
#define test  int t;  cin >> t; while (t--)
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

/* YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY */

signed main()
{
    fastio;

    geti(a); geti(b);

    int ans = 0;

    while(a && b)
    {
        ans += a/b;

        a = a%b;
        if(a < b)
            swap(a,b);
    }

    cout<< ans;

    return 0;
}

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
    __int64 a,b,s=0;
    cin>>a>>b;
    if (a==b) {
              cout<<1;
              return 0;
              }
    while (a!=1)
    {
          s+=a/b;
          a=a%b;
          swap(a,b);
          }
          s+=b/a;
          cout<<s;
    //system("pause");
    return 0;
    }

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{

long long a , b ,sum=0;
cin>>a>>b;
if(a%b==0)
cout<<a/b;
else if (a%b==a)
while(a)
{
sum+=b/a;
long long c=a;
a=b%a;
b=c;

}
else if (a/b>1)
sum+=a/b;

a=a%b;

while(a)
{
sum+=b/a;
int c=a;
a=b%a;
b=c;

}
if(sum>0)
    cout<<sum;
    return 0;
}

#include <iostream>
using namespace std;
int main(){
	std::ios::sync_with_stdio(false);
	long long int a=0,b=0;
	cin>>a>>b;
	if(a==1 && b==1)cout<<1<<'\n';
	else if(a==1)cout<<b<<'\n';
	else if(b==1)cout<<a<<'\n';
	else if(a>b)
		cout<<a/b+b<<'\n';
	else cout<<b/a+a<<'\n';
}

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
using namespace std;
// int queries[100001];
int main() {
    ll a,b;
    cin >> a >> b;
    if(b==1)
    {
        cout << a <<endl;
        return 0;
    }
    else if(a<b)
    {
        cout << b <<endl;
        return 0;
    }
    ll temp = a/b + b;
    cout << temp <<endl;
	return 0;
}

#include <cstdio>
#include <queue>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
const int inf = 1e9;
const int MAXN = 20+5;
int n;
long long get_ans(long long a,long long b)
{
    for(long long i = 1; i <= sqrt(a); ++i)
    {
        if(a%i == 0)
        {
            if(a/i + i ==b)
            {
                return b;
            }
        }
    }
    return 0;
}
int main()
{
    long long a,b;
    scanf("%I64d%I64d",&a,&b);
    if(a%b == 0)cout<<a/b;
    else
    {
        if(a > b)
        {
            long long ans = a/b;
            long long a1=a%b;
            long long t = get_ans(a1,b);
            if(t)
            {
                cout<<ans+t<<endl;
                return 0;
            }
            cout << a/(b-1)*b <<endl;
        }
        else cout<<b<<endl;
    }
    return 0;
}

#include <iostream>
using namespace std;
long int func(long int a,long int b)
{
    if(a==1)
        return b;
    else if(a<b)
        return func(b,a);
    else
        return (a/b + func(a%b,b));
}
int main()
{
    long int a;
    long int b;
    cin>>a>>b;
    cout<<func(a,b);
    return 0;
}

#include <fstream>
#include <iostream>
#include <string>
#include <map>
#include <deque>
#include <vector>

using namespace std;

//std::ifstream in("input.txt", std::ios::in);
//std::ofstream out("output.txt", std::ios::out);

int gcd (unsigned long long int a, unsigned long long int b)
{
//cout << a << " " << b << endl;
    if (b == 0)
        return a;
    else
    {
        if (((a == 1)||(b == 1)) && (a != b))
            cout << a+b;
        return gcd (b, a % b);
    }
}

int main()
{
unsigned long long int a,b;
    cin >> a >> b;
    if ((a == 1) && (b == 1))
    cout << 1;
    else
    gcd(a,b);
}

#include <bits/stdc++.h>

using namespace std;

typedef complex<int> point;
#define dp(a, b) ((conj(a) * b).real())
#define vec(a, b) (b - a)
#define cp(a, b) ((conj(a) * b).imag())
#define angle(a) (atan2(a.imag(), a.real()))
#define rotate0(a, angle) (a * exp(point(0, angle)))
#define length(a) (hypot(a.imag(), a.real()))
#define rotateAr(a, angle, around) (rotate0(((a) - (around)),(angle)) + around)
#define reflect(a, m) (conj((a)/(m)) * (m))
#define normalize(a) ((a) / length(a));
typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define per(i, start, end) for(int i = (int)start-1; i >= end; --i)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
const double PI = 3.14159265358979323846;
const double eps = (1e-8);

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("facebook.txt", "w", stdout);
#endif
    int x, y;
    cin >> x >> y;
    ll ans = x/y;
    x = x%y;
    if (x)
    {
        ll a = 1, b = -y, c = x;
        ll temp = sqrt(b * b - 4 * a * c + eps);
        ans = ans + (-b + temp) / (2LL * a);
        ans = ans + (-b - temp) / (2LL * a);
    }
    cout << ans << endl;
    return 0;
}

#include "cstdio"
#include "cstring"
#include "iostream"
using namespace std;
long long gcd(long long a,long long b)
{
	if(a<b)return gcd(b,a);
	if(b==0)return a;
	return gcd(b,a%b);
}
int main()
{
	//freopen("input.txt","r",stdin);
	long long a,b;
	cin>>a>>b;
	long long ans=0;
	ans+=a/b;
	a=a%b;
	if(a!=0)
	{
		long long g=gcd(a,b);
		b/=g;
		ans+=b;
	}
	cout<<ans;

	return 0;
}

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef vector<int> vint;
typedef pair<int, int> pii;
typedef long long ll;

void solve() {
	ll a, b;
	cin >> a >> b;
	ll r = a / b;
	r = min(r + b, max(a, b));
	cout << r << endl;
}

int main() {
	#ifndef ONLINE_JUDGE
		freopen("test.in", "rt", stdin);
		freopen("test.out", "wt", stdout);
	#endif
	solve();
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    cout<<max(a,b)<<endl;
}

#include <iostream>
#define ll long long
using namespace std;
ll gcd(ll a,ll b)
{
    return !b?a:gcd(b,a%b);
}
int main()
{
    ll a,b,ans=0;
    cin>>a>>b;
    if(a==b)cout<<"1\n";
    else if(a>b)
    {
        ans+=a/b;
        a%=b;
        int x=gcd(a,b);
        a/=x,b/=x;
        ans+=a*b;
        cout<<ans<<endl;
    }
    else
    {
        int x=gcd(a,b);
        a/=x,b/=x;
        ans+=a*b;
        cout<<ans<<endl;
    }
}
/**
  *https://acm.scut.space/
  *Job ID:6991
  *Time: 1523102983
**/

#include <iostream>
#include <iomanip>

using namespace std;
int main() {
double  a,b ,A;
 cin>>a>>b;
 A=a/b;

if(A>=1)cout<<fixed<<setprecision(0) <<a;
else cout<<fixed<<setprecision(0) <<b;
}

#include <cstdio>
#include <algorithm>

int main()
{
    long long a, b, res = 0;
    scanf("%I64d%I64d", &a, &b);
    if (b == 1) res = a;
    else
    while (b > 1)
    {
        if (a < b) std::swap(a, b);
        res += a / b;
        a -= a / b;
    }
    printf("%I64d", res);
}

#include <iostream>
#include <bits/stdc++.h>
#define mod 1000000007
#define all(c) c.begin(), c.end()
const double pi = acos(-1.0);
typedef long long ll;
using namespace std;
int main()
{
ll a , b ; cin>>a >>b ;
if (b>a)
    cout<<b;
else if (b==a)
    cout<<1;
else{
    if (a%b==0)
        cout<<a/b;
    else
        cout<<(a/b)+b;
}
return 0 ;
}

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define foreach(i,c) for(auto i=(c).begin();i!=(c).end();++i)
#define all(c) begin(c),end(c)
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

int main()
{
	for(ll a,b;cin>>a>>b;)
		cout<<max(a,b)<<endl;
}

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int main(){
	__int64 a,b;
	cin>>a>>b;
	int s=0;
	while(b){
		s+=(int)(a/b);
		__int64 c = b;
		b=a%b;a=c;
	}
	cout<<s;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    int first=max(a,b),second=min(a,b);
    long long x=0;
    while(1){
        x+=first/second;
        first%=second;
        if(first==0)break;
        a=max(first,second),b=min(first,second);
        first=a,second=b;
    }cout<<x<<endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6+5;
const int mod = 1e9+7;

#define inf 1e12
#define int long long
#define fast ios_base::sync_with_stdio(0); cin.tie(0);
#define F first
#define S second
#define pb push_back

int cal(int a, int b) {
	if(b == 1)
		return a;
	if(a >= b)
		return a / b + cal(a % b, b);
	return 1 + (b - a, a);
}

int32_t main() {
	fast;
	int a, b;
	cin >> a >> b;
	cout << cal(a, b);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define sqr(x) ((x) * (x))
#define pb push_back
#define mp make_pair
#define ins insert
#define X first
#define Y second
#define fin(name) freopen(name, "r", stdin)
#define fout(name) freopen(name, "w", stdout)
#define files(name) fin(name".in"); fout(name".out")
#define enter cout << "\n"
#define space cout << " "
#define endl "\n"
#define fi(st,n) for(int i=(st);i<=(n);i++)
#define fj(st,n) for(int j=(st);j<=(n);j++)
#define clean(a) memset((a),0,sizeof (a));
#define sync ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

typedef long long ll;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ldbl;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int inf = (int) 1e9, maxn = (int) 1e5 + 1;
const double eps = (double) 1e-8;
const int mod = (int) 1000000007;

//fin("t.in");
//fout("t.out");
// cout<<fixed<<setprecision(20);
//srand(time(0));

ll a, b;

int gcd(int a, int b)
{
	if (a == 0)
		return 0;
	return gcd(b % a, a) + b / a;
}

int main()
{
//	fin("t.in");
	sync;
	cin >> a >> b;
	cout << gcd(a, b);
    return 0;
}

#include<stdio.h>
#include<iostream>
#include<cstdio>
#include<queue>
#include<ctype.h>
#include<cstring>
#include<math.h>
using namespace std;
long long cnt=0;
long long a,b,t;
void dfs(long long a,long long b)
{   if(a==0||b==0)
        return;
	if(a<b)
	{
		t=a;
		a=b;
		a=t;
	}
	cnt+=a/b;
	dfs(b,a%b);
}
int main()
{
	cin>>a>>b;
	dfs(a,b);
	printf("%d",cnt);
	return 0;
}

#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pi acos(-1.0)
#define EPS 1e-9
#define mem(n,x) memset(n,x,sizeof(n))
typedef long long ll;

using namespace std;

int steps(ll a,ll b){
	if(b==0)return 0;
	return (a/b)+steps(b,a%b);
}

int main()
{
	ll a,b;cin>>a>>b;
	cout<<steps(a,b)<<"\n";
	return 0;
}

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
#include <list>
#include <queue>
#include <map>
#include <ctime>

#define F first
#define S second
#define pb push_back
#define INF 1<<30
#define mp make_pair
#define pi pair<int, int>
#define LL long long
#define readString(s) gets(rs);s = rs
#define SQR(a) ((a)*(a))

#define swag swap

using namespace std;

const int N = 111111;
char buf[N];

long long a, b;

int main()
{

    #ifdef DEBUG
        freopen("input.txt", "r", stdin);
        //freopen(TASKNAME".in", "r", stdin);
        //freopen(TASKNAME".out", "w", stdout);
    #else
        //freopen("input.txt","r",stdin);
        //freopen("output.txt","w",stdout);
    #endif

    scanf("%I64d%I64d", &a, &b);

    long long x, y;
    long long resistors = 1;

    resistors += b-1;

    resistors += (a-1)/b;
    if ((a-1) % b != 0)
        resistors++;

    printf("%I64d", resistors);

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int lli;

int main(int argc, char *argv[]){
  lli a, b;
  cin >> a >> b;
  lli ans = a / b;
  a %= b;
  if(a != 0){
    lli re = a;
    lli add = (b - 1LL) / re;
    std::cout << ans + re + add << std::endl;
  }else{
    std::cout << ans << std::endl;
  }
  return 0;
}

#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<utility>
using namespace std;
#define lli long long int
#define rep(i,a,b) for(lli i=a;i<b;i++)

lli gcd(lli a,lli b)
{
    if(b==0)
    return a;
    else
    return gcd(b,a%b);
}

int main()
{
    lli a,b,ans=0,t,p;
    cin>>a>>b;
    if(a<b)
    {
        t=b/a;
        ans=a+t;
    }
    else if(a==b)
    ans=1;
    else
    {
        t=a/b;
        a=a-t*b;
        p=b/a;
        if(a==1)
        ans=p+t;
        else
        ans=a+p+t;
    }
    cout<<ans;
}

#include<bits/stdc++.h>

using namespace std;

int main()
{
	long long a,b;
	cin>>a>>b;
	long long ans=a/b;
	a=a%b;
	if((b-a)*b>0)
		ans+=min(a*b,(b-a)*b);
	else
		ans+=a*b;
	cout<<ans<<endl;
	return 0;
}

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main(){
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    long long n,m;
    cin >> n >> m;
    long long res = 0;
    res+=n/m;
    n=n%m;

    if (n){
        long long min = LONG_MAX;
        for(long long i =1; i<=sqrt(1.*n); i++){
            if (n%i == 0){
                if ((m-i)%(n/i) == 0){
                    if ((m-i)/(n/i)+(n/i)<min) min = (m-i)/(n/i)+(n/i);
                }
                if ((m-i)%(i) == 0){
                    if ((m-i)/(i)+(i)<min) min = (m-i)/(i)+(i);
                }
            }
        }
        cout << res+min;
    } else cout << res;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main(){
    long long int a , b , c , h ;
    cin >> a >> b ;
    c = a/b ;
    if(b==1)
     cout << c << endl;
    else {
        a = a - (a/b)*b ;
        //cout << a <<" "<< b <<"\n";
        while(a%b!=0){
            h = a ;
            a = b-a ;
            b = h ;
            //cout << a <<" "<< b <<"\n";
            //c++;
            if(a/b>1){
                c+=(a/b);
                a = a- (a/b)*b;
                if(a==0)
                    c++;
            }else
                c++;
            if(a<0)
                break;
        }
        //cout<< a << " " << b << "\n";
        cout << c+a << endl;

    }
}
//5 / 8

#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
using namespace std;

typedef long long int           ll;
typedef long double             ld;

#define test(t)		        int t; cin >> t; while(t--)
#define f(i, a, b)	        for(int (i) = (a); (i) < (b); ++(i))
#define all(a)		        (a).begin(), (a).end()
#define endl 		        "\n"
#define deb(x)		        cout << #x << ": " << x << endl;
#define deb2(x,y)	        cout << #x << ": " << x << " ~ " <<  #y << ": " << y << endl;
#define deba(arr)	        cout << #arr << " ~ [ "; for(auto n: arr) cout << n << " "; cout << "]" << endl;
#define debap(arr)	        cout << #arr << " ~ [ "; for(auto n: arr) cout << n.first << "-" << n.second << " "; cout << "]" << endl;
#define fast 		        ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

void solve()
{
    ll a, b;
    cin >> a >> b;
    ll ans = a / b;
    if(a % b)
        ans += b;
    cout << ans << endl;
}

int main()
{
    fast;
    // test(t)
        solve();
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <stack>

using namespace std;

typedef vector<int> vi;
typedef vector<string> vs;

#define pb push_back
#define sz(a) (int)((a).size())
#define tr(c, it) for(typeof(c.begin()) it = c.begin(); it != c.end(); ++it)
#define fru(a, b) for(int i = a; i < b; ++i)
#define frd(b, a) for(int i = b - 1; i >= a; --i)

int main (int argc, char **argv) {
	/*freopen ("input.in", "r", stdin);
	freopen ("output.out", "w", stdout);*/

	int a, b, temp;
	cin >> a >> b;
	int count = 0;

	while (b != 1) {
		count += (a / b);
		if (a > b)
			a = a - b;
		temp = b;
		b = a;
		a = temp;
	}
	count += a;

	cout << count << endl;

	return 0;
}

#include<bits/stdc++.h>
#define lli long long int
using namespace std;
lli gcd(lli a,lli b)
{
    if(b==0)
    {
	return a;
    }
    return gcd(b,a%b);
}
int main()
{
    lli a,b,ans=0,c,d,t1,t2;
    lli x;
    cin>>a>>b;
    while(1)
    {
        if(a>b)
        {
            x=a/b;
            ans+=x;
            a-=x*b;
        }
        else
        {
            x=b/a;
            ans+=x;
            b-=x*a;
        }

        if(a==1 || b==1)
        {
            ans+=max(a,b);
            break;
        }
    }
    cout<<ans<<endl;
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
ll sum;
ll f(ll a,ll b)
{
    if(a < b) swap(a,b);
    ll d = a/b;
    ll c = a%b;
    if(c==0) return d;
    else
    {
        sum += (f(c,b)+d);
    }
    return sum;
}
int main()
{
    ll n,m;
    while(cin >> n >> m)
    {
        sum = 0;
        cout << f(n,m) << endl;
        //cout << sum << endl;
        /*ll a = n, b = m;
        while(1)
        {
            if(a < b) swap(a,b);
            ll d = a/b;
            ll c = a%b;
            if(c==0)
            {
                sum+=d;
                break;
            }
            else
            {
                sum += d;
                a = c;
            }
        }*/
        //cout << sum << endl;
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long a , b , seris = 0 , parallel1 = 0 , parellel2 = 0;
    cin >> a >> b;

    if(b==1 || a-b==1)
    {
        cout << a ;
        return 0;

    }
    if(b-a==1 || a==1)
    {
        cout << b;
        return 0;
    }
    if(a < b )
    {
        cout << (b /  a ) + (a);
    }
    else
    {   long long ans1= a /b;
         long long ans2 = a % b;
        cout << ans1 + ( ans2/  b) + ans2;
    }

    return 0;
}

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long int cal(long int a,long int b)
{ if(a==b || b==1)
return a;
else
    return (a/b + cal(b,a%b));

}
int main()
{ long long int n,m;
cin>>n>>m;
cout<<cal(n,m);
return 0;
}

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 10010000
int main(){
    int i,j,n,m,k;
    long long int a,b;
    cin>>a>>b;
    long long int res=0;
    if(a==b) res+=a;
    else {
    while(1){
        if(a==1){
            res+=b;
            break;
        }
        if(b==1){
            res+=a;
            break;
        }
      if(a>b){ res+=(a-b);
        a-=b;}
        else {
            res+=(b-a);
            b-=a;
        }
     }
    }
    cout<<res<<endl;
    return 0;
}

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{

long long a , b ,sum=0;
cin>>a>>b;
if(a%b==0)
cout<<a/b;
else if (a%b==a)
while(a)
{
sum+=b/a;
long long c=a;
a=b%a;
b=c;

}
else if (a/b>1)
sum+=a/b;
long long y=a;
a=a%b;
b=y;
while(a)
{
sum+=b/a;
int c=a;
a=b%a;
b=c;

}
if(sum>0)
    cout<<sum;
    return 0;
}

#include <cmath>

#include <climits>

#include <queue>

#include <vector>

#include <map>

#include <cstdlib>

#include <fstream>

#include <iomanip>

#include <iostream>

#include <sstream>  // istringstream buffer(myString);

#include <stack>

#include <algorithm>

#include <cstring>

#include <cassert>

using namespace std;

#define bit(x,i) (x&(1<<i))  //select the bit of position i of x

#define lowbit(x) ((x)&((x)^((x)-1))) //get the lowest bit of x

#define hBit(msb,n) asm("bsrl %1,%0" : "=r"(msb) : "r"(n)) //get the highest bit of x, maybe the fastest

#define IN(i,l,r) (l<i&&i<r) //the next for are for checking bound

#define LINR(i,l,r) (l<=i&&i<=r)

#define LIN(i,l,r) (l<=i&&i<r)

#define INR(i,l,r) (l<i&&i<=r)

#define F(i,L,R) for (ll i = L; i < R; i++) //next four are for "for loops"

#define FE(i,L,R) for (int i = L; i <= R; i++)

#define FF(i,L,R) for (int i = L; i > R; i--)

#define FFE(i,L,R) for (int i = L; i >= R; i--)

#define getI(a) scanf("%d", &a) //next three are handy ways to get ints, it's also force you to use '&' sign

#define getII(a,b) scanf("%d%d", &a, &b)

#define getIII(a,b,c) scanf("%d%d%d", &a, &b, &c)

#define wez(n) int (n); scanf("%d",&(n)) //handy if the input is right after the definition of a variable

#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m))

#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k))

#define TESTS wez(testow)while(testow--) //for multilple cases problems

#define whileZ int T; getI(T); while(T--) // the same as above

#define getS(x) scanf("%s", x) //get a char* string

#define clr(a,x) memset(a,x,sizeof(a)) //set elements of array to some value

#define char2Int(c) (c-'0')

#define lastEle(vec) vec[vec.size()-1]

#define SZ(x) ((int)((x).size()))

#define REMAX(a,b) (a)=max((a),(b)) // set a to the maximum of a and b

#define REMIN(a,b) (a)=min((a),(b));

#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) // traverse an STL data structure

#define ALL(c) (c).begin(),(c).end() //handy for function like "sort()"

#define PRESENT(c,x) ((c).find(x) != (c).end())

#define CPRESENT(c,x) (find(ALL(c),x) != (c).end())

#define ll long long //data types used often, but you don't want to type them time by time

#define ull unsigned long long

#define ui unsigned int

#define us unsigned short

#define IOS ios_base::sync_with_stdio(0); //to synchronize the input of cin and scanf

#define INF 1001001001

#define PI 3.1415926535897932384626

//for map, pair
#define pb push_back

#define mp make_pair

#define fi first

#define se second
#define MAX 1000005

int main() {
 ll a,b;
cin>>a>>b;
if(a==1 and b==1)
{cout<<"1";return 0;}
ll x=a/b;
cout<<((x)+b);

    return 0;
}

#include<iostream>
using namespace std;
long long gcd(long long n,long long m)
{
	if(n<m)
		swap(n,m);
	while(m)
	{
		int r=n%m;
		n=m;
		m=r;
	}
	return n;
}
int main()
{
	long long int a,b;
	while(cin>>a>>b)
	{
		long long int num=0;
		if(a>b)
		{
			num=a/b;
			a=a%b;
		}
		b=b/gcd(a,b);
		if(a!=0)
		num+=b;
		cout<<num<<endl;
	}
	return 0;
}

#include <iostream>
using namespace std;

long long int  solve(long long int  a,long long int  b){
    if(a%b==0) a /=b;
    if(b==1)    return a;
    if(b==0)    return 0;
    return solve(a,b/2);
}

int  main() {
    long long int  a,b;
    cin>>a>>b;
    cout<<solve(a,b);
    return 0;
}

/*
ID: espr1t
TASK:
KEYWORDS:
*/

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

using namespace std;
FILE *in; FILE *out;

const int MAX = 1024;

int main(void) {
	in = stdin; out = stdout;
//	in = fopen("A.in", "rt"); out = fopen("A.out", "wt");

	long long a, b;
	cin >> a >> b;
	cout << a / b + b << endl;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define all(sdk) sdk.begin(),sdk.end()
#define mll map<ll, ll>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define repd(i,a,b) for(ll i=a;i>=b;--i)
//#define mp make_pair
#define hell 1000000007
#define endl '\n'
#define vvll vector<vector<int> >
#define vll vector<int>
#define mint map<int,int>
#define sz(x) (int)x.size()
#define sll set<int>
#define pll pair<int,int>
#define F first
#define S second
#define INF 1e18
#define NINF -1e16

#define maxn 100005
int spf[maxn];
vll primes,prev[maxn],next[maxn];
set<int>s;
void sieve()
{
	rep(i,2,maxn)
	{
		if (spf[i]==0)
		{
			spf[i]=i;
			for(int j=2*i;j<=maxn;j+=i)
			{
				if(spf[j]==0)
					spf[j]=i;
			}
		}
	}
}
void SieveOfEratosthenes(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));

    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*2; i<=n; i += p)
                prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
    {
        if (prime[p])
        {
            primes.pb(p);
            s.insert(p);
        }
    }
}
// bool isPrime(ll n)
// {
//     if (n <= 1)
//         return false;
//     if (n <= 3)
//         return true;
//     if (n % 2 == 0 || n % 3 == 0)
//         return false;
//     for (int i = 5; i * i <= n; i = i + 6)
//         if (n % i == 0 || n % (i + 2) == 0)
//             return false;
//     return true;
// }
// ll power(ll x,ll y)
// {
//     ll X=x;
//     //X=X%p;//if x is greater than p
//     if(X==0)
//     {
//         return 0;
//     }
//     ll res=1ll;
//     while(y>0)
//     {
//         if(y&1)
//         {
//             res=((res)*(X));
//             y--;
//         }
//         y=y>>1;
//         X=((X)*(X));
//     }
//     // res=(res>=0)?(res%p):(res+p)%p;
//     return(res);
// }
// void dfs(int u, int par){
//     cnt[u] = 1;
//     p[u] = par;
//     for(int i : g[u]){
//         if(i != par){
//             dfs(i,u);
//             cnt[u] += cnt[i];
//         }
//     }
// }
// int children(vll v[],int parent,int src,vll& chil)
// {
// 	int ans =0;
// 	for(auto it:v[src])
// 	{
// 		if(it!=parent)
// 		{
// 			ans+= 1+children(v,src,it,chil);
// 		}
// 	}
// 	chil[src] = ans;
// 	return ans;

// }
bool cmp(const pair<int,int>&a,const pair<int,int>&b)
{
    if (a.F==b.F)
    return a.S<b.S;
    else
    return a.F>b.F;
}
signed main() {
	//freopen("output.txt","w",stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	srand (static_cast <unsigned> (time(0)));
// 	SieveOfEratosthenes(100005);
// 	sort(all(primes));
    sieve();
	int t=1;
	//cin >> t;
	while (t--)
	{
	    double a,b;
	    cin >> a>> b;
	    if (a<b)
	    swap(a,b);
	    double d=a/b;
	    if (ceil(d)==d)
	    {
	        cout << d<<endl;
	        return 0;
	    }
	    else if (d>1)
	    {
	        double res=floor(d);
	        d-=floor(d);
	        double x=1/d;
	        res+=x;
	        //cout <<d<<" "<<x<<endl;
	        cout <<res<<endl;
	    }
	}
	return 0;
}

#include <cstdio>
#include <cstring>

using namespace std;

long long a, b, t, res;

int main() {
    scanf("%lld %lld", &a, &b);
    while (b) {
        res += (a / b);
        t = a % b;
        b = t;
        a = b;
    }
    printf("%lld\n", res);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main(){
    long long int a , b , c , h ;
    cin >> a >> b ;
    c = a/b ;
    if(b==1)
     cout << c << endl;
    else {
        a = a - (a/b)*b ;
        //cout << a <<" "<< b <<"\n";
        while(a%b!=0){
            h = a ;
            a = b-a ;
            b = h ;
            //cout << a <<" "<< b <<"\n";
            //c++;
            if(a/b>1){
                c+=(a/b);
                a = a- (a/b)*b;
            }else
                c++;
            if(a<0)
                break;
        }
        //cout<< a << " " << b << "\n";
        cout << c+a << endl;

    }
}
//5 / 8

/*
 * ps.cpp
 * Author: Ali Ahmed Eltoney
 */

#include <bits/stdc++.h>

using namespace std;

#define el				'\n'
#define sz(v)			((int)((v).size()))
#define all(v)          ((v).begin()),((v).end())
#define clr(v, d)		memset(v, d, sizeof(v))
#define read(v)         freopen(v, "rt", stdin);
#define write(v)        freopen(v, "w", stdout);
#define fastIO          cout << fixed << setprecision(0), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

double const EPS = 1e-8, PI = acos(-1);
const int N = 1e5 + 9, M = 1e6 + 1, OO = 1e6 + 1;
const long long MOD = 1e9 + 7;

typedef long long ll;

int main() {
	fastIO;
//	read("input.txt")
//	read("mosalah.in")
	ll a,
	b, ans = 0;
	cin >> a >> b;
	ans += a / b;
	a -= ans * b;
	if (a)
		ans += b;
	cout << ans;

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
//
int main() {
long long int a,b,c=0,t;
cin>>a>>b;
while(1)
{//cout<<a;
    if(a<=1)
    {
        cout<<(c+b);
        break;
    }
    if(a>=b)
    {
        c+=(a/b);
        a=a%b;
    }
    else
    {
        t=a;
        a=b;
        b=t;
    }
}

return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll a,b;cin>>a>>b;
    if(a<b)
    {
        cout<<b;
    }
    else
    {
        //cout<<a/b<<endl;
        cout<<(a/b)+abs(a-b*(a/b));
    }
}

#include <bits/stdc++.h>
using namespace std ;

#define int long long

int32_t main(){
	int a , b ;
	cin >> a >> b ;
	int ans = a/b ;
	a %= b ;
	if(a){
		ans += min(a,(b-a)) * b ;
	}
	cout << ans ;
	return 0 ;
}

/*input
2 3
*/
#include <bits/stdc++.h>
#define pb push_back
#define epb emplace_back
#define mp make_pair
#define pr pair<int,int>
#define all(x) x.begin(),x.end()
#define rt return
#define f(x,y,z) for(x=y;x<z;++x)
#define rev(x,y,z) for(x=z-1;x>=y;--x)
#define pf(x) printf("%d\n",x)
#define pfs(s) printf("%s\n",s)
#define out(s) cout<<s<<"\n"
#define in(s) cin>>s
#define st string
#define X first
#define Y second
using namespace std;
typedef long long ll;typedef vector<int> vi;typedef vector<ll> vl;typedef vector<bool> vb;
ll modpow(ll x, ll y, ll m){ll z=1;while(y){if(y&1)z=(z*x)%m;x=(x*x)%m;y>>=1;}return z;}
int egcd(int a, int b, int& x, int& y){if(a == 0){x= 0;y= 1;return b;}int x1, y1;int gcd= egcd(b%a, a, x1, y1);x= y1 - (b/a) * x1;y= x1;return gcd;}
string z[2]={"NO","YES"};
ll mod=1e9+7;
// vector<vi> adj;vb v(1e5);
// void dfs(int s)
// {
//     v[s]=1;
//     int i;
//     f(i,0,adj[s].size())
//     {
//         if(!v[adj[s][i]])
//         {
//             dfs(adj[s][i]);
//         }
//     }
// }
int t=0;
void euclid(ll a,ll b)
{
    t+=b;
    if(a%b==0)
        return;
    return euclid(b,a%b);
}
void te()
{
    int i,j,n;
    ll a,b;
    in(a>>b);
    euclid(max(a,b),min(a,b));
    out(t);
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int t=1;
    while(t--)te();
    rt 0;
}

/*
* I'm a Ghost
* Now you see me, now you don't
*/
#include<bits/stdc++.h>
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define mod 1000000007
#define inf ((ll)1e18)
#define eps 1e-9
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
using namespace std;
int main()
{
    // fast();
    ll a , b;
    cin>>a>>b;
    ll ans = a / b;
    a = a % b;
    if(a)
    ans += ((b % a) * a + (b / a));
    cout<<ans<<endl;

}

#include <bits/stdc++.h>

#pragma GCC optimize("O2")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
using namespace std;
#define ll long long
#define rep(i,n,N) for(int i=n;i<=N;i++)
#define rap(i,n,N) for(int i=(int)n;i>=(int)N;i--)
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define sf se.fi
#define ss se.se
#define pii pair<int,int>
#define pll pair<ll,ll>
#define piii pair<int,pii>
#define piiii pair<piii,pii>
#define endl "\n"
const ll MAX=1e5+5;
const ll MAX2 = 11;
const int MOD=1000000000 + 7;
const ll INF=20000;
const int nr[]={1,0,-1,0,1,1,-1,-1};
const int nc[]={0,1,0,-1,1,-1,1,-1};

ll x,y;

int main(){
	cout<<fixed<<setprecision(10);
    //freopen("input.txt", "r", stdin);
	//freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin>>x>>y;
    x/=y;
    cout<<y+x;
    return 0;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll val;
ll ans(ll a,ll b)
{

    if(a>b)
        swap(a,b);
    if(a==1)
        return b;
    val+=b/a;
    val+=ans(a,b%a);
    return val;
}
int main()
{
    ll a,b;
    cin>>a>>b;
    cout<<ans(a,b);
}

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr first
#define sc second
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll a,b,r=0;
    cin>>a>>b;
    if(a==b) cout<<a;
    else if(a>b)
    {
    	r=a/b;
    	a=a%b;
    	if(a+1==b)
		{
			r=r+b;
    		cout<<r;
    	}
	}
	else if(a<b)
	{
		if(a+1==b || a==1) cout<<b;
	}
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

long long a, b, ans;

int gcd(int x, int y)
{
	return (y == 0)?x :gcd(y,x%y);
}

int solve(int x, int y)
{
	if (y == 1) return x;

	int newx = y;
	int newy = x%y;

	int temp = gcd(newx, newy);

	return 1 + solve(newx/temp, newy/temp);
}

int main()
{
	cin >> a >> b;

	ans += (a / b);
	a %= b;
	if (a > 0)
	{
		int temp = gcd(b,a);
		ans += solve(b / temp, a / temp);
	}

	cout << ans << endl;

	return 0;
}

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
long long gcd(long a,long b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
long long d1,d2;

int main()
{
    while(cin>>d1>>d2)
    {
        long long ans=0,temp,c,k;
        while(d1&&d2)
        {
            c=gcd(d1,d2);
            if(c)
            {
                d1=d1/c;d2=d2/c;
                k=d1/d2;
                ans+=k;
                temp=d2;
                d2=d1-d2*k;
                d1=temp;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
typedef long double ld; //fixed and precision
const ll INF = 2e18;

// Searching an element in Calkin–Wilf tree

ld solve(ll a, ll b) {
	if(a < b) swap(a, b);
	if(!b) return 0;
	return (a / b) + solve(b, a % b);
}

int main() {
	FAST;
	ll a, b, i, d = 0; cin >> a >> b;
	cout << solve(a, b);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long a,b;
    scanf("%I64d%I64d",&a,&b);
    if(a%b==0){
        printf("%I64d",a/b);
    }else if(b%a==0){printf("%I64d",b/a);}
    else{
        printf("%I64d",(max(a,b)-1)/min(a,b)+min(a,b));
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a,b;
    cin>>a>>b;
    int cnt = 0;
    while(b)
    {
        cnt+=a/b;
        long long temp = a%b;
        a = b;
        b = temp;
    }
    cout<<cnt;
}

//By Brickgao
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
#define out(v) cerr << #v << ": " << (v) << endl
#define SZ(v) ((int)(v).size())
#define LL long long
const int maxint = -1u>>1;
template <class T> bool get_max(T& a, const T &b) {return b > a? a = b, 1: 0;}
template <class T> bool get_min(T& a, const T &b) {return b < a? a = b, 1: 0;}

LL a, b;
LL ans = 0;

int main() {
    cin >> a >> b;
    ans += (LL)(a / b);
    a = a % b;
    if(a == 0)
        cout << ans << endl;
    else {
        LL tmp1 = a, tmp2 = b;
        while(tmp1 == 1) {
            swap(tmp1, tmp2);
            ans += tmp1 / tmp2;
            tmp2 %= a;
        }
        ans += tmp2;
        cout << ans << endl;
    }
    return 0;
}

#include <stdio.h>
int main()
{
    long long a,b;
    scanf("%lld%lld",&a,&b);
    if(a==b)
    {
        printf("1");
        return 0;
    }
    long long n=a/b;
    printf("%I64d",b+n);
}

#include <cstdio>
long long gcd(long long a,long long b){
    while(a&&b)a>b?a%=b:b%=a;
    return a+b;
}
int main(){
    long long a,b;
    scanf("%I64d %I64d",&a,&b);
    long long k=gcd(a,b);
    a/=k;b/=k;
    printf("%I64d",a>b?a:b);
    return 0;
}

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

int F(long long int step, long long int x, long long int y, bool &flag) {
    cout << x << " " << y << endl;
    if (x == 1 && y == 1) {
        cout << step;
        flag = 1;
    }
    if (!flag) {
        long long int a1 = x, b1 = y - x, b2 = y, a2 = x - y;
        if (a1 > 0 && b1 > 0) {
            b1 = y - y / x * x;
            if (b1 == 0) b1 += x;
            F(step + (y / x), a1, b1, flag);
        }
        if (a2 > 0 && b2 > 0) {
            a2 = x - (x / y) * y;
            if (a2 == 0) a2 += y;
            F(step + (x / y), a2, b2, flag);
        }
    }
}

int main() {
    long long int a, b;
    cin >> a >> b;
    bool flag = 0;
    unsigned long long int step = 0;
    F(step, a, b, flag);
}

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define speedX ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
using namespace std;
// #define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.first); __p(a.second); cout << "}"; }
template<typename T>
void __f(const char* s, T t) { cout << s << " : "; __p(t); }
template<typename T>
void __t(const char* s, const T &x) { cout << s << " : { "; for (auto it : x) __p(it); cout << "} ";}
template<typename T, typename ... F>
void __f(const char* s, T t, F ... f) {
	int i = 0; for (;; ++i) if (s[i] == ',') break;
	cout.write(s, i) << " : "; __p(t); cout << " | "; __f(s + i + 1, f...);
}
#define trace(...) { cout<<"LINE: "<<__LINE__<<" || "; __f(#__VA_ARGS__,__VA_ARGS__); cout<<"\n\n"; }
#define debug(...) { cout<<"LINE: "<<__LINE__<<" || "; __t(#__VA_ARGS__,__VA_ARGS__); cout<<"\n\n"; }
int begtime = clock();
#define end_time() cout << "\n\nTime of execution: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define trace(...)
#define debug(...)
#define end_time()
#endif

#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;
typedef vector<pii> vii;

inline bool equals(ld a, ld b) {return fabs(a - b) < 1e-9;}
inline int gcd(int a, int b) { a=abs(a); b=abs(b); while(a>0 && b>0) (a>b?a%=b:b%=a); return (a==0?b:a); }
inline int power(int x, int n, int m = LLONG_MAX)
    {  int res=1; x = (x%m + m)%m;
       while(n) { if(n&1) res=(res*x)%m;  x=(x*x)%m;  n >>= 1; }  return res;
    }
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define mod 1000000007

void solve(){
	int a,b;
	cin>>a>>b;
	int ans = a/b;
	a %= b;
	if(a!=0){
		int d = gcd(a,b);
		a/=d;
		b/=d;
		ans+=b;
	}
	cout<<ans<<"\n";
}
signed main(){
	speedX;
	#ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
	int t=1;
	//cin>>t;
	for(int i=1;i<=t;++i) {
		//cout<<"Case #"<<i<<": ";
		solve();
	}
	end_time();
	return 0;
}

#include<iostream>
using namespace std;
__int64 cal(__int64 a,__int64 b);
int main()
{__int64 a,b,m;
cin>>a>>b;
 m=cal(a,b);
 cout<<m;
 return 0;
}
__int64 cal(__int64 a,__int64 b)
{__int64 s=0;
  if(a==b)
  s+=a;
  else
  {if(a>b)
  s=s+1+cal(a-b,b);
  else
  {if(b-a==1)
      s=s+a;
      else
      s=s+1+cal(b-a,b);}}
return s;
}

#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define FF first.first
#define FS first.second
#define pb push_back
using namespace std;
const ll N=1000006, INF=1e16, MOD=998244353;

ll n, q=1, a[N], f[N], m, x, y, z, o, h, w, mx, mn, ans=INF, Ans;
string s, S;
vector <ll> v[N];
pair<ll, ll> p[N];

main(){ios_base::sync_with_stdio(false), cin.tie(0);

    //cin>>q;

    while(q--){ans=0;

    	cin>>n>>m;

    	cout<<m+n/m;

    }

}

#include <bits/stdc++.h>

#define       ll         long long

using namespace std;

ll ans;

ll gcd(ll a, ll b)
{
	if(b>a) swap(a, b);

	if(b==0) return ans;
	else
	{
		ans++;
		if((a%b)==1) return ans+b;
		else return gcd(b, a%b);
	}
}

int main()
{
	ll a, b;

	cin >> a >> b;

	cout << gcd(a, b) << endl;

	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define fast ios::sync_with_stdio(false); cin.tie(NULL);
const double eps = 1e-6;

/////////////////////////////////////////////

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

////////////////////////////////

#define int long long

void solve(){
	int a,b,i,j;
	cin>>a>>b;
	if(a<=b){
		cout<<b;
		return;
	}
	cout<< a/b + b;
}

signed main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    fast;
    int t = 1;
    //cin>>t;
    while(t--)
        solve();
}

#include <bits/stdc++.h>
using namespace std;

#define sz(v)               ((int)v.size())
#define ll                  long long
#define all(v)              (v.begin()) , (v.end())
#define rall(v)             (v.rbegin()) , (v.rend())

int main ()
{
    //freopen("input.txt", "r", stdin);
    int a, b, ans = 0;
    cin >> a >> b;

    ans = a / b;
    a %= b;

    while(b != 1){
        ans++;
        int tmp = b - a;
        b = a;
        a = tmp;
    }
    ans += a / b;

    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

const int MAX = 200010;
const int MOD = 1000000007;
const int INF = 1e8;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
//

int main() {
    int a, b;
    cin >> a >> b;

    int n = 0;
    while(a) {
        n += (a/b);
        a = a%b;
        if(a) swap(a, b);

        // cout << "n = " << n << " a = " << a << " b = " << b << endl;
    }

    cout << n << endl;

    return 0;
}

#include <iostream>
#include <cmath>
#define INF 111222333444555666
using namespace std;
typedef long long i64;

i64 solve(int num, int dem) {
	if (dem == 1)
		return num;
	if (num < 0 || dem < 0)
		return INF;
	return 1 + min(solve(num - dem, dem), solve(num, dem - num));
}

int main() {
	i64 x, y;
	cin >> x >> y;
	i64 ans = solve(x, y);
	cout << ans << endl;
	return 0;
}

#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld double
#define rep(i,a,b) for(ll i=a; i<b; ++i)
#define BOOST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define all(v) v.begin(),v.end()
#define sl(a) a.size()
#define mod 1000000007
#define lcm(a,b) (a*b)/__gcd(a,b)
#define MAX1 100006
#define f first
#define s second
using namespace std;
void solve()
{
    ll a,b,c=0;  cin>>a>>b;
    if(a==1 and b==1)  {  cout<<"1";  return;  }
    if(a>b) { c+=(a/b);  a-=c*b;  }
    if(a==1) c+=b;
    else  c+=(a+a/b);
    cout<<c;
}
int main()
{
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt","r",stdin);
    // for writing output to output.txt
    freopen("output.txt","w",stdout);
    #endif
    BOOST
    int T=1;  //cin>>T;
    while(T--) solve();
    return 0;
}

// KazamaHoang //

#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define FORD(i,a,b) for(int i = (a); i >= (b); i--)
#define F first
#define S second

using namespace std;

long long a,b;

void Nhap()
{
    cin>>a>>b;
}

void Tinh()
{
    int dem=0;
    if (a<b) swap(a,b);
    while(true)
    {
        dem+=a/b;
        a=a%b;
        if (a==0) break;
        swap(a,b);
    }
    cout<<dem;
}

int main()
{
    ios_base::sync_with_stdio(0);
//    freopen("INP.TXT","r",stdin);
//    freopen("OUT.TXT","w",stdout);
    int q=1;
    while(q--)
    {
        Nhap();
        Tinh();
    }
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <math.h>
#include <algorithm>

using namespace std;

long long gcd(long long a, long long b) {
    return a % b == 0 ? b : gcd(b, a % b);
}

long long f(long long a, long long b) {
    if(a != 0 && b != 0) {
        long long gg = gcd(a, b);
        a /= gg; b /= gg;
    }
    long long k = a / b;
    a = a - k * b;
    if(a == 0) return k;
    return k + min(a * b, 1 + f(b - a, a));
}
int main() {
    long long a, b;
    cin >> a >> b;
    long long ans = f(a, b);
    cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
long long a,b;
long long ans,nd;
long long nod(long long x,long long y)
{
    long long r;
    while(y!=0)
    {
        r=x%y;
        x=y;
        y=r;
    }
    return x;
}
int main()
{
    cin>>a>>b;
    ans=a/b;
    cout<<max(a,b)<<endl;
}

#include "bits/stdc++.h"
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long double db;
typedef long long ll;
typedef pair<db, db> pd;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
tree_order_statistics_node_update> ordered_set;

#define printlist(ans) for(auto x: ans) cout << x << " "; cout << endl;
#define printstring(ans)  for(auto x: ans) cout << x; cout << endl;

#define mod 1000000007
#define maxn 100005
#define pi 3.141592653589

ll tst;
void cleanData();

ll n, m, k, a[maxn], b[maxn];

void solve() {
	ll a, b;
	cin >> a >> b;
	cout << max(a, b);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	tst = 1;
	//cin >> tst;
	while (tst--) {
		solve();
	}

	cout << endl << endl;
	return 0;
}

void cleanData() {}

#include <iostream>
using namespace std;

int gcd(int a, int b){
//if(b==0)  return a;
return b==0?a:gcd(b,a%b);
}
inline int mini(int a,int b){return a<b?a:b;}
inline int maxi(int a,int b){return a>b?a:b;}

int  solve(int  a,int  b){
int sol ,g;
g = gcd(maxi(a,b),mini(a,b));
sol = maxi(a,b)/g;
return sol;
}

int  main() {
    int  a,b;
    cin>>a>>b;
    cout<<solve(a,b);
    return 0;
}

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define LL long long
LL n,m;
int main()
{
	while(scanf("%lld%lld",&n,&m)!=-1)
	{
		LL ans=0;
		while(n>1&&m>1)
		{
			if(n<m)
			{
				ans=m/n;
				m=m%n;
			}
			else if(n>m)
			{
				ans=n/m;
				n=n%m;
			}
			if(m==n)n=1,m=1;
		}
		if(n==1)ans+=m;
		else if(m==1) ans+=n;
		printf("%lld\n",ans);
	}
	return 0;
}

// .... .... .....!
// P..... C.....!

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cassert>

#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <numeric>

#include <cstdio>
#include <cstring>

using namespace std;

#define rep(i, n) for (int i = 0, _n = (int)(n); i < _n; i++)
#define fer(i, x, n) for (int i = (int)(x), _n = (int)(n); i < _n; i++)
#define rof(i, n, x) for (int i = (int)(n), _x = (int)(x); i-- > _x; )
#define fch(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define sz(x) (int((x).size()))
#define pb push_back
#define mkp make_pair
#define all(X) (X).begin(),(X).end()

#define X first
#define Y second

typedef long long ll;
typedef pair <int, int> pii;

////////////////////////////////////////////////////////////////////////////////

ll f (ll a, ll b)
{
	ll x = a / b;
	a %= b;
	if (a == 0) return x;
	if (b % a == 0) return b/a+1;
	return x + b / a + f (a, b % a);
}

int main()
{
	ios_base::sync_with_stdio (false);

	ll a, b;
	cin >> a >> b;
	cout << f (a, b) << endl;

	{ int _; cin >> _; return 0; }
}

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <cmath>
#include <ctime>
#include <cctype>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cassert>

using namespace std;

#define prt(x) cout<<#x<<":"<<x<<" "
#define prtn(x) cout<<#x<<":"<<x<<endl
#define shut assert(0)
#define when printf("%.2f\n",1.0*clock()/CLOCKS_PER_SEC)
#define rep(i,s,t) for(int i=s,i##_=t;i<i##_;++i)
#define per(i,s,t) for(int i=(t)-1,i##_=s;i>=i##_;--i)
#define inf (1<<30)
#define INF (1LL<<62)
#define sqr(x) ((x)*(x))
#define pb push_back
#define NOO puts("-1"),0

typedef long long ll;
typedef double db;
typedef vector<int> VI;
typedef vector<VI> MT;
struct ii
{
	int x,y;
	ii(int x=0,int y=0):x(x),y(y){}
	bool operator<(const ii &a)const{if(x!=a.x)return x<a.x;return y<a.y;}
	bool operator==(const ii&a)const{return x==a.x&&y==a.y;}
	ii friend operator+(ii a,ii b){return ii(a.x+b.x,a.y+b.y);}
	ii friend operator-(ii a,ii b){return ii(a.x-b.x,a.y-b.y);}
};

//ll memory freopen

template<class T>void sc(T &x)
{
	x=0;char c;int f=1;
	while(c=getchar(),c<48)if(c=='-')f=-1;
	do x=x*10+(c^48);
	while(c=getchar(),c>47);
	x*=f;
}
template<class T>void nt(T x)
{
	if(!x)return;
	nt(x/10);putchar(x%10+'0');
}
template<class T>void pt(T x)
{
	if(x<0)putchar('-'),x=-x;
	if(!x)putchar('0');else nt(x);
}
template<class T>void pts(T x)
{
	pt(x);putchar(' ');
}
template<class T>void ptn(T x)
{
	pt(x);putchar('\n');
}
template<class T>void pp(T x,int y)
{
	pt(x);putchar(" \n	"[y]);
}
template<class T>void PP(T *x,int y)
{
	rep(i,0,y)pp(x[i],i==y-1);
}
template<class T>void Max(T &x,T y){if(x<y)x=y;}
template<class T>void Min(T &x,T y){if(x>y)x=y;}

/*	template ends here	*/

int main()
{
//	freopen("pro.in","r",stdin);
//	freopen("chk.out","w",stdout);
	ll a,b;
	sc(a);sc(b);
	ll ans=a/b;
	if(a%b)ans+=b;
	ptn(ans);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll a,b;
    cin>>a>>b;
    if(a%b==0){
        cout<<"1";
        return 0;
    }
    cout<<max(a,b);
}

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

ll n , d , q , answer;

int main()
{
    cin >> n >> d;
    q = n / d;
    answer += q;
    if(q * d != n)answer += d;
    cout<<answer;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll a,b;cin>>a>>b;
    ll m=a/b;

a=a-m*(b);//cout<<m<<" "<<a<<" "<<b;
if(a!=0)
{
cout<<b+m;}
else{cout<<m;}
}

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    if (b<a)
    swap(a,b);
    int val = a/b;
    val += b;
    cout<<val;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main()
{
	long long a, b, g, res=0;
	cin>>a>>b;
	res+=(a/b), a%=b;
	if(a) g=__gcd(a,b), res+=(b/g);
	cout<<res;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
typedef int64_t ll;
const int sz = 1e7+4;
template<typename T, typename U> static inline void amin(T &x, U y){if (y < x)x = y;}
template<typename T, typename U> static inline void amax(T &x, U y){if (x < y)x = y;}

void solve(){
    ll a,b;cin>>a>>b;
    if(b==1)cout<<a<<"\n";
    else cout<<b+floor(a/b)<<"\n";
}
int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);

    //int tc;cin>>tc;while(tc--)
    solve();

    return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define REP(i,n) for(int i=0;i<n;i++)
#define REPS(i,a,b) for(int i=a;i<b;i++)
#define ii pair<ll,ll>
#define vi vector<int>
#define vii vector<ii>
#define TRvii(v,it) for(vii::iterator it=v.begin();it!=v.end();++it)
#define TRvi(v,it) for(vi::iterator it=v.begin();it!=v.end();++it)
#define gc getchar_unlocked
#define pc putchar_unlocked
#define cin fin
using namespace std;

ifstream fin("input.cpp");

ll gcd(ll x,ll y)
{
    if(y==0)return x;
    return gcd(y,x%y);
}
ii simplify(ii x)
{
    ll num(x.first),den(x.second);
    return ii(num/gcd(num,den),den/(gcd(num,den)));
}

ll ans(ii p)
{
    p=simplify(p);
    ll a=p.first,b=p.second;
    if(a==0 || b==0)return 0;
    if(b==1)return a;
    if(a<b)
        return b/a+ans(ii(a,b-a*(b/a)));
    else
        return a/b+ans(ii(a-(a/b)*b,b));
}

int main()
{
    ii p;
    cin>>p.first>>p.second;
    cout<<ans(p)<<endl;
}

#include <bits/stdc++.h>
// # define _DEBUG

using namespace std;

struct compare{
    bool operator()(const int64_t & a, const int64_t & b){
        return a>b;
    }
};

bool customS(pair<int64_t, int> a, pair<int64_t, int> b){
    // if(a.first==b.first)
    //     return a.second < b.second;
    return a.first < b.first;
}

bool customS2(pair<int64_t, int64_t> a, pair<int64_t, int64_t> b){
    // if((a.first-a.second)==(b.first-b.second))
    //     return a.second < b.second;
    return a.second<b.second;
}

class CompareDist
{
public:
    bool operator()(pair<int64_t, int64_t> a, pair<int64_t, int64_t> b){
        return a.first < b.first;
    }
};

int64_t gcd(int64_t a, int64_t b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

double gcdfloat(double a, double b) {
    if (a < b)
        return gcd(b, a);

    if (fabs(b) < 0.001)
        return a;

    else
        return (gcd(b, a - floor(a / b) * b));
}

// vector<vector<int64_t> > nodes(500001);
// vector<int64_t> visited(500001, 0);
// void DFS(int64_t start){
//     visited[start] = 1;
//     for(int i=0;i<nodes[start].size();i++){
//         if(visited[nodes[start][i]]==0){
//             DFS(nodes[start][i]);
//         }
//     }
// }

bool DFS(int64_t x, int64_t y, vector<string> &mat, vector<string>&visited){
    int64_t n=visited.size(), m=visited[0].size();
    visited[x][y] = '1';

    bool val=false;

    if(x == n-1 && y == m-1)
        val = true;

    int dx[4] = {0, 0, -1, +1};
    int dy[4] = {1, -1, 0, 0};

    for(int i=0;i<4;i++){
        int a = x+dx[i], b = y+dy[i];
        if(a>=0 && a < n && b>=0 && b<m){
            if(visited[a][b] == '0'){
                if(mat[a][b]!='#'){
                    val |= DFS(a, b, mat, visited);
                }
            }
        }
    }
    return val;
}

int64_t LCA(int64_t a, int64_t b){
    while(a!=b){
        // cout<<a<<" "<<b<<"\n";
        if(b>a){
            swap(a, b);
        }
        a /= 2;
    }
    return a;
}

vector<int64_t> ra(int64_t n){vector<int64_t> arr(n);for(int i=0;i<n;i++)cin>>arr[i];return arr;}

int64_t binary_search_lower(vector<int64_t> &arr, int64_t n){
    int64_t low = 0, high = arr.size()-1, mid, ans=-1;
    while(low<=high){
        mid = (low+high)/2;
        if(arr[mid]<=n){
            ans = mid;
            low = mid+1;
        }
        else{
            high = mid-1;
        }
    }
    return ans;
}

int64_t binary_search_upper(vector<int64_t> &arr, int64_t n){
    int64_t low = 0, high = arr.size()-1, mid, ans=-1;
    while(low<=high){
        mid = (low+high)/2;
        if(arr[mid]>=n){
            ans = mid;
            high = mid-1;
        }
        else{
            low = mid+1;
        }
    }
    return ans;
}

int main(){

    #ifdef _DEBUG
    	// freopen("input.txt", "r", stdin);
    	// freopen("output.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // int trial;
    // cin>>trial;
    // while(trial--){
    int64_t i, j, a=0, b=0, c=0, d=0, n, m, k, sum=0, x=0, y=0, z=0, flag=0, zero=0, ans=0;
    int64_t mod=1000000007;
    string s, newS, s2;
    double dn;
    char ch;

    cin>>a>>b;
    if(b==1){
        cout<<a<<" ";
        return 0;
    }
    while(a!=1){
        if(a>b){
            ans += 1;
            a = a%b;
        }
        else{
            ans += 1;
            x = a;
            y = b-a;
            a = y, b = x;
        }
    }

    ans += b;
    cout<<ans<<"\n";
    // }
    return 0;
}

#include <fstream>
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <list>
#include <queue>
#include <string>
#include <cmath>
#include <map>
#include <stack>

using namespace std;
typedef long long ll;
const ll mod = 1000000007, INF = 2000000001;
int dx[] = {0,1,0,-1, 1,1,-1,-1, 0}, dy[] = {-1,0,1,0, -1,1,1,-1, 0};
const double EPS = 1e-9;

ll gcd(ll a, ll b)
{
    return (b != 0) ? gcd(b, a % b) : a;
}

ll modpow(ll a, ll x, ll m = mod)
{
	int p = 1;
	while (x > 0)
	{
		if (x & 1 == 1)
		{
			p = p * a % m;
			--x;
		}
		else
		{
			x >>= 1;
			a = a * a % m;
		}
	}
	return p;
}

void z(const string & s, vector<int> & v)
{
	int n = s.length();
	for (int i = 1, l = 0, r = 0; i < n; ++i)
	{
		if (i <= r)
			v[i] = min(v[i-l],r-i+1);
		while (i + v[i] < n && s[v[i]] == s[i+v[i]])
			++v[i];
		if (i + v[i] - 1 > r)
			r = (l = i) + v[i] - 1;
	}
}

bool dfs(vector< vector<string> > & states, int sx, int sy, int n)
{
	bool ans = false;
	if (n == 8)
		ans = true;
	else if (states[n][sy][sx] == 'S')
		ans = false;
	else
		for (int d = 0; d < 9 && !ans; ++d)
		{
			int x = sx + dx[d], y = sy + dy[d];
			if (min(x,y) >= 0 && max(x,y) < 8)
			{
				if (states[n][y][x] == '.')
				{
					ans = dfs(states,x,y,n+1);
				}
			}
		}
	return ans;
}

void _a()
{
	ll a, b;
	cin >> a >> b;
	cout << (b == 1 ? a : b + a / b);
}

void _b()
{
	string s;
	cin >> s;
	vector<char> v(100000,'?');
	int i = 0;
	int l = 0;
	while (l < s.length())
	{
		int r = l;
		for (r = l; r < s.length() && s[r+1] == s[l]; ++r)
			;
		if ((r - l + 1) % 2 == 1)
		{
			v[i] = s[r];
			if (i > 0 && v[i] == v[i-1])
				--i;
			else
				++i;
		}
		l = r + 1;
	}
	cout << (i == 0 ? "Yes" : "No");
}

void _c()
{
	string s, a = "No such line.";
	ll k, n;
	cin >> s >> k;
	n = s.length();
	if (k <= n * (n + 1) / 2)
	{
		a = "";
		map< char, set<int> > m;
		for (int i = 0; i < s.length(); ++i)
			m[s[i]].insert(i+1);
		int cnt = 0;
		while (cnt != k)
		{
			char ch = '?';
			for (char c = 'a'; c <= 'z'; ++c)
			{
				int d = 0;
				for (set<int>::iterator it = m[c].begin(); it != m[c].end(); ++it)
					d += (n - *it + 1);
				if (cnt + d >= k)
				{
					a += c;
					ch = c;
					if (cnt + m[c].size() < k)
						cnt += m[c].size();
					else
						cnt = k;
					break;
				}
				else
					cnt += d;
			}
			if (cnt == k)
				break;
			for (char c = 'a'; c <= 'z'; ++c)
				if (c != ch)
					m[c] = set<int>();
			set<int> ss;
			for (set<int>::iterator it = m[ch].begin(); it != m[ch].end(); ++it)
				if (*it < n)
				{
					if (s[*it] != ch)
						m[s[*it]].insert(*it+1);
					else
						ss.insert(*it+1);
				}
			m[ch] = ss;
		}
	}
	cout << a;
}

void _d()
{
	int n;
	cin >> n;
	vector<int> a(n);
	set< pair<int,int> > s;
	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
		set< pair<int,int> >::iterator it = s.lower_bound(make_pair(a[i],0));
		if (it != s.end() && it->first == a[i])
		{
			s.insert(make_pair(a[i],it->second+1));
			s.erase(it);
		}
		else
			s.insert(make_pair(a[i],1));
	}
	bool ans = true;
	sort(a.begin(),a.end());
	for (int i = 1; i < n && ans; ++i)
		ans = (a[i] - a[i-1] <= 1);
	if (ans)
	{
		set< pair<int,int> >::iterator fit = s.begin(), sit = fit;
		++sit;
		for ( ; sit != s.end() && ans; )
		{
			int d = sit->second - fit->second;
			if (d > 0)
			{
				pair<int,int> p = make_pair(sit->first,d);
				s.erase(fit);
				s.erase(sit);
				s.insert(p);
				fit = s.find(p);
				sit = fit;
				++sit;
			}
			else if (d == 0)
			{
				if (++sit != s.end())
					ans = false;
				else
					--sit;
				break;
			}
			else
				ans = false;
		}
		if (fit != s.end() && sit == s.end())
			ans = false;
	}
	cout << (ans ? "YES" : "NO");
}

void build(vector< vector< pair<int,ll> > > & t, const vector<ll> & a, int v, int tl, int tr)
{
	if (tl == tr)
	{
		for (int i = 0; i < 20; ++i)
			t[i][v].second = (a[tl] & (1 << i)) >> i;
	}
	else
	{
		int tm = (tl + tr) / 2;
		build(t,a,2*v,tl,tm);
		build(t,a,2*v+1,tm+1,tr);
		for (int i = 0; i < 20; ++i)
			t[i][v].second = t[i][2*v].second + t[i][2*v+1].second;
	}
}

void push(vector< vector< pair<int,ll> > > & t, int v, int tl, int tr)
{
	for (int i = 0; i < 20; ++i)
		if (t[i][v].first == 1)
		{
			t[i][2*v].first = (t[i][2*v].first + 1) % 2;
			t[i][2*v+1].first = (t[i][2*v+1].first + 1) % 2;
			t[i][v].first = 0;
			ll tm = (tl + tr) / 2;
			t[i][2*v].second = tm - tl + 1 - t[i][2*v].second;
			t[i][2*v+1].second = tr - tm - t[i][2*v+1].second;
		}
}

ll get_sum(vector< vector< pair<int,ll> > > & t, int v, int tl, int tr, int l, int r)
{
	ll a = 0;
	if (l <= r)
	{
		if (tl == l && tr == r)
		{
			for (int i = 0; i < 20; ++i)
				a += t[i][v].second * (1 << i);
		}
		else
		{
			push(t,v,tl,tr);
			int tm = (tl + tr) / 2;
			a = get_sum(t,2*v,tl,tm,l,min(r,tm)) + get_sum(t,2*v+1,tm+1,tr,max(l,tm+1),r);
		}
	}
	return a;
}

void update(vector< vector< pair<int,ll> > > & t, int v, int tl, int tr, int l, int r, ll x)
{
	if (l <= r)
	{
		if (tl == l && tr == r)
		{
			//push(t,v,tl,tr);
			for (int i = 0; i < 20; ++i)
			{
				t[i][v].first = (t[i][v].first + 1) % 2; //(x & (1 << i)) >> i;//,x^t[v].second);
				if (t[i][v].first == 1)
					t[i][v].second = tr - tl + 1 - t[i][v].second;
			}
		}
		else
		{
			push(t,v,tl,tr);
			int tm = (tl + tr) / 2;
			update(t,2*v,tl,tm,l,min(r,tm),x);
			update(t,2*v+1,tm+1,tr,max(l,tm+1),r,x);
			for (int i = 0; i < 20; ++i)
				t[i][v].second = t[i][2*v].second + t[i][2*v+1].second;
		}
	}
}

void _e()
{
	int n;
	scanf("%d",&n);
	vector<ll> a(n);
	for (int i = 0; i < n; ++i)
		scanf("%I64d",&a[i]);
	vector< vector< pair<int,ll> > > tree(20,vector< pair<int,ll> >(5*n,make_pair(0,0)));
	build(tree,a,1,0,n-1);
	int m;
	scanf("%d",&m);
	while (m--)
	{
		int t;
		scanf("%d",&t);
		if (t == 1)
		{
			int l, r;
			scanf("%d %d",&l,&r);
			/*ll s = 0;
			for (int i = l - 1; i < r; ++i)
				s += a[i];
			printf("%I64d\n",s);*/
			printf("%I64d\n",get_sum(tree,1,0,n-1,l-1,r-1));
		}
		else if (t == 2)
		{
			int l, r, x;
			scanf("%d %d %d",&l,&r,&x);
			/*for (int i = l - 1; i < r; ++i)
				a[i] ^= x;*/
			update(tree,1,0,n-1,l-1,r-1,x);
		}
	}
}

ll gcdex(ll a, ll b, ll & x, ll & y)
{
	if (a == 0)
	{
		x = 0;
		y = 1;
		return b;
	}
	else
	{
		ll x1, y1;
		ll g = gcdex(b%a,a,x1,y1);
		x = y1 - b / a * x1;
		y = x1;
		return g;
	}
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif
	_a();
	//_b();
	//_c();
	//_d();
	//_e();
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

#define lli            long long int
#define ull            unsigned long long int
#define pb             push_back
#define bp             pop_back
#define mp             make_pair
#define rev            reverse
#define max3(a,b,c)    max(a,max(b,c))
#define max4(a,b,c,d)  max(max3(a,b,c),d)
#define min3(a,b,c)    min(a,min(b,c))
#define min4(a,b,c,d)  min(min3(a,b,c),d)
#define lol            return 0

int main()
{
    lli a, b;
    cin>>a>>b;
    if(a==b)
        cout<<1;
    else if(a>b)
        cout<<a;
    else cout<<b;
    lol;
}

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <time.h>

using namespace std;

int main()
{
    long long a,b;
    cin >> a >> b;
    long long count=0;
    if(a==b) {cout<<"1";return 0;}
    while(a!=1)
    {
        int countTemp=a/b;
        count+=countTemp;
        int temp=a-b*countTemp;
        a=b;
        b=temp;
    }
    cout<<count+b;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long a,b;
    scanf("%I64d%I64d",&a,&b);
    if(a%b==0){
        printf("%I64d",a/b);
    }else if(b%a==0){printf("%I64d",b/a);}
    else{
        printf("%I64d",(max(a,b)-1)/min(a,b)+min(a,b));
    }
    return 0;
}

/* Code by : Suraj (@suraj1611) */

#include<bits/stdc++.h>
#include <string>

using namespace std;

#define ll long long int
#define rep(i,n) for(int i=0; i<n; i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define mx INT_MAX
#define mn INT_MIN
#define md 1000000007
#define pb push_back
#define mp make_pair
#define pf printf
#define sc scanf
#define maxsize 1100005
#define lb cout<<endl;
#define F first
#define S second
#define print(i) cout<<i<<endl
#define print1(i,j) cout<<i<<" "<<j<<endl
#define label cout<<"hello!"<<endl
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);

ll power(ll x, ll  y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}

ll calc(ll a,ll b)
{
	return b*(b-a);
}

int main()
{
	IOS

	#ifndef ONLINE_JUDGE
	    freopen("in.txt", "r", stdin);
	    freopen("out.txt", "w", stdout);
	#endif

	ll a,b,ans=0;
	cin>>a>>b;
	ll p=__gcd(a,b);
	a/=p;b/=p;
	if(a>b)
	{
		ans+=a/b;
		a=a%b;
	}
	//cout<<ans<<endl;
	cout<<ans+calc(a,b)<<endl;

	#ifndef ONLINE_JUDGE
    	cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif

    return 0;
}

#include <iostream>
#include <cmath>

using namespace std;
using ll = long long;
using ld = long double;
using lld = __float128;

ll binsearch(ll a, ll b, lld start, lld end, bool reverse, int iters = 0) {
    if (iters > 130) {
        return llround((ld)start);
    }

    lld x = (start + end) / 2;
    lld val = (x - b) * x + a;
    if (reverse) {
        val = -val;
    }

    if (val < 0) {
        return binsearch(a, b, start, x, reverse, iters + 1);
    }
    return binsearch(a, b, x, end, reverse, iters + 1);
}

ll processSingle(ll a, ll b) {
    if (a == 1 && b == 2) {
        return 2;
    }
    if (b < 10000) {
        ll first = -1;
        for (ll x = 0; x <= b; ++x) {
            ll val = (x - b) * x + a;
            if (val == 0) {
                if (first == -1) {
                    first = x;
                } else {
                    return first + x;
                }
            }
        }
    }

    ld center = ((ld)b)/2;
    ll x = binsearch(a, b, 0, center, false);
    ll y = binsearch(a, b, center, b, true);
    return x + y;
}

ll process(ll a, ll b) {
    ll rv = 0;

    if (a >= b) {
        rv += a / b;
        a %= b;
    }

    if (a > 0) {
        rv += processSingle(a, b);
    }

    return rv;
}

int main() {
    ll a, b;
    cin >> a >> b;
    cout << process(a, b) << endl;
}

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    long long int  a,b;
    cin>>a>>b;
    long long int counter=0;
    while( a !=0){
        if(a>=b){
                long long int x=int (a/b);
            counter+=x;
            a%=b;
        }else{
        swap(a,b);
        }
    }
    cout<<counter<<endl;
    return 0;
}

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include<bits/stdc++.h>
#include<chrono>
#include<unordered_map>
#include<random>
#include<complex>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned ll
#define endl '\n'
#define mk make_pair
#ifndef ONLINE_JUDGE
int __builtin_popcount(int mask) {
	int cnt = 0;
	for (int i = 0; i < 23; i++)
		if (mask & (1 << i)) ++cnt;
	return cnt;
}
#endif
void fast_in_out() {
	std::ios_base::sync_with_stdio(NULL);
	cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
int dx[] = { 0,0,-1,1,-1,1,-1,1,0 };
int dy[] = { -1,1,0,0,-1,-1,1,1,0 };
int lx[] = { 2, 2, -1, 1, -2, -2, -1, 1 };
int ly[] = { -1, 1, 2, 2, 1, -1, -2, -2 };
const ld eps = 1e-9;
const int N = 1e6 + 50, M = 1e5 + 10, mod = 1e9 + 7;
ll ans;
ll gcd(ll a, ll b) { ans += b / a; return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { ll t = gcd(a, b); return t ? (a / t * b) : 0; }
int main() {
	fast_in_out();
//	cout << fixed << setprecision(6);
	ll a, b;
	cin >> a >> b;
	if (gcd(a, b) == 1)ans = max(a, b);
	cout << ans << endl;
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<map>
using namespace std;

int main()
{
    //freopen("c.txt","r",stdin);

    long long int a,b,p,q;

    while(cin>>a>>b)
    {
        p=0;
        q=0;
        if(a>b)
        {
            q=a-b;
            p=b+(q/b);
            if(q%b>0)
            p++;
        }

        else
        {
            q=b-a;
            p=a+(q/a);
            if(q%a>0)
            p++;
        }

        cout<<p<<endl;

    }

}

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <bitset>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
using namespace std;

#define FOR0(i,n) for( i = 0 ; i < n ; ++i )
#define FOR1(i,n) for( i = 1 ; i <= n ; ++i )
#define sys_p system( "pause" )
#define pb push_back
#define mp make_pair
#define FI first
#define SE second

typedef long long LL ;
typedef vector < LL > vl ;
typedef vector < vl > vvl ;
typedef vector < vvl > vvvl ;

const LL INF = (LL)1e18 ;
const LL MOD = (LL)1e9 + 7 ;

ifstream Cin( "input.txt" ) ;
ofstream Cout( "output.txt" ) ;

main()
{
       LL a, b, ans, i, z, ch = 0, zn = 0, s = 0, tr, count ; cin >> a >> b ;

       z = a * b ;

       z = min( z, a/b + ( a%b > 0 ? (a%b)*(b-(a%b)) + 1LL : 0 ) ) ;

       if( a > 1 )
       {

       tr = 1 ;

       LL sq = (LL)sqrt( a ) ;
       for( i = 2 ; i <= sq ; i++ )
       {
            count = 0 ;
            while( a % i == 0 )
            {
                   if( count ) { tr = 0 ; break ; }
                   count ++ ;
                   a /= i ;
                   if( ch == 0 ) ch = 1, zn = i ;
                   else ch = ch * i + zn, zn *= i ;
                   s += i ;
            }
            if( tr == 0 ) break ;
       }

       if( tr && a > 1 )
       {
           if( ch == 0 ) ch = 1, zn = i ;
           else ch = ch * a + zn, zn *= a ;
           s += a ;
       }
       while( tr && ch < b ) ch += zn, s++ ; if( ch == b ) z = min( z, s ) ;

       }
       cout << z ;
}

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
using namespace std;
int main()
{ ll a,b,ans=0;
  cin>>a>>b;
  double q,p;
  ll i,k,j;
  q=(double)a/(double)b;
  i=a/b;
  ans+=i;

  p=q-i;
  ll c,d;
  d=b;
  c=a%b;
  if(c!=0)
  ans+=d/c;
  if(c!=0)
  ans+=(d%c)*c;
cout<<ans;//<<"  "<<p<<" "<<c<<"  "<<d<<endl;
	return 0;
}

#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define P push
#define B begin()
#define E end()
#define SI size()
#define R return
#define C continue
#define W while
#define SF(x) scanf("%I64d",&x);
#define MEM(vis,i) memset(vis,i,sizeof(vis))
#define rep(i,x,n) for(int i=x;i<n;i++)
using namespace std;
typedef unsigned long long ll;
typedef pair<ll,ll> pll;
const long long MOD = 1e9+7;
const long long MAX = 1e14+8;
const long long MAXN = 1e5+8;
ll gcd(ll a,ll b){
if(a%b==0) R b;
else R gcd(b,a%b);}

int main(){
ll a,b;
cin>>a>>b;
ll sum=(a>=b);
a%=b;
if(a==0){
    cout<<sum;
    R 0;
}
cout<<a<<" "<<b<<endl;
ll g=gcd(a,b);
a/=g,b/=g;
cout<<sum+max(a,b);
        return 0;
}