#include <bits/stdc++.h>
using namespace std;
using ld = long double;
using ll = long long;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "[";
  for (const auto& e : v) {
    os << e << ",";
  }
  os << "] ";
  os << "size = " << v.size() << endl;
  return os;
}
int main() {
  ll n, m, a, b, p, q;
  cin >> n >> m >> a >> b >> p >> q;
  if (p + q > m) {
    cout << m << endl;
  } else if (a == b) {
    cout << m % (p + q) << endl;
  } else {
    ll k = 0;
    ll A = 1;
    ll B = 1;
    vector<ll> value;
    for (; p * A + q * B <= m and k <= n; k++) {
      value.push_back(p * A + q * B);
      A *= a;
      B *= b;
    }
    n = k;
    vector<ll> sum(n);
    for (int i = 0; i < n; i++) {
      if (i == 0) {
        sum[i] = value[i];
      } else {
        sum[i] = sum[i - 1] + value[i];
      }
    }
    ll tot = 0;
    ll res = m;
    for (ll i = n - 1; i >= 1; i--) {
      if (sum[i - 1] < res) {
        if (res < value[i]) {
          tot += sum[i - 1];
          break;
        } else {
          res -= value[i];
          tot += value[i];
        }
      } else if (sum[i - 1] == res) {
        tot = m;
        break;
      }
    }
    cout << m - tot << endl;
  }
  return 0;
}
