#include <bits/stdc++.h>
using namespace std;
const int MX = 1000005;
const int alphabet = 256;
int p[MX], cnt[MX], c[22][MX];
int pn[MX], cn[MX];
void sufArr(char* s, int n) {
  for (int i = 0; i < n; ++i) ++cnt[(int)s[i]];
  for (int i = 1; i < alphabet; ++i) cnt[i] += cnt[i - 1];
  for (int i = 0; i < n; ++i) p[--cnt[(int)s[i]]] = i;
  c[0][p[0]] = 0;
  int classes = 1;
  for (int i = 1; i < n; ++i) {
    if (s[p[i]] != s[p[i - 1]]) ++classes;
    c[0][p[i]] = classes - 1;
  }
  for (int h = 0; (1 << h) <= n; ++h) {
    for (int i = 0; i < n; ++i) {
      pn[i] = p[i] - (1 << h);
      if (pn[i] < 0) pn[i] += n;
    }
    memset(cnt, 0, classes * sizeof(int));
    for (int i = 0; i < n; ++i) ++cnt[c[h][pn[i]]];
    for (int i = 1; i < classes; ++i) cnt[i] += cnt[i - 1];
    for (int i = n - 1; i >= 0; --i) p[--cnt[c[h][pn[i]]]] = pn[i];
    cn[p[0]] = 0;
    classes = 1;
    for (int i = 1; i < n; ++i) {
      int mid1 = (p[i] + (1 << h)) % n, mid2 = (p[i - 1] + (1 << h)) % n;
      if (c[h][p[i]] != c[h][p[i - 1]] || c[h][mid1] != c[h][mid2]) ++classes;
      cn[p[i]] = classes - 1;
    }
    memcpy(c[h + 1], cn, n * sizeof(int));
    for (int i = 0; i < n; i++) {
    }
  }
}
int Log2[MX];
int compare(int i, int j, int l) {
  int k = Log2[l];
  pair<int, int> a = make_pair(c[k][i], c[k][i + l - (1 << k)]);
  pair<int, int> b = make_pair(c[k][j], c[k][j + l - (1 << k)]);
  return a == b ? 0 : a < b ? -1 : 1;
}
char t[MX];
char s[MX];
int pr[MX][2];
bool valid2Len[MX];
int main() {
  for (int i = 2; i < MX; i++) {
    Log2[i] = Log2[i - 1];
    if ((i & (i - 1)) == 0) Log2[i]++;
  }
  ignore = scanf(" %s", s);
  ignore = scanf(" %s", t);
  int n = strlen(s);
  int m = strlen(t);
  if (s[0] == '1') {
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        s[i] = '0';
      else
        s[i] = '1';
    }
  }
  for (int i = 0; i < n; i++) {
    if (i > 0) {
      pr[i][0] = pr[i - 1][0];
      pr[i][1] = pr[i - 1][1];
    }
    if (s[i] == '0')
      pr[i][0]++;
    else
      pr[i][1]++;
  }
  vector<int> c0pos;
  vector<int> c1pos;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      c0pos.push_back(i);
    } else {
      c1pos.push_back(i);
    }
  }
  int c0 = c0pos.size(), c1 = c1pos.size();
  t[m] = 'a' - 1;
  sufArr(t, m + 1);
  for (int x = 1; x * c0 < m; x++) {
    int r = m - x * c0;
    if (r % c1 > 0) continue;
    int y = r / c1;
    bool ok = true;
    for (int pos : c0pos) {
      if (pos == 0) continue;
      int q = x * pr[pos - 1][0] + y * pr[pos - 1][1];
      if (compare(0, q, x) != 0) {
        ok = false;
        break;
      }
    }
    if (x == y) {
      int pos = 0;
      while (s[pos] != '1') pos++;
      int q = x * pr[pos - 1][0] + y * pr[pos - 1][1];
      if (compare(0, q, x) == 0) {
        ok = false;
      }
    }
    if (ok) {
      valid2Len[y] = true;
    }
  }
  int ans = 0;
  int sp = 0;
  while (s[sp] == '0') sp++;
  for (int y = 1; y * c1 < m; y++) {
    if (!valid2Len[y]) continue;
    int r = m - y * c1;
    if (r % c0 > 0) continue;
    int x = r / c0;
    bool ok = true;
    for (int pos : c1pos) {
      int q = x * pr[pos - 1][0] + y * pr[pos - 1][1];
      int qq = x * pr[sp - 1][0] + y * pr[sp - 1][1];
      if (compare(qq, q, y) != 0) {
        ok = false;
        break;
      }
    }
    if (ok) {
      ans++;
    }
  }
  printf("%d\n", ans);
}
