#include <bits/stdc++.h>
using namespace std;
const int maxn = int(1e6) + 100;
const int mod1 = 100019;
const int mod2 = 999999893;
char st[maxn], str[maxn];
long long ex[maxn], hashst[maxn], nex[maxn];
int exgcd(int b, int c, long long &x, long long &y) {
  if (c == 0) {
    if (x % b == 0) x /= b, y = 0;
    return b;
  }
  int d = exgcd(c, b % c, x, y);
  long long tmp = x;
  x = y;
  y = tmp - 1LL * b / c * y;
  return d;
}
long long POW(long long num, long long times) {
  long long ans = 1;
  while (times) {
    if (times & 1LL) ans = ans * num % mod2;
    num = num * num % mod2;
    times >>= 1;
  }
  return ans;
}
void solve() {
  scanf("%s", str);
  scanf("%s", st);
  int lenst, lenstr, zero, one;
  lenstr = strlen(str);
  lenst = strlen(st);
  zero = 0;
  one = 0;
  for (int i = 0; i < lenstr; ++i) {
    zero += (str[i] == '0');
    one += (str[i] == '1');
  }
  long long x = lenst, y;
  int d = exgcd(zero, one, x, y);
  if (lenst % d != 0) {
    puts("0");
    return;
  }
  x %= one / d;
  if (x <= 0) x += one / d;
  ex[0] = nex[0] = 1;
  for (int i = 1; i <= lenst; ++i) {
    ex[i] = ex[i - 1] * mod1 % mod2;
    nex[i] = POW(ex[i], mod2 - 2);
  }
  hashst[0] = 0;
  for (int i = 1; i <= lenst; ++i)
    hashst[i] = (hashst[i - 1] + ex[i] * (st[i - 1] - 'a')) % mod2;
  int ans = 0;
  for (; x < lenst; x += one / d) {
    int y = (lenst - x * zero) / one;
    if (y <= 0) break;
    long long zerohash = 0, onehash = 0;
    for (int i = 0; i < lenstr; ++i)
      if (str[i] == '0') {
        zerohash = (hashst[y * i + x] - hashst[y * i] + mod2) % mod2 *
                   nex[y * i] % mod2;
        break;
      }
    for (int i = 0; i < lenstr; ++i)
      if (str[i] == '1') {
        onehash = (hashst[x * i + y] - hashst[x * i] + mod2) % mod2 *
                  nex[x * i] % mod2;
        break;
      }
    if (zerohash == onehash && x == y) continue;
    long long now = 0;
    for (int i = 0, s = 0; i < lenstr; ++i)
      if (str[i] == '0') {
        now = (now + ex[s] * zerohash % mod2) % mod2;
        s += x;
      } else {
        now = (now + ex[s] * onehash % mod2) % mod2;
        s += y;
      }
    ans += (now == hashst[lenst]);
  }
  printf("%d\n", ans);
}
int main() {
  solve();
  return 0;
}

### Prompt

In CPP, your task is to solve the following problem:

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;
const int maxn = int(1e6) + 100;
const int mod1 = 100019;
const int mod2 = 999999893;
char st[maxn], str[maxn];
long long ex[maxn], hashst[maxn], nex[maxn];
int exgcd(int b, int c, long long &x, long long &y) {
  if (c == 0) {
    if (x % b == 0) x /= b, y = 0;
    return b;
  }
  int d = exgcd(c, b % c, x, y);
  long long tmp = x;
  x = y;
  y = tmp - 1LL * b / c * y;
  return d;
}
long long POW(long long num, long long times) {
  long long ans = 1;
  while (times) {
    if (times & 1LL) ans = ans * num % mod2;
    num = num * num % mod2;
    times >>= 1;
  }
  return ans;
}
void solve() {
  scanf("%s", str);
  scanf("%s", st);
  int lenst, lenstr, zero, one;
  lenstr = strlen(str);
  lenst = strlen(st);
  zero = 0;
  one = 0;
  for (int i = 0; i < lenstr; ++i) {
    zero += (str[i] == '0');
    one += (str[i] == '1');
  }
  long long x = lenst, y;
  int d = exgcd(zero, one, x, y);
  if (lenst % d != 0) {
    puts("0");
    return;
  }
  x %= one / d;
  if (x <= 0) x += one / d;
  ex[0] = nex[0] = 1;
  for (int i = 1; i <= lenst; ++i) {
    ex[i] = ex[i - 1] * mod1 % mod2;
    nex[i] = POW(ex[i], mod2 - 2);
  }
  hashst[0] = 0;
  for (int i = 1; i <= lenst; ++i)
    hashst[i] = (hashst[i - 1] + ex[i] * (st[i - 1] - 'a')) % mod2;
  int ans = 0;
  for (; x < lenst; x += one / d) {
    int y = (lenst - x * zero) / one;
    if (y <= 0) break;
    long long zerohash = 0, onehash = 0;
    for (int i = 0; i < lenstr; ++i)
      if (str[i] == '0') {
        zerohash = (hashst[y * i + x] - hashst[y * i] + mod2) % mod2 *
                   nex[y * i] % mod2;
        break;
      }
    for (int i = 0; i < lenstr; ++i)
      if (str[i] == '1') {
        onehash = (hashst[x * i + y] - hashst[x * i] + mod2) % mod2 *
                  nex[x * i] % mod2;
        break;
      }
    if (zerohash == onehash && x == y) continue;
    long long now = 0;
    for (int i = 0, s = 0; i < lenstr; ++i)
      if (str[i] == '0') {
        now = (now + ex[s] * zerohash % mod2) % mod2;
        s += x;
      } else {
        now = (now + ex[s] * onehash % mod2) % mod2;
        s += y;
      }
    ans += (now == hashst[lenst]);
  }
  printf("%d\n", ans);
}
int main() {
  solve();
  return 0;
}

```