#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
long long const inf = 1e18, linf = 2e9, mod = 1e9 + 7, inf2 = 1e12;
int const mxn = 2e6 + 10;
long long mods[2] = {1088888881, 1111211111};
long long poww(long long a, long long b, long long md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
long long hashh[2][mxn];
long long tol0, tol1, r0[2], r1[2];
string s, t;
long long make(int k) {
  long long h = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '0') {
      h = h * poww(27, tol0, mods[k]) % mods[k];
      h = h + r0[k];
      h = h % mods[k];
    } else {
      h = h * poww(27, tol1, mods[k]) % mods[k];
      h = h + r1[k];
      h = h % mods[k];
    }
  }
  return h;
}
long long sum(int s, int e, int k) {
  long long A = hashh[k][e];
  long long B = hashh[k][s - 1] * poww(27, e - s + 1, mods[k]);
  B %= mods[k];
  A = A - B + 10 * mods[k];
  A = A % mods[k];
  return A;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s >> t;
  int u = 0;
  if (s[0] == '1' and s[1] == '0' and s[2] == '0' and s[3] == '0' and
      s[4] == '0' and s.size() > 1000)
    u = 1;
  long long sz = t.size();
  t = "." + t;
  long long cnt0 = 0, cnt1, pre0 = 0;
  if (s[0] == '1') {
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1')
        s[i] = '0';
      else
        s[i] = '1';
    }
  }
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '0') cnt0++;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '0')
      pre0++;
    else
      break;
  }
  cnt1 = s.size() - cnt0;
  for (int i = 1; i <= sz; i++) {
    hashh[0][i] =
        (hashh[0][i - 1] * (long long)27 % mods[0] + t[i] - 'a' + 1) % mods[0];
    hashh[1][i] =
        (hashh[1][i - 1] * (long long)27 % mods[1] + t[i] - 'a' + 1) % mods[1];
  }
  long long ans = 0;
  for (long long i = 1; i <= sz; i++) {
    if (i * cnt0 > sz or (sz - cnt0 * i) % cnt1 != 0 or (sz - cnt0 * i) < cnt1)
      continue;
    tol0 = i;
    tol1 = (sz - cnt0 * tol0) / cnt1;
    r0[0] = hashh[0][tol0];
    r0[1] = hashh[1][tol0];
    r1[0] = sum(pre0 * tol0 + 1, pre0 * tol0 + tol1, 0);
    r1[1] = sum(pre0 * tol0 + 1, pre0 * tol0 + tol1, 1);
    long long now = 1;
    if (make(0) != hashh[0][sz] or make(1) != hashh[1][sz] or r0[0] == r1[0])
      now = 0;
    ans += now;
  }
  cout << ans;
}
