#include <bits/stdc++.h>
using namespace std;
int issubstring(string s1, string s2) {
  if (s1.find(s2) != std::string::npos) {
    return 1;
  } else
    return 0;
}
int ischarpresent(string s1, char a, int sn) {
  for (int i = 0; i < sn; i++) {
    if (a == s1[i]) {
      return 1;
    }
  }
  return 0;
}
string getstring(char x) {
  string s(1, x);
  return s;
}
bool issubsequence(string str1, string str2, int m, int n) {
  int j = 0;
  for (int i = 0; i < n && j < m; i++)
    if (str1[j] == str2[i]) j++;
  return (j == m);
}
int countchar(char s[], char a, int slen) {
  int count = 0;
  for (int i = 0; i < slen; i++) {
    if (s[i] == a) {
      count++;
    }
  }
  return count;
}
bool isint(long long int s[], long long int a, long long int slen) {
  for (int i = 0; i < slen; i++) {
    if (s[i] == a) {
      return true;
    }
  }
  return false;
}
long long int vecsum(vector<long long int> a, long long int n) {
  long long int sum = 0;
  for (int i = 0; i < n; i++) {
    sum = sum + a[i];
  }
  return sum;
}
bool sortbysecdesc(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first == b.first) {
    return a.second > b.second;
  }
}
void merge(long long int arr[], long long int l, long long int m,
           long long int r) {
  long long int n1 = m - l + 1;
  long long int n2 = r - m;
  long long int L[n1], R[n2];
  for (long long int i = 0; i < n1; i++) L[i] = arr[l + i];
  for (long long int j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
  long long int i = 0;
  long long int j = 0;
  long long int k = l;
  while (i < n1 && j < n2) {
    if (L[i] <= R[j]) {
      arr[k] = L[i];
      i++;
    } else {
      arr[k] = R[j];
      j++;
    }
    k++;
  }
  while (i < n1) {
    arr[k] = L[i];
    i++;
    k++;
  }
  while (j < n2) {
    arr[k] = R[j];
    j++;
    k++;
  }
}
void mergeSort(long long int arr[], long long int l, long long int r) {
  if (l >= r) {
    return;
  }
  long long int m = (l + r - 1) / 2;
  mergeSort(arr, l, m);
  mergeSort(arr, m + 1, r);
  merge(arr, l, m, r);
}
void rvereseArray(long long int arr[], long long int start, long long int end) {
  if (start >= end) return;
  long long int temp = arr[start];
  arr[start] = arr[end];
  arr[end] = temp;
  rvereseArray(arr, start + 1, end - 1);
}
long long int pour(long long int num, long long int a) {
  long long int c = 1;
  long long int an = a;
  while (an < num) {
    an = an * a;
    c++;
  }
  return c;
}
int convertBinaryToDecimal(long long int n) {
  int decimalNumber = 0, i = 0, remainder;
  while (n != 0) {
    remainder = n % 10;
    n /= 10;
    decimalNumber += remainder * pow(2, i);
    ++i;
  }
  return decimalNumber;
}
long long convertDecimalToBinary(int n) {
  long long binaryNumber = 0;
  int remainder, i = 1, step = 1;
  while (n != 0) {
    remainder = n % 2;
    n /= 2;
    binaryNumber += remainder * i;
    i *= 10;
  }
  return binaryNumber;
}
unsigned long long int res(unsigned long long int a, unsigned long long int b) {
  if (a == 1) return b;
  if (b == 1) return a;
  if (a > b) {
    if (a % b == 0) {
      return a / b;
    } else
      return a / b + res(a % b, b);
  } else if (b > a) {
    return res(b, a);
  } else
    return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  unsigned long long int a, b;
  cin >> a >> b;
  unsigned long long int ans = 0;
  ans = res(a, b);
  cout << ans;
}
