#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline void chmin(A &a, B b) {
  if (a > b) a = b;
}
template <typename A, typename B>
inline void chmax(A &a, B b) {
  if (a < b) a = b;
}
long long N, M, A, B, P, Q;
long long ans;
void dfs(long long n, long long s, long long x, long long y) {
  if (n == 0) return;
  chmin(ans, abs(M - s));
  if (1.0 * x * A > 1e18 || 1.0 * y * B > 1e18) return;
  dfs(n - 1, s + x * A + y * B, x * A, y * B);
  dfs(n - 1, s, x * A, y * B);
}
void dfs2(long long n, long long f, long long s, long long m) {
  if (n == 0) {
    chmin(ans, abs(s - m));
    return;
  }
  if (n - 1 >= f) dfs2(n - 1, f, s, m);
  if (f && (s <= m || n == f)) {
    dfs2(n - 1, f - 1, s + Q * (1ll << (n - 1)), m);
  }
}
signed main() {
  cin >> N >> M >> A >> B >> P >> Q;
  if (A > B) {
    swap(A, B);
    swap(P, Q);
  }
  ans = M;
  dfs(min(N, 26ll), 0, P, Q);
  dfs(min(N, 26ll), P + Q, P, Q);
  if (B == 1) {
    P += Q;
    if (M / P > N)
      chmin(ans, abs(M - N * P));
    else {
      chmin(ans, abs(M - M / P * P));
      if (M / P + 1 <= N) chmin(ans, abs(M - M / P * P - P));
    }
  } else if (A == 1 && B == 2) {
    long long t = 1;
    while (Q * (1ll << t) < 1e15) t++;
    chmin(t, N);
    for (long long i = 1; i <= t; i++) {
      dfs2(t, i, 0, M - i * P);
    }
  } else if (B == 2) {
    P += Q;
    long long t = 1;
    while (Q * (1ll << t) < 1e15) t++;
    chmin(t, N);
    for (long long i = 1; i <= t; i++) dfs2(t, i, 0, M);
  }
  cout << ans << endl;
  return 0;
}
