#include <bits/stdc++.h>
using namespace std;
long long hashvals[1111111 + 2];
long long pwr[1111111 + 2];
long long GetHash(int p1, int p2) {
  if (p1 == 0) {
    return hashvals[p2];
  } else {
    long long h =
        (hashvals[p2] - hashvals[p1 - 1] * pwr[p2 - p1 + 1]) % 1000000007;
    if (h < 0) {
      h += 1000000007;
    }
    return h;
  }
}
bool Can(const string& S, const string& T, int L0, int L1) {
  long long hashvals[2];
  hashvals[0] = hashvals[1] = -1;
  int p1 = 0;
  int p2 = 0;
  while (p1 < S.size()) {
    int id = S[p1] - '0';
    int curr_len = (id == 0 ? L0 : L1);
    long long h = GetHash(p2, p2 + curr_len - 1);
    if (hashvals[id] == -1) {
      hashvals[id] = h;
    } else if (hashvals[id] != h) {
      return false;
    }
    p1++;
    p2 += curr_len;
  }
  if (L0 == L1 && hashvals[0] == hashvals[1]) return false;
  return true;
}
int main() {
  string S;
  cin >> S;
  int N0 = 0;
  int N1 = 0;
  for (int i = 0; i < S.size(); i++) {
    if (S[i] == '0')
      N0++;
    else
      N1++;
  }
  string T;
  cin >> T;
  int n = S.size();
  int m = T.size();
  hashvals[0] = T[0] - 'a';
  for (int i = 1; i < m; i++) {
    hashvals[i] = (hashvals[i - 1] * 30 + T[i] - 'a') % 1000000007;
  }
  pwr[0] = 1;
  for (int i = 1; i <= m; i++) pwr[i] = (pwr[i - 1] * 30) % 1000000007;
  long long ans = 0;
  for (long long L0 = 1; L0 <= m; L0++) {
    if (L0 * N0 > m) break;
    long long L1 = (m - L0 * N0) / N1;
    if (L1 >= 1 && L0 * N0 + L1 * N1 == m) {
      if (Can(S, T, L0, L1)) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
