#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100005;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n, m, a, b, p, q;
  cin >> n >> m >> a >> b >> p >> q;
  if (a == 1 && b == 1) {
    long long cnt = m / (p + q);
    cout << m - min(n, cnt) * (p + q) << "\n";
  } else {
    long long mid = 21;
    vector<long long> u, v;
    for (int i = 0; i < (int)((1 << mid)); ++i) {
      long long val1 = 0, val2 = 0;
      double ch1 = 0, ch2 = 0;
      for (int j = 0; j < (int)(mid); ++j) {
        if ((i >> j) & 1) {
          val1 += p * pow(a, j) + q * pow(b, j);
          val2 += p * pow(a, mid + j) + q * pow(b, mid + j);
          ch1 += (double)p * pow(a, j) + (double)q * pow(b, j);
          ch2 += (double)p * pow(a, mid + j) + (double)q * pow(b, mid + j);
        }
      }
      if (ch1 <= m) {
        u.push_back(val1);
        if (ch2 <= m) {
          v.push_back(val2);
        }
      }
    }
    long long mn = (1LL << 60);
    for (int i = 0; i < (int)((int)(u).size()); ++i) {
      int id = upper_bound((v).begin(), (v).end(), m - u[i]) - v.begin();
      if (id > 0) {
        mn = min(mn, m - (u[i] + v[id]));
      }
    }
    cout << mn << "\n";
  }
  return 0;
}
