#include <bits/stdc++.h>
using namespace std;
unsigned long long f[2000010], p[2000010], f1[2000010], p1[2000010];
int cnt0, cnt1, l0, l1, len0, len1, ans = 0, pos;
unsigned long long hash00, hash01, hash10, hash11;
const unsigned long long MOD = 1000000007;
unsigned long long get_hash(int l, int r) {
  return f[r] - f[l - 1] * p[r - l + 1];
}
unsigned long long get_hash1(int l, int r) {
  return (f1[r] + MOD - (f1[l - 1] * p1[r - l + 1]) % MOD) % MOD;
}
char s1[2000010], s2[2000010];
int main() {
  bool flag = 1;
  scanf("%s", s1 + 1);
  scanf("%s", s2 + 1);
  int n1 = strlen(s1 + 1), n2 = strlen(s2 + 1);
  p[0] = 1;
  p1[0] = 1;
  for (int i = 1; i <= n1; i++) {
    if (s1[i] == '0')
      cnt0++;
    else
      cnt1++;
    if (cnt1 == 1 && !l1) {
      l1 = i;
    }
    if (cnt0 == 1 && !l0) {
      l0 = i;
    }
  }
  l0--, l1--;
  for (int i = 1; i <= n2; i++) {
    f[i] = f[i - 1] * 131 + (s2[i] - 'a' + 1);
    p[i] = p[i - 1] * 131;
    f1[i] = ((f1[i - 1] * 13331) % MOD + (s2[i] - 'a' + 1)) % MOD;
    p1[i] = (p1[i - 1] * 13331) % MOD;
  }
  pos = 1;
  if (cnt1 == 0 || cnt0 == 0) {
    if (n2 % n1) {
      printf("0\n");
      return 0;
    } else {
      hash00 = get_hash(1, n2 / n1);
      hash01 = get_hash1(1, n2 / n1);
      for (int i = 1; i <= n1; i++) {
        if (get_hash(pos, pos + n2 / n1 - 1) == hash00 &&
            get_hash1(pos, pos + n2 / n1 - 1) == hash01)
          pos += n2 / n1;
        else {
          flag = 0;
          break;
        }
      }
    }
    if (flag)
      printf("1\n");
    else
      printf("0\n");
    return 0;
  }
  for (int i = 1; i < n2; i++) {
    if ((n2 - i * cnt0) % cnt1 != 0) continue;
    flag = 1;
    len0 = i;
    len1 = (n2 - i * cnt0) / cnt1;
    if (len0 <= 0 || len1 <= 0) break;
    hash00 = get_hash(l0 * len1 + 1, l0 * len1 + len0),
    hash10 = get_hash(l1 * len0 + 1, l1 * len0 + len1);
    hash01 = get_hash1(l0 * len1 + 1, l0 * len1 + len0),
    hash11 = get_hash1(l1 * len0 + 1, l1 * len0 + len1);
    if (hash00 == hash10 && hash01 == hash11) continue;
    pos = 1;
    for (int j = 1; j <= n1; j++) {
      if (s1[j] == '0') {
        if (get_hash(pos, pos + len0 - 1) == hash00 &&
            get_hash1(pos, pos + len0 - 1) == hash01)
          pos = pos + len0;
        else {
          flag = 0;
          break;
        }
      } else if (s1[j] == '1') {
        if (get_hash(pos, pos + len1 - 1) == hash10 &&
            get_hash1(pos, pos + len1 - 1) == hash11)
          pos = pos + len1;
        else {
          flag = 0;
          break;
        }
      }
    }
    if (flag == 1) ans++;
  }
  printf("%d\n", ans);
}
