#include <bits/stdc++.h>
using namespace std;
string prothom;
string str;
long long mods[2] = {1000000007, 1000000009};
long long bases[2] = {137, 281};
long long Pow[2][1000005 + 5];
long long Hash[2][1000005 + 5];
pair<long long, long long> getHash(long long i, long long j) {
  long long tmp1 =
      (Hash[0][i] - (Hash[0][j + 1] * Pow[0][j - i + 1]) % mods[0]) % mods[0];
  long long tmp2 =
      (Hash[1][i] - (Hash[1][j + 1] * Pow[1][j - i + 1]) % mods[1]) % mods[1];
  if (tmp1 < 0) tmp1 += mods[0];
  if (tmp2 < 0) tmp2 += mods[1];
  return make_pair(tmp1, tmp2);
}
char s[1000005 + 5];
int main() {
  scanf("%s", s);
  prothom = s;
  scanf("%s", s);
  str = s;
  Pow[0][0] = Pow[1][0] = 1;
  for (int i = 0; i < 2; i++)
    for (int j = 1; j < 1000005; j++)
      Pow[i][j] = (Pow[i][j - 1] * bases[i]) % mods[i];
  int len = str.size();
  for (int i = len - 1; i >= 0; i--) {
    for (int j = 0; j < 2; j++) {
      Hash[j][i] = (Hash[j][i + 1] * bases[j] + str[i]) % mods[j];
      Hash[j][i] = (Hash[j][i] + mods[j]) % mods[j];
    }
  }
  pair<long long, long long> act = getHash(0, len - 1);
  long long zero = 0, one = 0;
  long long ageKoytaone = -1, ageKoytazero = -1;
  for (long long i = 0; i < prothom.size(); i++) {
    one += prothom[i] - '0';
    if (ageKoytazero == -1 && prothom[i] == '1') {
      ageKoytazero = i;
    }
    if (ageKoytaone == -1 && prothom[i] == '0') {
      ageKoytaone = i;
    }
  }
  zero = prothom.size() - one;
  if (zero == 0) {
    if (str.size() % one) {
      cout << 0 << endl;
      return 0;
    }
    long long tmp = str.size() / one;
    bool flg = true;
    for (long long i = 0; i < one; i++) {
      if (str.substr(0, tmp) != str.substr(i * tmp, tmp)) {
        flg = false;
      }
    }
    if (flg) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
    return 0;
  }
  if (one == 0) {
    if (str.size() % zero) {
      cout << 0 << endl;
      return 0;
    }
    long long tmp = str.size() / zero;
    bool flg = true;
    for (long long i = 0; i < zero; i++) {
      if (str.substr(0, tmp) != str.substr(i * tmp, tmp)) {
        flg = false;
      }
    }
    if (flg) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
    return 0;
  }
  long long xx = one, yy = zero;
  if (xx < yy) swap(xx, yy);
  long long ans = 0;
  for (long long i = 1; i < len; i++) {
    long long tmp1 = i;
    if (i * xx >= len) break;
    if ((len - (i * xx)) % yy) continue;
    long long tmp2 = (len - (i * xx)) / yy;
    pair<long long, long long> onehash = {0, 0}, zerohash = {0, 0};
    if (xx == zero) {
      zerohash = getHash(tmp2 * ageKoytaone, tmp2 * ageKoytaone + tmp1 - 1);
      onehash = getHash(tmp1 * ageKoytazero, tmp1 * ageKoytazero + tmp2 - 1);
    } else {
      zerohash = getHash(tmp1 * ageKoytaone, tmp1 * ageKoytaone + tmp2 - 1);
      onehash = getHash(tmp2 * ageKoytazero, tmp2 * ageKoytazero + tmp1 - 1);
    }
    pair<long long, long long> tot = {0, 0};
    long long prvlen = 0;
    for (long long i = 0; i < prothom.size(); i++) {
      if (prothom[i] == '0') {
        long long fr = (zerohash.first * Pow[0][prvlen]) % mods[0];
        long long sc = (zerohash.second * Pow[1][prvlen]) % mods[1];
        tot.first += fr;
        tot.second += sc;
        tot.first = tot.first % mods[0];
        tot.second = tot.second % mods[1];
        prvlen += (xx == zero) ? tmp1 : tmp2;
      } else {
        long long fr = (onehash.first * Pow[0][prvlen]) % mods[0];
        long long sc = (onehash.second * Pow[1][prvlen]) % mods[1];
        tot.first += fr;
        tot.second += sc;
        tot.first = tot.first % mods[0];
        tot.second = tot.second % mods[1];
        prvlen += (xx == zero) ? tmp2 : tmp1;
      }
    }
    if (tot == act && onehash != zerohash) ans++;
  }
  cout << ans << endl;
  return 0;
}
