#include <bits/stdc++.h>
using namespace std;
const int maxn = int(1e6) + 100;
const int mod1 = 100019;
const int mod2 = 999999893;
char st[maxn], str[maxn];
long long ex[maxn], hashst[maxn], nex[maxn];
int exgcd(int b, int c, long long &x, long long &y) {
  if (c == 0) {
    if (x % b == 0) x /= b, y = 0;
    return b;
  }
  int d = exgcd(c, b % c, x, y);
  long long tmp = x;
  x = y;
  y = tmp - 1LL * b / c * y;
  return d;
}
long long POW(long long num, long long times) {
  long long ans = 1;
  while (times) {
    if (times & 1LL) ans = ans * num % mod2;
    num = num * num % mod2;
    times >>= 1;
  }
  return ans;
}
void solve() {
  scanf("%s", str);
  scanf("%s", st);
  int lenst, lenstr, zero, one;
  lenstr = strlen(str);
  lenst = strlen(st);
  zero = 0;
  one = 0;
  for (int i = 0; i < lenstr; ++i) {
    zero += (str[i] == '0');
    one += (str[i] == '1');
  }
  long long x = lenst, y;
  int d = exgcd(zero, one, x, y);
  if (lenst % d != 0) {
    puts("0");
    return;
  }
  x %= one / d;
  if (x <= 0) x += one / d;
  ex[0] = nex[0] = 1;
  for (int i = 1; i <= lenst; ++i) {
    ex[i] = ex[i - 1] * mod1 % mod2;
    nex[i] = POW(ex[i], mod2 - 2);
  }
  hashst[0] = 0;
  for (int i = 1; i <= lenst; ++i)
    hashst[i] = (hashst[i - 1] + ex[i] * (st[i - 1] - 'a')) % mod2;
  int ans = 0;
  for (; x < lenst; x += one / d) {
    int y = (lenst - x * zero) / one;
    if (y <= 0) break;
    long long zerohash = 0, onehash = 0;
    for (int i = 0; i < lenstr; ++i)
      if (str[i] == '0') {
        zerohash = (hashst[y * i + x] - hashst[y * i] + mod2) % mod2 *
                   nex[y * i] % mod2;
        break;
      }
    for (int i = 0; i < lenstr; ++i)
      if (str[i] == '1') {
        onehash = (hashst[x * i + y] - hashst[x * i] + mod2) % mod2 *
                  nex[x * i] % mod2;
        break;
      }
    if (zerohash == onehash && x == y) continue;
    long long now = 0;
    for (int i = 0, s = 0; i < lenstr; ++i)
      if (str[i] == '0') {
        now = (now + ex[s] * zerohash % mod2) % mod2;
        s += x;
      } else {
        now = (now + ex[s] * onehash % mod2) % mod2;
        s += y;
      }
    ans += (now == hashst[lenst]);
  }
  printf("%d\n", ans);
}
int main() {
  solve();
  return 0;
}
