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

const long max_n = 2e6;

bool check[max_n+1];
queue< pair<long, long> > q;

void test(long x, long cnt)
{
    if (!check[x])
    {
        check[x] = true;
        q.push({x, cnt});
    }
}

long Loang(long n, long p, long a, long b, long r)
{
    memset(check, false, sizeof check);
    int cnt = 0;
    q.push({n%p,cnt});
    check[n] = true;

    while(!q.empty())
    {
        tie(n, cnt) = q.front(); q.pop();
        if (n==r) return cnt;

        test((n+a)%p, cnt+1);
        test((n+b)%p, cnt+1);
        test((n+a+b)%p, cnt+1);
    }

    return -1;
}

int main()
{
   // freopen("REPLACE.INP", "r", stdin);
   // freopen("REPLACE.OUT", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    long long n; long p, a, b, r; cin >> n >> p >> a >> b >> r;

    cout << Loang(n%p,p,a,b,r);

    return 0;
}
