#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using R = long double;
int main(void) {
  ll n, m, a, b, p, q;
  cin >> n >> m >> a >> b >> p >> q;
  if (a == 1 and b == 1) {
    ll ticket = p + q;
    const ll used = min(n, m / ticket);
    const ll ans = min(abs(m - ticket * used), abs(m - ticket * (used + 1)));
    cout << ans << endl;
    return 0;
  }
  vector<ll> ticket;
  n = min(n, 40LL);
  for (int i = 0; i < n; ++i) {
    ll cur = p * pow<ll>(a, i) + q * pow<ll>(b, i);
    if (1.0 * p * pow<R>(a, i) + 1.0 * q * pow<R>(b, i) > 1e18) break;
    ticket.push_back(cur);
    if (cur > m) break;
  }
  n = ticket.size();
  vector<ll> item[2];
  const ll half[2] = {n / 2, n - n / 2};
  const ll offset[2] = {0, n / 2};
  for (int k = 0; k < 2; ++k)
    for (int mask = 0; mask < 1 << half[k]; ++mask) {
      ll cur = 0LL;
      for (int i = 0; i < half[k]; ++i)
        if (mask & (1 << i)) cur += ticket[i + offset[k]];
      item[k].push_back(cur);
    }
  ll ans = m;
  sort(begin(item[1]), end(item[1]));
  for (auto &it : item[0]) {
    const ll arg = m - it;
    auto it2 = upper_bound(begin(item[1]), end(item[1]), arg);
    if (it2 != end(item[1])) ans = min(ans, abs(m - it - *it2));
    it2--;
    ans = min(ans, m - it - *it2);
  }
  cout << ans << endl;
  return 0;
}
