#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
#define inf 1000000000000000LL
#define mod 1000000007LL
#define FOR(i,a,b) for(int i = a; i < b; i++)
const ll Mod = 1000000009LL;
const ll Base = 271LL;

struct hashed_string{
    private:
    string s; ll n;
    vector <ll> prefix;
    vector <ll> pow;
    public:
    hashed_string(string a){
        s = a; n = a.size();
        prefix.assign(n, 0);
        ll sum = 0;
        pow.assign(n+1, 0);
        pow[0] = 1;
        for (int i = 1; i <= n; i++){
            pow[i] = (pow[i-1] * Base) % Mod;
        }
        for (int i = 0; i < n; i++){
            prefix[i] = ((sum * Base) % Mod + s[i] - 'a' + 1) % Mod;
            sum = prefix[i];
        }
    }
    ll get_hash(ll l, ll r){
        ll x = prefix[r];
        if (l >= 1){
            l--;
            x = ((x - prefix[l] * pow[r - l] % Mod) % Mod + Mod) % Mod;
        }
        return x;
    }
};
void solve() {
    string s1,s2;
    cin >> s1 >> s2;
    vector <ll> cnt(2,0);
    for (int i = 0; i < s1.size(); i++){
        cnt[s1[i] - '0'] += 1;
    }  
    if (cnt[0] < cnt[1]){
        string sub;
        for (int i =0; i < s1.size(); i++) sub += (s1[i] == '0' ? '1' : '0');
        swap(cnt[0], cnt[1]);
        s1 = sub;
    }
    hashed_string v(s2);
    ll answ = 0;
    ll n = s2.size();
    for (int i = 1; i <= n; i++){
        if (cnt[0] * i > n) break;
        vector <ll> w(2, -1);
        if (cnt[1] == 0) continue;
        if ((n - cnt[0] * i) % cnt[1] != 0) continue;
        ll x0 = i;
        bool flag = true;
        ll x1 = (n - cnt[0] * i) / cnt[1];
        if (x1 == 0 || x0 == 0){
            continue;
        }
        int p = 0;
        for (int j = 0; j < n;){
            if (s1[p] == '0'){
                if (v.get_hash(j, j + x0 - 1) != w[0] && w[0] != -1){
                    flag = false;
                    break;
                }   
                w[0] = v.get_hash(j, j + x0 - 1);
                j += x0;
            }   
            else{
                if (v.get_hash(j, j + x1 - 1) != w[1] && w[1] != -1){
                    flag =false;
                    break;
                }
                w[1] = v.get_hash(j, j + x1 - 1);
                j += x1;
            }
            p++;
        }
        if (flag && w[0] != w[1]) answ++;
    }
    cout << answ << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll t = 1;
    while (t--) {
        solve();
    }

    return 0;
}