#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const long long mod = 1e9 + 7;
const int p = 131;
long long _hash[N], _pow[N];
char s[N], t[N];
long long calc(long long l, long long r) {
  return (_hash[r] - _hash[l] * _pow[r - l] % mod + mod) % mod;
}
int main() {
  int n, m, cnt0, cnt1, p0, p1, ans;
  long long rec0, rec1, sum;
  scanf("%s", s);
  n = strlen(s);
  scanf("%s", t);
  m = strlen(t);
  cnt0 = cnt1 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      cnt0++;
    else
      cnt1++;
  }
  p0 = p1 = -1;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0' && p0 == -1) p0 = i;
    if (s[i] == '1' && p1 == -1) p1 = i;
    if (~p0 && ~p1) break;
  }
  _pow[0] = 1;
  _hash[0] = 0;
  for (int i = 1; i <= m; i++) {
    _pow[i] = _pow[i - 1] * p % mod;
    _hash[i] = (_hash[i - 1] * p % mod + t[i - 1]) % mod;
  }
  ans = 0;
  for (int i = 1; i * cnt0 + cnt1 <= m; i++) {
    if ((m - i * cnt0) % cnt1) continue;
    int j = (m - i * cnt0) / cnt1;
    rec0 = calc(j * p0, j * p0 + i);
    rec1 = calc(i * p1, i * p1 + j);
    if (rec0 == rec1) continue;
    sum = 0;
    for (int k = 0; k < n; k++)
      sum = (sum * _pow[s[k] == '0' ? i : j] % mod +
             (s[k] == '0' ? rec0 : rec1)) %
            mod;
    if (sum == _hash[m]) ans++;
  }
  printf("%d\n", ans);
  return 0;
}
