#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int maxn = 200005;
struct Hasher {
  int base, m1, before, after;
  string str;
  vector<long long> powm1, prefm1;
  vector<unsigned long long> prefm2, powm2;
  void init(int base, long long m1, int before, int after) {
    this->base = base;
    this->m1 = m1;
    this->before = before;
    this->after = after;
  }
  void preprocess() {
    powm1.push_back(1);
    powm2.push_back(1);
    for (int i = 1; i < ((int)((str).size())); ++i) {
      powm1.push_back((1ll * powm1[i - 1] * base) % m1);
      powm2.push_back(powm2[i - 1] * base);
    }
    prefm1.push_back(str[0] - 'A');
    prefm2.push_back(str[0] - 'A');
    for (int i = 1; i < ((int)((str).size())); ++i) {
      prefm1.push_back((1ll * prefm1[i - 1] * 1LL * base + str[i] - 'A') % m1);
      prefm2.push_back(prefm2[i - 1] * 1ULL * base + str[i] - 'A');
    }
  }
  pair<long long, unsigned long long> getHash(int i, int j) {
    if (i == 0) {
      return make_pair(prefm1[j], prefm2[j]);
    }
    int len = j - i + 1;
    return make_pair(
        (m1 + 1LL * prefm1[j] - (prefm1[i - 1] * powm1[len]) % m1) % m1,
        (1ULL * prefm2[j] - (prefm2[i - 1] * powm2[len])));
  }
  void setString(string s) { str = s; }
  int gen_rand_base(const int before, const int after) {
    long long seed =
        std::chrono::high_resolution_clock::now().time_since_epoch().count();
    std::mt19937 mt_rand(seed);
    int base = std::uniform_int_distribution<int>(before + 1, after)(mt_rand);
    return base % 2 == 0 ? base - 1 : base;
  }
  void setRandomBase() { base = gen_rand_base(before, after); }
  void setBase(int b) { base = b; }
  void set(long long mod) { m1 = mod; }
};
bool same(pair<long long, unsigned long long> &f,
          pair<long long, unsigned long long> &s) {
  return f.first == s.first && s.second == f.second;
}
int main() {
  string s, t;
  Hasher h;
  cin >> s >> t;
  int s_len = ((int)((s).size())), t_len = ((int)((t).size()));
  int ones = 0, zeros = 0;
  h.init(27, 1000000123, 0, 1);
  h.setString(t);
  h.preprocess();
  for (int i = 0; i < s_len; ++i) {
    if (s[i] == '0') {
      ++zeros;
    } else {
      ++ones;
    }
  }
  pair<long long, unsigned long long> tot = h.getHash(0, t_len - 1);
  if (zeros > ones) {
    for (int i = 0; i < s_len; ++i) {
      if (s[i] == '1') {
        s[i] = '0';
      } else {
        s[i] = '1';
      }
    }
    swap(zeros, ones);
  }
  int first_one = -1, first_zero = -1;
  for (int i = 0; i < s_len; ++i) {
    if (s[i] == '1' && first_one == -1) {
      first_one = i;
    }
    if (s[i] == '0' && first_zero == -1) {
      first_zero = i;
    }
  }
  int ans = 0;
  assert(first_one != -1 && first_zero != -1);
  for (int i = 1; i <= t_len; ++i) {
    int other = t_len - (i * ones);
    if (other <= 0) {
      break;
    }
    if (other % zeros) continue;
    other /= (zeros);
    pair<long long, unsigned long long> one_hash, zero_hash;
    int st_one = 0, st_zero = 0, en_one = 0, en_zero = 0;
    if (first_one == 0) {
      one_hash = h.getHash(0, i - 1);
      zero_hash = h.getHash(i * (first_zero), i * (first_zero) + (other)-1);
      st_one = 0, st_zero = i * first_zero;
      en_one = i - 1, en_zero = i * first_zero + other - 1;
    } else {
      zero_hash = h.getHash(0, other - 1);
      one_hash = h.getHash(other * (first_one), other * (first_one) + (i)-1);
      st_zero = 0;
      st_one = other * first_one;
      en_zero = other - 1, en_one = other * first_one + i - 1;
    }
    if (same(one_hash, zero_hash)) {
      continue;
    }
    pair<long long, unsigned long long> cur = make_pair(0, 0);
    for (int j = 0; j < s_len; ++j) {
      if (s[j] == '1') {
        cur.first =
            ((1LL * cur.first * h.powm1[i]) % h.m1 + one_hash.first) % h.m1;
        cur.second = 1ULL * cur.second * h.powm2[i] + one_hash.second;
      } else {
        cur.first =
            ((1LL * cur.first * h.powm1[other]) % h.m1 + zero_hash.first) %
            h.m1;
        cur.second = 1ULL * cur.second * h.powm2[other] + zero_hash.second;
      }
    }
    if (same(cur, tot)) {
      ++ans;
    }
  }
  cout << ans << "\n";
}
