#include <bits/stdc++.h>
using namespace std;
const long long Inf = (long long)1e17;
long long dfs(int k, long long sum, vector<long long>& num,
              vector<long long>& imos) {
  if (k < 0) return sum;
  if (sum >= imos[k]) return sum - imos[k];
  if (num[k] > sum) return dfs(k - 1, sum, num, imos);
  long long a = dfs(k - 1, sum, num, imos);
  long long b = dfs(k - 1, sum - num[k], num, imos);
  return min(a, b);
}
int main() {
  long long n, m, a, b, p, q;
  cin >> n >> m >> a >> b >> p >> q;
  if (p + q > m)
    cout << m << "\n";
  else if (p * (double)a + q * (double)b - 0.1 > m)
    cout << (m - (p + q)) << "\n";
  else if (a >= 1000000 || b >= 1000000) {
    if (p * (a + 1) + q * (b + 1) > m)
      cout << (m - (p * a + q * b)) << "\n";
    else
      cout << (m - (p * (a + 1) + q * (b + 1))) << "\n";
  } else if (a == 1 && b == 1) {
    if ((p + q) * (double)n - 0.1 > m)
      cout << (m % (p + q)) << "\n";
    else
      cout << (m - n * (p + q)) << "\n";
  } else if (b == 1) {
    vector<long long> num, imos;
    int j = 1;
    num.push_back(p + q);
    imos.push_back(num[0]);
    long long aa = a;
    while (aa * p + q <= m) {
      num.push_back(aa * p + q);
      imos.push_back(imos[j - 1] + num[j]);
      aa *= a;
      ++j;
    }
    cout << dfs(j - 1, m, num, imos) << "\n";
  } else if (a == 1) {
    vector<long long> num, imos;
    int j = 1;
    num.push_back(p + q);
    imos.push_back(num[0]);
    long long bb = b;
    while (bb * q + p <= m) {
      num.push_back(bb * q + p);
      imos.push_back(imos[j - 1] + num[j]);
      bb *= b;
      ++j;
    }
    cout << dfs(j - 1, m, num, imos) << "\n";
  } else {
    vector<long long> num;
    int j = 1;
    num.push_back(p + q);
    long long aa = a, bb = b;
    while (aa * p + bb * q <= m) {
      num.push_back(aa * p + bb * q);
      aa *= a;
      bb *= b;
      ++j;
    }
    for (--j; j >= 0; --j) {
      if (m >= num[j]) m -= num[j];
    }
    cout << m << "\n";
  }
  return 0;
}
