#include <bits/stdc++.h>
using namespace std;
const int LOGMAXN = 21;
const int MX = 1000002;
vector<int> lcp;
long long int cont[2];
int regresa[MX];
int logTable[MX];
long long int M[MX][LOGMAXN];
void BucketSort(vector<int>& sa, const vector<int>& rank, int ranks) {
  vector<int> bucket(ranks, 0);
  vector<int> tmp_sa(sa.size());
  for (int i = 0; i < sa.size(); ++i) ++bucket[rank[sa[i]]];
  for (int i = 0, sum = 0; i < ranks; ++i) {
    swap(bucket[i], sum);
    sum += bucket[i];
  }
  for (int i = 0; i < sa.size(); ++i) tmp_sa[bucket[rank[sa[i]]]++] = sa[i];
  swap(sa, tmp_sa);
}
vector<int> SuffixArray(const string& str) {
  int ranks = 255;
  vector<int> sa(str.size());
  vector<int> nrank(str.size());
  vector<int> rank(str.size(), 0);
  vector<int> tmp_rank(str.size());
  for (int i = 0; i < str.size(); ++i) nrank[i] = str[i], sa[i] = i;
  for (int p = 0; true; ++p) {
    BucketSort(sa, nrank, ranks + 1);
    BucketSort(sa, rank, ranks + 1);
    tmp_rank[0] = ranks = 0;
    for (int i = 1; i < str.size(); ++i)
      if (rank[sa[i]] != rank[sa[i - 1]] || nrank[sa[i]] != nrank[sa[i - 1]])
        tmp_rank[i] = ++ranks;
      else
        tmp_rank[i] = ranks;
    if (ranks + 1 == str.size()) break;
    for (int i = 1; i <= 1 << p; ++i) nrank[str.size() - i] = 0;
    for (int i = 0; i < str.size(); ++i) {
      int prv = sa[i] - (1 << p);
      if (prv >= 0) nrank[prv] = tmp_rank[i];
      rank[sa[i]] = tmp_rank[i];
    }
  }
  return sa;
}
vector<int> LCP(const string& str, const vector<int>& sa) {
  vector<int> lcp(str.size());
  vector<int> plcp(str.size());
  vector<int> phi(str.size(), -1);
  for (int i = 1; i < str.size(); ++i) phi[sa[i]] = sa[i - 1];
  int len = 0;
  for (int i = 0; i < str.size(); ++i) {
    if (phi[i] == -1) continue;
    for (; str[phi[i] + len] == str[i + len]; ++len) {
    }
    plcp[i] = len;
    len = max(len - 1, 0);
  }
  for (int i = 0; i < str.size(); ++i) lcp[i] = plcp[sa[i]];
  return lcp;
}
vector<int> Z(const string& cad) {
  vector<int> z(cad.size(), 0);
  int l = 0, r = 0, n = cad.size();
  for (int i = 1; i < n; i++) {
    if (i <= r) z[i] = min(z[i - l], r - i + 1);
    while (z[i] + i < n && cad[z[i] + i] == cad[z[i]]) z[i]++;
    if (r < i + z[i] - 1) l = i, r = i + z[i] - 1;
  }
  return z;
}
void pre() {
  int i, j, n = lcp.size();
  for (i = 2; i <= n; i++) logTable[i] = logTable[i >> 1] + 1;
  for (i = 0; i < n; i++) M[i][0] = lcp[i];
  for (j = 1; (1 << j) <= n; j++)
    for (i = 0; i + (1 << j) - 1 < n; i++)
      M[i][j] = min(M[i][j - 1], M[i + (1 << (j - 1))][j - 1]);
}
int query(int i, int j) {
  if (i > j) return 1e9;
  int k = logTable[j - i + 1];
  return min(M[i][k], M[j - (1 << k) + 1][k]);
}
int go(int a, int b, const vector<int>& z, const string& s, const string& t) {
  int off = t.size();
  int j = 0, mini = -1, maxi = -1, aux = -1;
  for (int i = 0; i < t.size() - 1; j++) {
    if (s[j] == '0') {
      if (z[i + off] < a) return 0;
      i += a;
    } else {
      if (mini == -1) {
        mini = maxi = regresa[i];
        aux = i;
      } else {
        mini = min(mini, regresa[i]);
        maxi = max(maxi, regresa[i]);
      }
      i += b;
    }
  }
  if (a == b && z[aux] >= a) return 0;
  return query(mini + 1, maxi) >= b;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  string s, t;
  cin >> s >> t;
  if (s[0] == '1') {
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '0')
        s[i] = '1';
      else
        s[i] = '0';
    }
  }
  string T = t + "$" + t;
  vector<int> z = Z(T);
  for (int i = 0; i < s.size(); i++) cont[s[i] - 48]++;
  t += "$";
  vector<int> sa = SuffixArray(t);
  lcp = LCP(t, sa);
  pre();
  for (int i = 0; i < sa.size(); i++) {
    regresa[sa[i]] = i;
  }
  int ans = 0;
  for (long long int uno = 1; uno < t.size(); uno++) {
    long long int x = (long long int)t.size() - 1 - cont[0] * uno;
    if (x <= 0) break;
    if (x % cont[1] != 0) continue;
    x /= cont[1];
    ans += go(uno, x, z, s, t);
  }
  cout << ans << '\n';
  return 0;
}
