#include <bits/stdc++.h>   using namespace std;   string s;   long long x;   long long mp[1 << 20];   vector<long long> vec;   long long trans[1 << 20][10];   long long dp[1005][12005];   int main(void) {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> s;     long long n = s.size();     s =  #  + s;     cin >> x;     if (x == 1) {       long long ans = 0;       for (int i = 1; i <= n; i++) {         if (s[i] ==  1 ) ans++;       }       cout << ans << endl;       return 0;     }     long long X = 1 << x;     for (int i = 0; i < X; i++) {       mp[i] = -1;       if (!(i & 1)) continue;       for (int d = 1; d < x; d++) {         if (x % d) continue;         for (int j = 0; j < x; j++) {           if ((i & (1 << j)) && (i & (1 << (j + d)))) goto pass;         }       }       vec.push_back(i);       mp[i] = (int)vec.size() - 1;     pass:;     }     long long m = vec.size();     for (int i = 0; i < m; i++) {       for (int j = 1; j <= 9; j++) {         long long b = vec[i];         b <<= j;         for (int d = 1; d < x; d++) {           if (x % d) continue;           if (b & (1 << d)) {             b %= (1 << d);             break;           }         }         if (b & (1 << x)) {           trans[i][j] = -1;           continue;         }         b %= X;         b |= 1;         trans[i][j] = mp[b];       }     }     for (int i = 0; i <= n; i++) {       for (int j = 0; j < m; j++) {         dp[i][j] = 1e18;       }     }     dp[0][mp[1]] = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         (dp[i + 1][j]) = min((dp[i + 1][j]), (dp[i][j] + 1));         if (trans[j][s[i + 1] -  0 ] != -1)           (dp[i + 1][trans[j][s[i + 1] -  0 ]]) =               min((dp[i + 1][trans[j][s[i + 1] -  0 ]]), (dp[i][j]));       }     }     long long ans = 1e18;     for (int j = 0; j < m; j++) ans = min(ans, dp[n][j]);     cout << ans << endl;     return 0;   }   