#include <bits/stdc++.h>
using namespace std;
long long n, m, a, b, p, q, s[50], g;
vector<long long> v[2];
void dfs(int f, long long pos, long long ss, long long e) {
  if (ss > m) return;
  if (pos == e) {
    v[f].push_back(ss);
    return;
  }
  long long ns = s[pos] + ss;
  dfs(f, pos + 1, ss, e);
  dfs(f, pos + 1, ns, e);
}
int main() {
  cin >> n >> m >> a >> b >> p >> q;
  if (b == 1 && a == 1) {
    if (m / (p + q) >= n)
      cout << m - (p + q) * n << endl;
    else
      cout << min(p + q - (m % (p + q)), m % (p + q)) << endl;
  } else {
    for (long long i = 0, x = p, y = q; i < n; i++, x *= a, y *= b) {
      long long ss = x + y;
      if (ss > m) break;
      s[g++] = ss;
    }
    v[0].push_back(0);
    v[1].push_back(0);
    v[1].push_back(1e16);
    dfs(0, 0, 0, g / 2);
    dfs(1, g / 2, 0, g);
    long long ans = m;
    sort(v[1].begin(), v[1].end());
    for (int i = 0; i < v[0].size(); i++) {
      int l = 0, M, r = v[1].size();
      while (l < r) {
        M = (l + r) / 2;
        if (v[0][i] + v[1][M] <= m)
          l = M + 1;
        else
          r = M;
      }
      ans = min(ans, m - v[0][i] - v[1][r - 1]);
      ans = min(ans, v[0][i] + v[1][r] - m);
    }
    cout << ans << endl;
  }
  return 0;
}
