inline ll get_limit(ll a, ll b){ ll res = 1; while (b) { if (b & 1) res *= a; if (res >= LMT + 1 || res <= 0) return LMT + 1; b >>= 1; if (b) a *= a; if (a >= LMT + 1 || a <= 0) return LMT + 1; } return res; }
intmain(){ scanf("%lld%lld%lld%lld", &b1, &d1, &b2, &d2); ll ul1 = get_limit(b1, d1); ll ll1 = get_limit(b1, d1 - 1); ll ul2 = get_limit(b2, d2); ll ll2 = get_limit(b2, d2 - 1); ll ul = min(ul1, ul2), lo = max(ll1, ll2); printf("%lld", ul > lo ? ul - lo : 0); return0; }
template <typename T> inlinevoidread(T &x){ x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; }
template <typename T> inline T Max(T &a, T &b){return a > b ? a : b;}
template <typename T> inlinevoidread(T &x){ x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; }
char ca[N]; int n, m, a[N], id[N], sub[N][2], bcnt, f[N][2];
inlinevoiddivide(){ for (int i = 1; i <= n; ++ i) id[i] = (i - 1) / m + 1; for (int i = 1; i <= id[n]; ++ i) sub[i][0] = (i - 1) * m + 1, sub[i][1] = i * m; sub[id[n]][1] = min(sub[id[n]][1], n); bcnt = id[n]; }
inlineintbitcnt(int x){ int cnt = 0; while (x) x -= x & (-x), ++ cnt; return cnt; }
intsolve_1(){ int res = INF; for (int s = 0; s < 1 << bcnt; ++ s) { int cnt = 0, lst = 0; for (int i = bcnt; i >= 1; -- i) if ((s >> (i - 1)) & 1 != lst) ++ cnt, lst ^= 1; for (int i = 1; i <= m; ++ i) { int c[2]; c[0] = c[1] = 0; for (int j = i; j <= n; j += m) ++ c[a[j] ^ (s >> (id[j] - 1)) & 1]; cnt += min(c[0], c[1]); } res = min(res, cnt); } return res; }
intsolve_2(){ int res = INF; for (int s = 0; s < 1 << m; ++ s) { for (int i = 1; i <= bcnt; ++ i) { int cnt[2]; cnt[0] = cnt[1] = 0; for (int j = sub[i][0]; j <= sub[i][1]; ++ j) ++ cnt[a[j] ^ (s >> (j - sub[i][0]) & 1)]; f[i][0] = min(f[i - 1][0] + cnt[1], f[i - 1][1] + cnt[1] + 1); f[i][1] = min(f[i - 1][1] + cnt[0], f[i - 1][0] + cnt[0] + 1); } res = min(res, min(f[bcnt][0], f[bcnt][1] + 1)); } return res; }
intmain(){ scanf("%s", ca); n = strlen(ca); read(m); for (int i = 0; i < n; ++ i) a[i + 1] = ca[i] - '0'; divide(); printf("%d", m > sqrt(n) ? solve_1() : solve_2()); return0; }