#include <bits/stdc++.h>
using namespace std;
const int maxx = 1000010;
const double pi = acos(-1.0);
const double eps = 1e-15;
long long t, n, m;
long long ans = 0, total = 0, sum = 0, sum1 = 0, sum2 = 0, cnt = 0, pos = 0,
          res = 0, l1 = 0, l2 = 0;
long long h[maxx], rk[maxx], sa[maxx], ta[maxx], tp[maxx], lg[maxx];
long long st[maxx][21];
char c[maxx], s[maxx];
void Qsort() {
  for (int i = 0; i <= m; i++) ta[i] = 0;
  for (int i = 1; i <= n; i++) ta[rk[i]]++;
  for (int i = 1; i <= m; i++) ta[i] += ta[i - 1];
  for (int i = n; i >= 1; i--) sa[ta[rk[tp[i]]]--] = tp[i];
}
void f() {
  m = 100;
  for (int i = 1; i <= n; i++) rk[i] = s[i] - 'a' + 1, tp[i] = i;
  Qsort();
  for (int w = 1, p = 0; p < n; m = p, w <<= 1) {
    p = 0;
    for (int i = 1; i <= w; i++) tp[++p] = n - w + i;
    for (int i = 1; i <= n; i++)
      if (sa[i] > w) tp[++p] = sa[i] - w;
    Qsort();
    swap(tp, rk);
    rk[sa[1]] = p = 1;
    for (int i = 2; i <= n; i++)
      rk[sa[i]] =
          (tp[sa[i]] == tp[sa[i - 1]] && tp[sa[i] + w] == tp[sa[i - 1] + w])
              ? p
              : ++p;
  }
}
void get() {
  long long k = 0;
  for (int i = 1; i <= n; i++) {
    if (k) k--;
    int j = sa[rk[i] - 1];
    while (s[i + k] == s[j + k]) k++;
    st[rk[i]][0] = h[rk[i]] = k;
  }
  lg[0] = -1;
  for (int i = 1; i <= n; i++) lg[i] = lg[i / 2] + 1;
  for (int i = 1; i <= lg[n]; i++)
    for (int j = 1; j + (1 << i) - 1 <= n; j++)
      st[j][i] = min(st[j][i - 1], st[j + (1 << (i - 1))][i - 1]);
}
long long query(long long l, long long r) {
  if (l == r) return n;
  if (l > r) swap(l, r);
  l++;
  long long le = lg[r - l + 1];
  return min(st[l][le], st[r - (1 << (le)) + 1][le]);
}
int main() {
  scanf("%s", c + 1);
  scanf("%s", s + 1);
  long long len = strlen(c + 1);
  if (c[1] == '1') {
    for (int i = 1; i <= len; i++)
      if (c[i] == '1')
        c[i] = '0';
      else
        c[i] = '1';
  }
  n = strlen(s + 1);
  for (int i = 1; i <= len; i++)
    if (c[i] == '0')
      sum1++;
    else {
      sum2++;
      if (!res) res = i - 1;
    }
  f(), get();
  for (long long i = 1; i <= n; i++) {
    l1 = i, sum = sum1 * i;
    if (sum >= n) break;
    if ((n - sum) % sum2) continue;
    pos = res * l1 + 1, l2 = (n - sum) / sum2;
    long long u = 0;
    for (long long i = 1, p = 1; i <= n; p++) {
      if (c[p] == '0') {
        if (query(rk[1], rk[i]) < l1) {
          u = 1;
          break;
        }
        i += l1;
      } else {
        if (query(rk[pos], rk[i]) < l2) {
          u = 1;
          break;
        }
        i += l2;
      }
    }
    if (!u) {
      if (l1 != l2 || query(rk[1], rk[pos]) < l1) ans++;
    }
  }
  printf("%lld", ans);
  return 0;
}
