#include <iostream>
#include <fstream>

using namespace std;

int nod(long long int a,long long int b)
{
    while (a!=0 && b!=0)
    if (a>b)
        a%=b;
    else
        b%=a;
    return a+b;
}

int main()
{
    long long int a,b,c,d,ans,m;
    ans = 0;
    ifstream file1 ("input.txt");
    ofstream file2 ("output.txt");
    file1 >> a >> b >> c >> d;

    while (a * d < c * b){
        a += 1;
        b += 1;
        m = nod(a, b);
        a /= m;
        b /= m;
        ans += 1;
    }
    if (a == c && b == d)
        file2 << ans;
    else
        file2 << 0;

    return 0;
}
