#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef int (*itoi)(int);
typedef int (*iitoi)(int, int);
typedef int (*iiitoi)(int, int, int);
typedef ll (*ltol)(ll);
typedef ll (*lltol)(ll, ll);
typedef ll (*llltol)(ll, ll, ll);
const long double PI = acos(0) * 2;
const int INF = 0x3f2f1f0f;
const ll LINF = 1ll * INF * INF;
int X4[] = {-1, 0, 1, 0}, Y4[] = {0, -1, 0, 1};
int X8[] = {-1, -1, -1, 0, 1, 1, 1, 0}, Y8[] = {-1, 0, 1, 1, 1, 0, -1, -1};
template <typename T>
inline bool uax(T &a, T b) {
  return a < b ? (a = b, 1) : 0;
}
template <typename T>
inline bool uin(T &a, T b) {
  return a > b ? (a = b, 1) : 0;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ", " << p.second << ")";
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i];
    if (i + 1 < (int)v.size()) os << ' ';
  }
  return os;
}
namespace number_theory {
long long MOD = 1e9 + 7;
long long __gcd_two(long long a, long long b) {
  return a < 0ll   ? __gcd_two(-a, b)
         : b < 0ll ? __gcd_two(a, -b)
         : a < b   ? __gcd_two(b, a)
         : b       ? __gcd_two(b, a % b)
                   : a;
}
long long gcd(long long a) { return a < 0ll ? -a : a; }
template <typename... rests>
long long gcd(long long a, const rests &...rest) {
  long long b = gcd(rest...);
  return __gcd_two(a, b);
}
void extended_euclid(long long a, long long b, long long &x, long long &y) {
  if (a < 0) {
    extended_euclid(-a, b, x, y);
    x = -x;
  } else if (b < 0) {
    extended_euclid(a, -b, x, y);
    y = -y;
  } else if (a < b) {
    extended_euclid(b, a, y, x);
  } else if (b > 0) {
    extended_euclid(b, a % b, y, x);
    y -= a / b * x;
  } else {
    x = 1, y = 0;
  }
}
long long mod_clip(long long a, long long m) { return (a % m + m) % m; }
long long mod_clip(long long a) { return mod_clip(a, MOD); }
long long mod_inverse(long long a, long long m) {
  assert(gcd(a, m) == 1);
  long long x, y;
  extended_euclid(a, m, x, y);
  return mod_clip(x, m);
}
long long mod_inverse(long long a) { return mod_inverse(a, MOD); }
long long mod_mult(long long a, long long b, long long m) {
  if (b < 0) return mod_clip(-mod_mult(a, -b, m), m);
  long long r = 0, x = a;
  while (b) {
    if (b & 1) r = (r + x) % m;
    x = (x + x) % m;
    b >>= 1;
  }
  return mod_clip(r, m);
}
long long mod_mult(long long a, long long b) { return mod_mult(a, b, MOD); }
long long mod_mults(long long a) { return mod_clip(a); }
template <typename... rests>
long long mod_mults(long long a, const rests &...rest) {
  long long b = mod_mults(rest...);
  return mod_mult(a, b);
}
long long mod_div(long long a, long long b, long long m) {
  assert(b != 0);
  return mod_mult(a, mod_inverse(b, m), m);
}
long long mod_div(long long a, long long b) { return mod_div(a, b, MOD); }
long long mod_pow(long long a, long long p, long long m) {
  if (p < 0) return mod_inverse(mod_pow(a, -p, m), m);
  long long r = 1, x = a;
  while (p) {
    if (p & 1) r = mod_clip(mod_mult(r, x, m), m);
    x = mod_mult(x, x, m);
    p >>= 1;
  }
  return r;
}
long long mod_pow(long long a, long long p) { return mod_pow(a, p, MOD); }
vector<long long> primes, phi, mob;
vector<bool> sieve;
void make_sieve(int n) {
  sieve.assign(n + 1, 1);
  sieve[0] = sieve[1] = 0;
  for (int i = 2; i <= n; i++) {
    if (!sieve[i]) continue;
    for (int j = i + i; j <= n; j += i) {
      sieve[j] = 0;
    }
  }
}
void make_primes(int n, bool naive_way = 0) {
  primes.resize(0);
  if (naive_way) {
    if (n >= 2) primes.push_back(2);
    for (int i = 3; i <= n; i += 2) {
      int flag = 0;
      for (auto &p : primes) {
        if (i % p == 0) {
          flag = 1;
          break;
        }
      }
      if (!flag) primes.push_back(i);
    }
  } else {
    if (((int)sieve.size()) <= n) make_sieve(n);
    for (int i = 2; i <= n; i++)
      if (sieve[i]) primes.push_back(i);
  }
}
void make_primes_added(int n) {
  if (((int)primes.size()) > 0 && primes.back() >= n) return;
  int start = ((int)primes.size()) == 0 ? 2 : primes.back() + 1;
  for (int i = start; i <= n; i++) {
    int flag = 0;
    for (auto &p : primes) {
      if (i % p == 0) {
        flag = 1;
        break;
      }
    }
    if (!flag) primes.push_back(i);
  }
}
void make_phi(int n) {
  phi.assign(n + 1, 0);
  for (int i = 0; i <= n; i++) phi[i] = i;
  for (int i = 1; i <= n; i++) {
    for (int j = i + i; j <= n; j += i) phi[j] -= phi[i];
  }
}
void make_mob(int n) {
  mob.assign(n + 1, 0);
  mob[1] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = i + i; j <= n; j += i) mob[j] -= mob[i];
  }
}
long long __AUTO_POLLARD_RHO_BOUND = 1e14;
long long __POLLARD_RHO_NAIVE_BOUND = 5e4;
int __MAX_POLLARD_RHO_ITERATION = 1e5;
int __MAX_MILLER_RABIN_TEST = 37;
bool __MILLER_RABIN_BEFORE_POLLARD_RHO = 1;
bool miller_rabin(long long n) {
  if (n <= 1) return 0;
  if (((int)primes.size()) == 0 || primes.back() < __MAX_MILLER_RABIN_TEST)
    make_primes_added(__MAX_MILLER_RABIN_TEST);
  for (int i = 0;
       i < ((int)primes.size()) && primes[i] <= __MAX_MILLER_RABIN_TEST; i++) {
    if (n == primes[i]) return 1;
    if (n % primes[i] == 0) return 0;
  }
  int s = 0;
  long long m = n - 1;
  while ((m & 1) == 0) m >>= 1, s++;
  for (int i = 0;
       i < ((int)primes.size()) && primes[i] <= __MAX_MILLER_RABIN_TEST; i++) {
    long long p = primes[i];
    long long q = mod_pow(p, m, n);
    if (q == 1) continue;
    bool flag = 1;
    for (int j = 0; j < s; j++) {
      if (q == n - 1) {
        flag = 0;
        break;
      }
      q = mod_mult(q, q, n);
    }
    if (flag) return 0;
  }
  return 1;
}
long long pollard_rho(long long n) {
  if (__MILLER_RABIN_BEFORE_POLLARD_RHO)
    if (miller_rabin(n)) return n;
  for (long long p = 2; p < __POLLARD_RHO_NAIVE_BOUND; p++)
    if (n % p == 0) return p;
  int it = 0;
  long long d = 1;
  while (it < __MAX_POLLARD_RHO_ITERATION) {
    srand(time(0));
    if (n == 1) return n;
    if (n % 2 == 0) return 2;
    long long x = (rand() % (n - 2)) + 2, y = x;
    long long c = (rand() % (n - 1)) + 1;
    while (it < __MAX_POLLARD_RHO_ITERATION && d == 1) {
      ++it;
      x = (mod_mult(x, x, n) + c + n) % n;
      y = (mod_mult(y, y, n) + c + n) % n;
      y = (mod_mult(y, y, n) + c + n) % n;
      d = gcd(x - y, n);
      if (d == n) {
        d = 1;
        break;
      } else if (d > 1)
        return d;
    }
  }
  return n;
}
vector<pair<long long, int> > __merge_factors(vector<pair<long long, int> > a,
                                              vector<pair<long long, int> > b) {
  map<long long, int> m;
  for (auto &p : a) m[p.first] += p.second;
  for (auto &p : b) m[p.first] += p.second;
  vector<pair<long long, int> > v;
  for (auto &p : m) v.push_back(p);
  return v;
}
vector<pair<long long, int> > factorization(long long n, int calc_mode = 0) {
  int orig_calc_mode = calc_mode;
  if (calc_mode == 0) calc_mode = (n > __AUTO_POLLARD_RHO_BOUND) ? 2 : 1;
  if (calc_mode == 1) {
    if (((int)primes.size()) > 0 && primes.back() > n / primes.back()) {
      vector<pair<long long, int> > v;
      for (auto &p : primes) {
        if (p * p > n) break;
        if (n % p == 0) {
          v.emplace_back(p, 0);
          while (n % p == 0) {
            v.back().second++;
            n /= p;
          }
        }
      }
      if (n > 1) v.emplace_back(n, 1);
      return v;
    } else {
      vector<pair<long long, int> > v;
      for (long long p = 2; p * p <= n; p++) {
        if (n % p == 0) {
          v.emplace_back(p, 0);
          while (n % p == 0) {
            v.back().second++;
            n /= p;
          }
        }
      }
      if (n > 1) v.emplace_back(n, 1);
      return v;
    }
  } else {
    long long d = pollard_rho(n);
    if (d == n || d == 1) {
      vector<pair<long long, int> > v;
      v.emplace_back(n, 1);
      return v;
    } else {
      return __merge_factors(factorization(d, orig_calc_mode),
                             factorization(n / d, orig_calc_mode));
    }
  }
}
bool is_prime(long long n, int calc_mode = 0) {
  if (n <= 1) return 0;
  if (((int)sieve.size()) > n) return sieve[n];
  if (calc_mode == 0) calc_mode = (n > __AUTO_POLLARD_RHO_BOUND) ? 2 : 1;
  if (calc_mode == 1) {
    if (((int)primes.size()) > 0 && primes.back() >= n) {
      auto p = lower_bound(primes.begin(), primes.end(), n);
      return (*p == n);
    } else if (((int)primes.size()) > 0 && primes.back() * primes.back() >= n) {
      for (auto &p : primes) {
        if (n % p == 0) return 0;
        if (p * p >= n) return 1;
      }
      return 1;
    } else {
      for (long long p = 2; p * p <= n; p++) {
        if (n % p == 0) return 0;
      }
      return 1;
    }
  } else {
    return miller_rabin(n);
  }
  return 0;
}
long long euler_phi(long long n, int calc_mode = 0) {
  if (n <= 1) return max(0ll, n);
  if (((int)phi.size()) > n) return phi[n];
  if (calc_mode == 0) calc_mode = (n > __AUTO_POLLARD_RHO_BOUND) ? 2 : 1;
  if (calc_mode == 1) {
    if (((int)primes.size()) > 0 && primes.back() * primes.back() >= n) {
      long long r = n;
      for (auto &p : primes) {
        if (n % p == 0) {
          r -= r / p;
          while (n % p == 0) n /= p;
        }
        if (p * p > n) break;
      }
      if (n > 1) r -= r / n;
      return r;
    } else {
      long long r = n;
      for (long long p = 2; p * p <= n; p++) {
        if (n % p == 0) {
          r -= r / p;
          while (n % p == 0) n /= p;
        }
      }
      if (n > 1) r -= r / n;
      return r;
    }
  } else {
    vector<pair<long long, int> > f = factorization(n, 2);
    long long r = n;
    for (auto &pe : f) {
      r -= r / pe.first;
    }
    return r;
  }
}
long long mobius(long long n, int calc_mode = 0) {
  assert(n > 0);
  if (((int)mob.size()) > n) return mob[n];
  if (calc_mode == 0) calc_mode = (n > __AUTO_POLLARD_RHO_BOUND) ? 2 : 1;
  if (calc_mode == 1) {
    if (((int)primes.size()) > 0 && primes.back() * primes.back() >= n) {
      long long r = 1;
      for (auto &p : primes) {
        if (n % (p * p) == 0) return 0;
        if (n % p == 0) r = -r, n /= p;
        if (p * p > n) break;
      }
      if (n > 1) r = -r;
      return r;
    } else {
      long long r = 1;
      for (long long p = 2; p * p <= n; p++) {
        if (n % (p * p) == 0) return 0;
        if (n % p == 0) r = -r, n /= p;
      }
      if (n > 1) r = -r;
      return r;
    }
  } else {
    vector<pair<long long, int> > f = factorization(n, 2);
    long long r = 1;
    for (auto &pe : f) {
      if (pe.second >= 2) return 0;
      r = -r;
    }
    return r;
  }
}
long long to_number(vector<pair<long long, int> > f) {
  long long r = 1;
  for (auto &pe : f)
    for (int i = 0; i < pe.second; i++) r *= pe.first;
  return r;
}
struct divisor_iterator {
  long long n;
  vector<pair<long long, int> > f;
  void init() { f = factorization(n); }
  divisor_iterator(long long n_) : n(n_) { init(); }
  void set_number(long long n_) {
    n = n_;
    init();
  }
  long long next_divisor(long long d) {
    assert(n % d == 0);
    vector<pair<long long, int> > iter = f;
    for (auto &pe : iter) pe.second = 0;
    for (auto &pe : iter) {
      while (d % pe.first == 0) pe.second++, d /= pe.first;
    }
    for (int i = 0; i < ((int)iter.size()); i++) {
      if (iter[i].second == f[i].second)
        iter[i].second = 0;
      else {
        iter[i].second++;
        break;
      }
    }
    return to_number(iter);
  }
};
vector<long long> divisors(long long n) {
  vector<long long> r;
  divisor_iterator iter(n);
  long long d = 1;
  while (d != n) {
    r.push_back(d);
    d = iter.next_divisor(d);
  }
  r.push_back(n);
  return r;
}
vector<long long> facts;
vector<vector<long long> > binoms;
bool __FACTS_ENABLE_MOD = 1;
long long __FACTS_MOD = MOD;
bool __BINOMS_ENABLE_MOD = 1;
long long __BINOMS_MOD = MOD;
void make_facts(int n, bool enable_mod = 1, long long mod = 0) {
  if (enable_mod && mod == 0) mod = MOD;
  __FACTS_ENABLE_MOD = enable_mod;
  __FACTS_MOD = enable_mod ? mod : 0;
  facts.assign(n + 1, 1);
  for (int i = 1; i <= n; i++) {
    facts[i] = enable_mod ? mod_mult(facts[i - 1], i, mod) : facts[i - 1] * i;
  }
}
void make_binoms(int n, int m, bool enable_mod = 1, long long mod = 0) {
  if (enable_mod && mod == 0) mod = MOD;
  __BINOMS_ENABLE_MOD = enable_mod;
  __BINOMS_MOD = enable_mod ? mod : 0;
  binoms.assign(n, vector<long long>(m, 0));
  binoms[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      binoms[i][j] = binoms[i - 1][j] + (j > 0 ? binoms[i - 1][j - 1] : 0);
      if (enable_mod) binoms[i][j] %= mod;
    }
  }
}
long long factorial(long long n, bool enable_mod = 1, long long mod = 0) {
  if (enable_mod && mod == 0) mod = MOD;
  if (((int)facts.size()) > n && enable_mod == __FACTS_ENABLE_MOD &&
      mod == __FACTS_MOD)
    return facts[n];
  long long r = 1;
  for (int i = 1; i <= n; i++) {
    r = enable_mod ? mod_mult(r, i, mod) : r * i;
  }
  return r;
}
long long binomial(long long n, long long m, bool enable_mod = 1,
                   long long mod = 0) {
  if (enable_mod && mod == 0) mod = MOD;
  if (((int)binoms.size()) > n && ((int)binoms[0].size()) > m &&
      enable_mod == __BINOMS_ENABLE_MOD && mod == __BINOMS_MOD)
    return binoms[n][m];
  else {
    if (enable_mod)
      return mod_div(mod_div(factorial(n, 1, mod), factorial(n - m, 1, mod)),
                     factorial(m, 1, mod));
    else {
      long long a = m, b = n - m;
      if (a < b) swap(a, b);
      long long r = 1;
      for (long long c = a + 1; c <= n; c++) r *= c;
      return r / factorial(b, 0);
    }
  }
}
};  // namespace number_theory
using namespace number_theory;
const long long HASH = 10007, IHASH = 820925358;
const int MAX_T = 1e6 + 5;
string S, T;
long long Sum[MAX_T];
long long Hash[MAX_T], IHash[MAX_T];
int N, M;
inline long long sum(int a, int b) {
  return mod_mult(a ? Sum[b - 1] - Sum[a - 1] : Sum[b - 1], IHash[a]);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << std::setprecision(10);
  cout << fixed;
  cin >> S >> T;
  N = ((int)(S).size()), M = ((int)(T).size());
  Hash[0] = IHash[0] = IHASH;
  for (int i = 1; i < M; i++) {
    Hash[i] = mod_mult(Hash[i - 1], HASH);
    IHash[i] = mod_mult(IHash[i - 1], IHASH);
  }
  for (int i = 0; i < M; i++) {
    Sum[i] = (i ? Sum[i - 1] : 0) + Hash[i] * ((int)T[i]);
  }
  int n0 = 0, n1 = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '0')
      n0++;
    else
      n1++;
  }
  if (n0 < n1) {
    swap(n0, n1);
    for (int i = 0; i < N; i++) S[i] = '0' + '1' - S[i];
  }
  long long ans = 0;
  for (long long s0 = 1; s0 * n0 < M; s0++) {
    long long s1 = (M - s0 * n0) / n1;
    if (s0 * n0 + s1 * n1 != M) continue;
    long long h0 = -1, h1 = -1;
    int p = 0;
    bool flag = 0;
    for (int i = 0; i < N; i++) {
      if (S[i] == '0') {
        if (h0 == -1)
          h0 = sum(p, p + s0);
        else if (h0 != sum(p, p + s0)) {
          flag = 1;
          break;
        }
        p += s0;
      } else {
        if (h1 == -1)
          h1 = sum(p, p + s1);
        else if (h1 != sum(p, p + s1)) {
          flag = 1;
          break;
        }
        p += s1;
      }
    }
    if (!flag && h0 != h1) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Construct a CPP code solution to the problem outlined:

One of Arkady's friends works at a huge radio telescope. A few decades ago the telescope has sent a signal s towards a faraway galaxy. Recently they've received a response t which they believe to be a response from aliens! The scientists now want to check if the signal t is similar to s.

The original signal s was a sequence of zeros and ones (everyone knows that binary code is the universe-wide language). The returned signal t, however, does not look as easy as s, but the scientists don't give up! They represented t as a sequence of English letters and say that t is similar to s if you can replace all zeros in s with some string r_0 and all ones in s with some other string r_1 and obtain t. The strings r_0 and r_1 must be different and non-empty.

Please help Arkady's friend and find the number of possible replacements for zeros and ones (the number of pairs of strings r_0 and r_1) that transform s to t.

Input

The first line contains a string s (2 ≤ |s| ≤ 10^5) consisting of zeros and ones — the original signal.

The second line contains a string t (1 ≤ |t| ≤ 10^6) consisting of lowercase English letters only — the received signal.

It is guaranteed, that the string s contains at least one '0' and at least one '1'.

Output

Print a single integer — the number of pairs of strings r_0 and r_1 that transform s to t.

In case there are no such pairs, print 0.

Examples

Input


01
aaaaaa


Output


4


Input


001
kokokokotlin


Output


2

Note

In the first example, the possible pairs (r_0, r_1) are as follows:

  * "a", "aaaaa" 
  * "aa", "aaaa" 
  * "aaaa", "aa" 
  * "aaaaa", "a" 



The pair "aaa", "aaa" is not allowed, since r_0 and r_1 must be different.

In the second example, the following pairs are possible: 

  * "ko", "kokotlin" 
  * "koko", "tlin" 

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef int (*itoi)(int);
typedef int (*iitoi)(int, int);
typedef int (*iiitoi)(int, int, int);
typedef ll (*ltol)(ll);
typedef ll (*lltol)(ll, ll);
typedef ll (*llltol)(ll, ll, ll);
const long double PI = acos(0) * 2;
const int INF = 0x3f2f1f0f;
const ll LINF = 1ll * INF * INF;
int X4[] = {-1, 0, 1, 0}, Y4[] = {0, -1, 0, 1};
int X8[] = {-1, -1, -1, 0, 1, 1, 1, 0}, Y8[] = {-1, 0, 1, 1, 1, 0, -1, -1};
template <typename T>
inline bool uax(T &a, T b) {
  return a < b ? (a = b, 1) : 0;
}
template <typename T>
inline bool uin(T &a, T b) {
  return a > b ? (a = b, 1) : 0;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ", " << p.second << ")";
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i];
    if (i + 1 < (int)v.size()) os << ' ';
  }
  return os;
}
namespace number_theory {
long long MOD = 1e9 + 7;
long long __gcd_two(long long a, long long b) {
  return a < 0ll   ? __gcd_two(-a, b)
         : b < 0ll ? __gcd_two(a, -b)
         : a < b   ? __gcd_two(b, a)
         : b       ? __gcd_two(b, a % b)
                   : a;
}
long long gcd(long long a) { return a < 0ll ? -a : a; }
template <typename... rests>
long long gcd(long long a, const rests &...rest) {
  long long b = gcd(rest...);
  return __gcd_two(a, b);
}
void extended_euclid(long long a, long long b, long long &x, long long &y) {
  if (a < 0) {
    extended_euclid(-a, b, x, y);
    x = -x;
  } else if (b < 0) {
    extended_euclid(a, -b, x, y);
    y = -y;
  } else if (a < b) {
    extended_euclid(b, a, y, x);
  } else if (b > 0) {
    extended_euclid(b, a % b, y, x);
    y -= a / b * x;
  } else {
    x = 1, y = 0;
  }
}
long long mod_clip(long long a, long long m) { return (a % m + m) % m; }
long long mod_clip(long long a) { return mod_clip(a, MOD); }
long long mod_inverse(long long a, long long m) {
  assert(gcd(a, m) == 1);
  long long x, y;
  extended_euclid(a, m, x, y);
  return mod_clip(x, m);
}
long long mod_inverse(long long a) { return mod_inverse(a, MOD); }
long long mod_mult(long long a, long long b, long long m) {
  if (b < 0) return mod_clip(-mod_mult(a, -b, m), m);
  long long r = 0, x = a;
  while (b) {
    if (b & 1) r = (r + x) % m;
    x = (x + x) % m;
    b >>= 1;
  }
  return mod_clip(r, m);
}
long long mod_mult(long long a, long long b) { return mod_mult(a, b, MOD); }
long long mod_mults(long long a) { return mod_clip(a); }
template <typename... rests>
long long mod_mults(long long a, const rests &...rest) {
  long long b = mod_mults(rest...);
  return mod_mult(a, b);
}
long long mod_div(long long a, long long b, long long m) {
  assert(b != 0);
  return mod_mult(a, mod_inverse(b, m), m);
}
long long mod_div(long long a, long long b) { return mod_div(a, b, MOD); }
long long mod_pow(long long a, long long p, long long m) {
  if (p < 0) return mod_inverse(mod_pow(a, -p, m), m);
  long long r = 1, x = a;
  while (p) {
    if (p & 1) r = mod_clip(mod_mult(r, x, m), m);
    x = mod_mult(x, x, m);
    p >>= 1;
  }
  return r;
}
long long mod_pow(long long a, long long p) { return mod_pow(a, p, MOD); }
vector<long long> primes, phi, mob;
vector<bool> sieve;
void make_sieve(int n) {
  sieve.assign(n + 1, 1);
  sieve[0] = sieve[1] = 0;
  for (int i = 2; i <= n; i++) {
    if (!sieve[i]) continue;
    for (int j = i + i; j <= n; j += i) {
      sieve[j] = 0;
    }
  }
}
void make_primes(int n, bool naive_way = 0) {
  primes.resize(0);
  if (naive_way) {
    if (n >= 2) primes.push_back(2);
    for (int i = 3; i <= n; i += 2) {
      int flag = 0;
      for (auto &p : primes) {
        if (i % p == 0) {
          flag = 1;
          break;
        }
      }
      if (!flag) primes.push_back(i);
    }
  } else {
    if (((int)sieve.size()) <= n) make_sieve(n);
    for (int i = 2; i <= n; i++)
      if (sieve[i]) primes.push_back(i);
  }
}
void make_primes_added(int n) {
  if (((int)primes.size()) > 0 && primes.back() >= n) return;
  int start = ((int)primes.size()) == 0 ? 2 : primes.back() + 1;
  for (int i = start; i <= n; i++) {
    int flag = 0;
    for (auto &p : primes) {
      if (i % p == 0) {
        flag = 1;
        break;
      }
    }
    if (!flag) primes.push_back(i);
  }
}
void make_phi(int n) {
  phi.assign(n + 1, 0);
  for (int i = 0; i <= n; i++) phi[i] = i;
  for (int i = 1; i <= n; i++) {
    for (int j = i + i; j <= n; j += i) phi[j] -= phi[i];
  }
}
void make_mob(int n) {
  mob.assign(n + 1, 0);
  mob[1] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = i + i; j <= n; j += i) mob[j] -= mob[i];
  }
}
long long __AUTO_POLLARD_RHO_BOUND = 1e14;
long long __POLLARD_RHO_NAIVE_BOUND = 5e4;
int __MAX_POLLARD_RHO_ITERATION = 1e5;
int __MAX_MILLER_RABIN_TEST = 37;
bool __MILLER_RABIN_BEFORE_POLLARD_RHO = 1;
bool miller_rabin(long long n) {
  if (n <= 1) return 0;
  if (((int)primes.size()) == 0 || primes.back() < __MAX_MILLER_RABIN_TEST)
    make_primes_added(__MAX_MILLER_RABIN_TEST);
  for (int i = 0;
       i < ((int)primes.size()) && primes[i] <= __MAX_MILLER_RABIN_TEST; i++) {
    if (n == primes[i]) return 1;
    if (n % primes[i] == 0) return 0;
  }
  int s = 0;
  long long m = n - 1;
  while ((m & 1) == 0) m >>= 1, s++;
  for (int i = 0;
       i < ((int)primes.size()) && primes[i] <= __MAX_MILLER_RABIN_TEST; i++) {
    long long p = primes[i];
    long long q = mod_pow(p, m, n);
    if (q == 1) continue;
    bool flag = 1;
    for (int j = 0; j < s; j++) {
      if (q == n - 1) {
        flag = 0;
        break;
      }
      q = mod_mult(q, q, n);
    }
    if (flag) return 0;
  }
  return 1;
}
long long pollard_rho(long long n) {
  if (__MILLER_RABIN_BEFORE_POLLARD_RHO)
    if (miller_rabin(n)) return n;
  for (long long p = 2; p < __POLLARD_RHO_NAIVE_BOUND; p++)
    if (n % p == 0) return p;
  int it = 0;
  long long d = 1;
  while (it < __MAX_POLLARD_RHO_ITERATION) {
    srand(time(0));
    if (n == 1) return n;
    if (n % 2 == 0) return 2;
    long long x = (rand() % (n - 2)) + 2, y = x;
    long long c = (rand() % (n - 1)) + 1;
    while (it < __MAX_POLLARD_RHO_ITERATION && d == 1) {
      ++it;
      x = (mod_mult(x, x, n) + c + n) % n;
      y = (mod_mult(y, y, n) + c + n) % n;
      y = (mod_mult(y, y, n) + c + n) % n;
      d = gcd(x - y, n);
      if (d == n) {
        d = 1;
        break;
      } else if (d > 1)
        return d;
    }
  }
  return n;
}
vector<pair<long long, int> > __merge_factors(vector<pair<long long, int> > a,
                                              vector<pair<long long, int> > b) {
  map<long long, int> m;
  for (auto &p : a) m[p.first] += p.second;
  for (auto &p : b) m[p.first] += p.second;
  vector<pair<long long, int> > v;
  for (auto &p : m) v.push_back(p);
  return v;
}
vector<pair<long long, int> > factorization(long long n, int calc_mode = 0) {
  int orig_calc_mode = calc_mode;
  if (calc_mode == 0) calc_mode = (n > __AUTO_POLLARD_RHO_BOUND) ? 2 : 1;
  if (calc_mode == 1) {
    if (((int)primes.size()) > 0 && primes.back() > n / primes.back()) {
      vector<pair<long long, int> > v;
      for (auto &p : primes) {
        if (p * p > n) break;
        if (n % p == 0) {
          v.emplace_back(p, 0);
          while (n % p == 0) {
            v.back().second++;
            n /= p;
          }
        }
      }
      if (n > 1) v.emplace_back(n, 1);
      return v;
    } else {
      vector<pair<long long, int> > v;
      for (long long p = 2; p * p <= n; p++) {
        if (n % p == 0) {
          v.emplace_back(p, 0);
          while (n % p == 0) {
            v.back().second++;
            n /= p;
          }
        }
      }
      if (n > 1) v.emplace_back(n, 1);
      return v;
    }
  } else {
    long long d = pollard_rho(n);
    if (d == n || d == 1) {
      vector<pair<long long, int> > v;
      v.emplace_back(n, 1);
      return v;
    } else {
      return __merge_factors(factorization(d, orig_calc_mode),
                             factorization(n / d, orig_calc_mode));
    }
  }
}
bool is_prime(long long n, int calc_mode = 0) {
  if (n <= 1) return 0;
  if (((int)sieve.size()) > n) return sieve[n];
  if (calc_mode == 0) calc_mode = (n > __AUTO_POLLARD_RHO_BOUND) ? 2 : 1;
  if (calc_mode == 1) {
    if (((int)primes.size()) > 0 && primes.back() >= n) {
      auto p = lower_bound(primes.begin(), primes.end(), n);
      return (*p == n);
    } else if (((int)primes.size()) > 0 && primes.back() * primes.back() >= n) {
      for (auto &p : primes) {
        if (n % p == 0) return 0;
        if (p * p >= n) return 1;
      }
      return 1;
    } else {
      for (long long p = 2; p * p <= n; p++) {
        if (n % p == 0) return 0;
      }
      return 1;
    }
  } else {
    return miller_rabin(n);
  }
  return 0;
}
long long euler_phi(long long n, int calc_mode = 0) {
  if (n <= 1) return max(0ll, n);
  if (((int)phi.size()) > n) return phi[n];
  if (calc_mode == 0) calc_mode = (n > __AUTO_POLLARD_RHO_BOUND) ? 2 : 1;
  if (calc_mode == 1) {
    if (((int)primes.size()) > 0 && primes.back() * primes.back() >= n) {
      long long r = n;
      for (auto &p : primes) {
        if (n % p == 0) {
          r -= r / p;
          while (n % p == 0) n /= p;
        }
        if (p * p > n) break;
      }
      if (n > 1) r -= r / n;
      return r;
    } else {
      long long r = n;
      for (long long p = 2; p * p <= n; p++) {
        if (n % p == 0) {
          r -= r / p;
          while (n % p == 0) n /= p;
        }
      }
      if (n > 1) r -= r / n;
      return r;
    }
  } else {
    vector<pair<long long, int> > f = factorization(n, 2);
    long long r = n;
    for (auto &pe : f) {
      r -= r / pe.first;
    }
    return r;
  }
}
long long mobius(long long n, int calc_mode = 0) {
  assert(n > 0);
  if (((int)mob.size()) > n) return mob[n];
  if (calc_mode == 0) calc_mode = (n > __AUTO_POLLARD_RHO_BOUND) ? 2 : 1;
  if (calc_mode == 1) {
    if (((int)primes.size()) > 0 && primes.back() * primes.back() >= n) {
      long long r = 1;
      for (auto &p : primes) {
        if (n % (p * p) == 0) return 0;
        if (n % p == 0) r = -r, n /= p;
        if (p * p > n) break;
      }
      if (n > 1) r = -r;
      return r;
    } else {
      long long r = 1;
      for (long long p = 2; p * p <= n; p++) {
        if (n % (p * p) == 0) return 0;
        if (n % p == 0) r = -r, n /= p;
      }
      if (n > 1) r = -r;
      return r;
    }
  } else {
    vector<pair<long long, int> > f = factorization(n, 2);
    long long r = 1;
    for (auto &pe : f) {
      if (pe.second >= 2) return 0;
      r = -r;
    }
    return r;
  }
}
long long to_number(vector<pair<long long, int> > f) {
  long long r = 1;
  for (auto &pe : f)
    for (int i = 0; i < pe.second; i++) r *= pe.first;
  return r;
}
struct divisor_iterator {
  long long n;
  vector<pair<long long, int> > f;
  void init() { f = factorization(n); }
  divisor_iterator(long long n_) : n(n_) { init(); }
  void set_number(long long n_) {
    n = n_;
    init();
  }
  long long next_divisor(long long d) {
    assert(n % d == 0);
    vector<pair<long long, int> > iter = f;
    for (auto &pe : iter) pe.second = 0;
    for (auto &pe : iter) {
      while (d % pe.first == 0) pe.second++, d /= pe.first;
    }
    for (int i = 0; i < ((int)iter.size()); i++) {
      if (iter[i].second == f[i].second)
        iter[i].second = 0;
      else {
        iter[i].second++;
        break;
      }
    }
    return to_number(iter);
  }
};
vector<long long> divisors(long long n) {
  vector<long long> r;
  divisor_iterator iter(n);
  long long d = 1;
  while (d != n) {
    r.push_back(d);
    d = iter.next_divisor(d);
  }
  r.push_back(n);
  return r;
}
vector<long long> facts;
vector<vector<long long> > binoms;
bool __FACTS_ENABLE_MOD = 1;
long long __FACTS_MOD = MOD;
bool __BINOMS_ENABLE_MOD = 1;
long long __BINOMS_MOD = MOD;
void make_facts(int n, bool enable_mod = 1, long long mod = 0) {
  if (enable_mod && mod == 0) mod = MOD;
  __FACTS_ENABLE_MOD = enable_mod;
  __FACTS_MOD = enable_mod ? mod : 0;
  facts.assign(n + 1, 1);
  for (int i = 1; i <= n; i++) {
    facts[i] = enable_mod ? mod_mult(facts[i - 1], i, mod) : facts[i - 1] * i;
  }
}
void make_binoms(int n, int m, bool enable_mod = 1, long long mod = 0) {
  if (enable_mod && mod == 0) mod = MOD;
  __BINOMS_ENABLE_MOD = enable_mod;
  __BINOMS_MOD = enable_mod ? mod : 0;
  binoms.assign(n, vector<long long>(m, 0));
  binoms[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      binoms[i][j] = binoms[i - 1][j] + (j > 0 ? binoms[i - 1][j - 1] : 0);
      if (enable_mod) binoms[i][j] %= mod;
    }
  }
}
long long factorial(long long n, bool enable_mod = 1, long long mod = 0) {
  if (enable_mod && mod == 0) mod = MOD;
  if (((int)facts.size()) > n && enable_mod == __FACTS_ENABLE_MOD &&
      mod == __FACTS_MOD)
    return facts[n];
  long long r = 1;
  for (int i = 1; i <= n; i++) {
    r = enable_mod ? mod_mult(r, i, mod) : r * i;
  }
  return r;
}
long long binomial(long long n, long long m, bool enable_mod = 1,
                   long long mod = 0) {
  if (enable_mod && mod == 0) mod = MOD;
  if (((int)binoms.size()) > n && ((int)binoms[0].size()) > m &&
      enable_mod == __BINOMS_ENABLE_MOD && mod == __BINOMS_MOD)
    return binoms[n][m];
  else {
    if (enable_mod)
      return mod_div(mod_div(factorial(n, 1, mod), factorial(n - m, 1, mod)),
                     factorial(m, 1, mod));
    else {
      long long a = m, b = n - m;
      if (a < b) swap(a, b);
      long long r = 1;
      for (long long c = a + 1; c <= n; c++) r *= c;
      return r / factorial(b, 0);
    }
  }
}
};  // namespace number_theory
using namespace number_theory;
const long long HASH = 10007, IHASH = 820925358;
const int MAX_T = 1e6 + 5;
string S, T;
long long Sum[MAX_T];
long long Hash[MAX_T], IHash[MAX_T];
int N, M;
inline long long sum(int a, int b) {
  return mod_mult(a ? Sum[b - 1] - Sum[a - 1] : Sum[b - 1], IHash[a]);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << std::setprecision(10);
  cout << fixed;
  cin >> S >> T;
  N = ((int)(S).size()), M = ((int)(T).size());
  Hash[0] = IHash[0] = IHASH;
  for (int i = 1; i < M; i++) {
    Hash[i] = mod_mult(Hash[i - 1], HASH);
    IHash[i] = mod_mult(IHash[i - 1], IHASH);
  }
  for (int i = 0; i < M; i++) {
    Sum[i] = (i ? Sum[i - 1] : 0) + Hash[i] * ((int)T[i]);
  }
  int n0 = 0, n1 = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '0')
      n0++;
    else
      n1++;
  }
  if (n0 < n1) {
    swap(n0, n1);
    for (int i = 0; i < N; i++) S[i] = '0' + '1' - S[i];
  }
  long long ans = 0;
  for (long long s0 = 1; s0 * n0 < M; s0++) {
    long long s1 = (M - s0 * n0) / n1;
    if (s0 * n0 + s1 * n1 != M) continue;
    long long h0 = -1, h1 = -1;
    int p = 0;
    bool flag = 0;
    for (int i = 0; i < N; i++) {
      if (S[i] == '0') {
        if (h0 == -1)
          h0 = sum(p, p + s0);
        else if (h0 != sum(p, p + s0)) {
          flag = 1;
          break;
        }
        p += s0;
      } else {
        if (h1 == -1)
          h1 = sum(p, p + s1);
        else if (h1 != sum(p, p + s1)) {
          flag = 1;
          break;
        }
        p += s1;
      }
    }
    if (!flag && h0 != h1) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

```