#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 50;
long long n, m, a, b, p, q;
long long calc(long long k) {
  long long A = 1, B = 1;
  long long res = p + q;
  while (0 < res && res <= INF && k--) A *= a, B *= b, res = p * A + q * B;
  if (res < 0 || res > INF) return -1;
  return res;
}
long long ans = INF;
set<pair<long long, long long> > used;
void dfs(long long pos, long long sum) {
  assert(sum >= 0);
  ans = min(ans, (long long)abs(m - sum));
  if (pos >= n || sum < 0LL || sum > INF) return;
  if (used.count(pair<long long, long long>(pos, sum))) return;
  used.insert(pair<long long, long long>(pos, sum));
  long long r = calc(pos);
  if (r <= 0) return;
  dfs(pos + 1, sum + r);
  dfs(pos + 1, sum);
}
int main() {
  cin >> n >> m >> a >> b >> p >> q;
  if (max(a, b) == 1) {
    long long s = abs(m - (m / (p + q)) * (p + q));
    long long t = abs(m - (m / (p + q)) * (p + q) - (p + q));
    if (m / (p + q) == n) t = INF;
    if (m / (p + q) <= n)
      cout << min(s, t) << endl;
    else
      cout << m - (p + q) * n << endl;
    return 0;
  }
  dfs(0, 0);
  cout << ans << endl;
  return 0;
}
