JerryL/AlgoSimBench huggingface dataset:
p02764 AtCoder Beginner Contest 157 - Yakiniku Optimization Problem

Takahashi wants to grill N pieces of meat on a grilling net, which can be seen as a two-dimensional plane. The coordinates of the i-th piece of meat are \left(x_i, y_i\right), and its hardness is c_i.

Takahashi can use one heat source to grill the meat. If he puts the heat source at coordinates \left(X, Y\right), where X and Y are real numbers, the i-th piece of meat will be ready to eat in c_i \times \sqrt{\left(X - x_i\right)^2 + \left(Y-y_i\right)^2} seconds.

Takahashi wants to eat K pieces of meat. Find the time required to have K or more pieces of meat ready if he put the heat source to minimize this time.

Constraints

* All values in input are integers.
* 1 \leq N \leq 60
* 1 \leq K \leq N
* -1000 \leq x_i , y_i \leq 1000
* \left(x_i, y_i\right) \neq \left(x_j, y_j\right) \left(i \neq j \right)
* 1 \leq c_i \leq 100

Input

Input is given from Standard Input in the following format:


N K
x_1 y_1 c_1
\vdots
x_N y_N c_N


Output

Print the answer.

It will be considered correct if its absolute or relative error from our answer is at most 10^{-6}.

Examples

Input

4 3
-1 0 3
0 0 3
1 0 2
1 1 40


Output

2.4


Input

10 5
-879 981 26
890 -406 81
512 859 97
362 -955 25
128 553 17
-885 763 2
449 310 57
-656 -204 11
-270 76 40
184 170 16


Output

7411.2252

Example input:
10 5
-879 981 26
890 -406 81
512 859 97
362 -955 25
128 553 17
-885 763 2
449 310 57
-656 -204 11
-270 76 40
184 170 16
4 3
-1 0 3
0 0 3
1 0 2
1 1 40

Example output:
7411.2252
2.4

Difficulty rating: 0

Solution:
from math import hypot
from collections import deque

N, K, *XYC = map(int, open(0).read().split())
XYC = list(zip(*[iter(XYC)] * 3))

Q = deque([(0, 0, 1000.0)])

ans = float("inf")
while Q:
    X, Y, r = Q.popleft()
    if ans * 0.9999995 > sorted(c * hypot(max(abs(X - x) - r, 0), max(abs(Y - y) - r, 0)) for x, y, c in XYC)[K - 1]:
        ans = min(ans, sorted(c * hypot(X - x, Y - y) for x, y, c in XYC)[K - 1])
        for dx, dy in [(-0.5, -0.5), (-0.5, 0.5), (0.5, -0.5), (0.5, 0.5)]:
            Q.append((X + dx * r, Y + dy * r, r / 2))

print(ans)
333_E. Summer Earnings

Many schoolchildren look for a job for the summer, and one day, when Gerald was still a schoolboy, he also decided to work in the summer. But as Gerald was quite an unusual schoolboy, he found quite unusual work. A certain Company agreed to pay him a certain sum of money if he draws them three identical circles on a plane. The circles must not interfere with each other (but they may touch each other). He can choose the centers of the circles only from the n options granted by the Company. He is free to choose the radius of the circles himself (all three radiuses must be equal), but please note that the larger the radius is, the more he gets paid. 

Help Gerald earn as much as possible.

Input

The first line contains a single integer n — the number of centers (3 ≤ n ≤ 3000). The following n lines each contain two integers xi, yi ( - 104 ≤ xi, yi ≤ 104) — the coordinates of potential circle centers, provided by the Company.

All given points are distinct.

Output

Print a single real number — maximum possible radius of circles. The answer will be accepted if its relative or absolute error doesn't exceed 10 - 6.

Examples

Input

3
0 1
1 0
1 1


Output

0.50000000000000000000


Input

7
2 -3
-2 -3
3 0
-3 -1
1 -2
2 -2
-1 0


Output

1.58113883008418980000

Example input:
7
2 -3
-2 -3
3 0
-3 -1
1 -2
2 -2
-1 0

3
0 1
1 0
1 1


Example output:
1.5811387129

0.4999998055


Tags: binary search
bitmasks
brute force
geometry
sortings
Difficulty rating: 2500

Solution:
#include <bits/stdc++.h>
using namespace std;
bitset<3001> w[3010];
int n, m, x[3010], y[3010];
struct edge {
  int x, y, d;
} s[4500010];
inline bool c1(edge a, edge b) { return a.d > b.d; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", x + i, y + i);
    for (int j = 1; j < i; ++j)
      s[++m] = (edge){
          i, j, (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])};
  }
  sort(s + 1, s + 1 + m, c1);
  for (int i = 1; i <= m; ++i) {
    int x = s[i].x, y = s[i].y;
    if ((w[x] & w[y]).count())
      return 0 & printf("%.10lf\n", sqrt(s[i].d) * 0.5);
    w[x][y] = w[y][x] = 1;
  }
}
682_E. Alyona and Triangles

You are given n points with integer coordinates on the plane. Points are given in a way such that there is no triangle, formed by any three of these n points, which area exceeds S.

Alyona tried to construct a triangle with integer coordinates, which contains all n points and which area doesn't exceed 4S, but, by obvious reason, had no success in that. Please help Alyona construct such triangle. Please note that vertices of resulting triangle are not necessarily chosen from n given points.

Input

In the first line of the input two integers n and S (3 ≤ n ≤ 5000, 1 ≤ S ≤ 1018) are given — the number of points given and the upper bound value of any triangle's area, formed by any three of given n points.

The next n lines describes given points: ith of them consists of two integers xi and yi ( - 108 ≤ xi, yi ≤ 108) — coordinates of ith point.

It is guaranteed that there is at least one triple of points not lying on the same line.

Output

Print the coordinates of three points — vertices of a triangle which contains all n points and which area doesn't exceed 4S.

Coordinates of every triangle's vertex should be printed on a separate line, every coordinate pair should be separated by a single space. Coordinates should be an integers not exceeding 109 by absolute value.

It is guaranteed that there is at least one desired triangle. If there is more than one answer, print any of them.

Example

Input

4 1
0 0
1 0
0 1
1 1


Output

-1 0
2 0
0 2

Note

<image>

Example input:
4 1
0 0
1 0
0 1
1 1


Example output:
0 -1
0 1
2 1


Tags: geometry
two pointers
Difficulty rating: 2600

Solution:
#include <bits/stdc++.h>
using namespace std;
long long n, s, x[5678], y[5678];
long long ar(int a, int b, int c) {
  return abs((x[b] - x[a]) * (y[c] - y[a]) - (y[b] - y[a]) * (x[c] - x[a]));
}
int main() {
  scanf("%lld %lld", &n, &s);
  for (int i = (0); i < (n); i++) scanf("%lld %lld", x + i, y + i);
  int a = 0, b = 1, c = 2;
  while (1) {
    char g = 1;
    for (int i = (0); i < (n); i++) {
      if (ar(i, b, c) > ar(a, b, c)) a = i, g = 0;
      if (ar(a, i, c) > ar(a, b, c)) b = i, g = 0;
      if (ar(a, b, i) > ar(a, b, c)) c = i, g = 0;
    }
    if (g) break;
  }
  printf("%lld %lld\n%lld %lld\n%lld %lld\n", x[a] + x[b] - x[c],
         y[a] + y[b] - y[c], x[b] + x[c] - x[a], y[b] + y[c] - y[a],
         x[c] + x[a] - x[b], y[c] + y[a] - y[b]);
  return 0;
}
906_B. Seating of Students

Students went into a class to write a test and sat in some way. The teacher thought: "Probably they sat in this order to copy works of each other. I need to rearrange them in such a way that students that were neighbors are not neighbors in a new seating."

The class can be represented as a matrix with n rows and m columns with a student in each cell. Two students are neighbors if cells in which they sit have a common side.

Let's enumerate students from 1 to n·m in order of rows. So a student who initially sits in the cell in row i and column j has a number (i - 1)·m + j. You have to find a matrix with n rows and m columns in which all numbers from 1 to n·m appear exactly once and adjacent numbers in the original matrix are not adjacent in it, or determine that there is no such matrix.

Input

The only line contains two integers n and m (1 ≤ n, m ≤ 105; n·m ≤ 105) — the number of rows and the number of columns in the required matrix.

Output

If there is no such matrix, output "NO" (without quotes). 

Otherwise in the first line output "YES" (without quotes), and in the next n lines output m integers which form the required matrix.

Examples

Input

2 4


Output

YES
5 4 7 2 
3 6 1 8 


Input

2 1


Output

NO

Note

In the first test case the matrix initially looks like this:
    
    
      
    1 2 3 4  
    5 6 7 8  
    

It's easy to see that there are no two students that are adjacent in both matrices.

In the second test case there are only two possible seatings and in both of them students with numbers 1 and 2 are neighbors.

Example input:
2 1

2 4


Example output:
NO

YES
2 4 1 3 
7 5 8 6 


Tags: brute force
constructive algorithms
math
Difficulty rating: 2200

Solution:
#include <bits/stdc++.h>
using namespace std;
int n, m, a[100011], dx[] = {-1, 1, 0, 0}, ddy[] = {0, 0, -1, 1};
int check(int x) {
  for (int i = 0; i < 4; i++)
    if ((x > 1 && x % m != 1 && a[x - 1] == a[x] + dx[i] + ddy[i] * m) ||
        (x > m && a[x - m] == a[x] + dx[i] + ddy[i] * m))
      return 0;
  return 1;
}
int dfs(int x) {
  if (x > n * m) return 1;
  for (int i = x; i <= n * m; i++) {
    swap(a[x], a[i]);
    if (!check(x)) continue;
    if (dfs(x + 1)) return 1;
    swap(a[x], a[i]);
  }
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) a[(i - 1) * m + j] = (i - 1) * m + j;
  if (dfs(1)) {
    puts("YES");
    for (int i = 1; i <= n; i++, puts(""))
      for (int j = 1; j <= m; j++) printf("%d ", a[(i - 1) * m + j]);
  } else
    puts("NO");
}
p02986 AtCoder Beginner Contest 133 - Colorful Tree

There is a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and Vertex b_i, and the color and length of that edge are c_i and d_i, respectively. Here the color of each edge is represented by an integer between 1 and N-1 (inclusive). The same integer corresponds to the same color, and different integers correspond to different colors.

Answer the following Q queries:

* Query j (1 \leq j \leq Q): assuming that the length of every edge whose color is x_j is changed to y_j, find the distance between Vertex u_j and Vertex v_j. (The changes of the lengths of edges do not affect the subsequent queries.)

Constraints

* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* 1 \leq a_i, b_i \leq N
* 1 \leq c_i \leq N-1
* 1 \leq d_i \leq 10^4
* 1 \leq x_j \leq N-1
* 1 \leq y_j \leq 10^4
* 1 \leq u_j < v_j \leq N
* The given graph is a tree.
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q


Output

Print Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to Query j.

Example

Input

5 3
1 2 1 10
1 3 2 20
2 4 4 30
5 2 1 40
1 100 1 4
1 100 1 5
3 1000 3 4


Output

130
200
60

Example input:
5 3
1 2 1 10
1 3 2 20
2 4 4 30
5 2 1 40
1 100 1 4
1 100 1 5
3 1000 3 4

Example output:
130
200
60

Difficulty rating: 0

Solution:
#include<bits/stdc++.h>
using namespace std;
struct node {
	int y,c,d;
};
vector<node>T[100001],Q[100001];
int H[100001],F[100001][18],D[100001],C[100001],CD[100001],An[100001];
void dfs(int x,int la,int d) {
	H[x]=H[la]+1,D[x]=d,F[x][0]=la;
	for(int i=0; i<T[x].size(); i++) {
		node nw=T[x][i];
		if(nw.y!=la)dfs(nw.y,x,d+nw.d);
	}
}
void dfs1(int x,int la){
	for(int i=0;i<Q[x].size();i++){
		node nw=Q[x][i];
		if(nw.y>0)An[nw.y]+=D[x]+C[nw.c]*nw.d-CD[nw.c];
		else An[-nw.y]-=2*(D[x]+C[nw.c]*nw.d-CD[nw.c]);
	}
	for(int i=0;i<T[x].size();i++){
		node nw=T[x][i];
		if(nw.y!=la)C[nw.c]++,CD[nw.c]+=nw.d,dfs1(nw.y,x),C[nw.c]--,CD[nw.c]-=nw.d;
	}
}
int LCA(int x,int y) {
	if(H[x]>H[y])swap(x,y);
	int s=H[y]-H[x];
	for(int i=0; i<=17; i++)if(s&(1<<i))y=F[y][i];
	if(x==y)return x;
	for(int i=17; i>=0; i--)if(F[x][i]!=F[y][i])x=F[x][i],y=F[y][i];
	return F[x][0];
}
int main() {
	int a,b,i,j,x,y,c,d;
	cin>>a>>b;
	for(i=1; i<a; i++)scanf("%d%d%d%d",&x,&y,&c,&d),T[x].push_back({y,c,d}),T[y].push_back({x,c,d});
	dfs(1,0,0);
	for(i=1; i<=17; i++)for(j=1; j<=a; j++)F[j][i]=F[F[j][i-1]][i-1];
	for(i=1; i<=b; i++)scanf("%d%d%d%d",&c,&d,&x,&y),Q[x].push_back({i,c,d}),Q[y].push_back({i,c,d}),Q[LCA(x,y)].push_back({-i,c,d});
	dfs1(1,0);
	for(i=1;i<=b;i++)printf("%d\n",An[i]);
}
p03630 AtCoder Regular Contest 081 - Flip and Rectangles

We have a board with an H \times W grid. Each square in the grid is painted in black or white. The square at the i-th row from the top and j-th column from the left is black if the j-th character in S_i is `#`, and white if that character is `.`.

Snuke can perform the following operation on the grid any number of times:

* Select a row or column in the grid, and invert the color of all the squares in that row or column (that is, black squares become white and vice versa).



Then, Snuke draws a rectangle along grid lines. Here, all the squares contained in the rectangle must be painted in black.

Find the maximum possible area of Snuke's rectangle when the operation is performed optimally.

Constraints

* 2 \leq H \leq 2000
* 2 \leq W \leq 2000
* |S_i| = W
* S_i consists of `#` and `.`.

Input

Input is given from Standard Input in the following format:


H W
S_1
S_2
:
S_H


Output

Print the maximum possible area of Snuke's rectangle.

Examples

Input

3 3
..#
##.
.#.


Output

6


Input

3 3
..#
.
.#.


Output

6


Input

4 4
....
....
....
....


Output

16


Input

10 8
...#.#
...#.#
..###.#.
.##.#.#
.#..#.#.
..##.#.#
.#.#..
...#.#..
.#.##
..###


Output

27

Example input:
3 3
..#
.
.#.
10 8
...#.#
...#.#
..###.#.
.##.#.#
.#..#.#.
..##.#.#
.#.#..
...#.#..
.#.##
..###
3 3
..#
##.
.#.
4 4
....
....
....
....

Example output:
6
27
6
16

Difficulty rating: 0

Solution:
H, W = map(int, input().split())
S = [input() for _ in range(H)]

ans = max(H, W)

f = [1] * W
for i in range(1, H):
    f = [(f[j]+1) if (S[i-1][j:j+2] + S[i][j:j+2]).count('#') in [0,2,4] else 1 for j in range(W-1)] + [0]
    stk = []
    for j, v in enumerate(f):
        while len(stk) > 0 and f[stk[-1]] >= v:
            ans = max(ans, f[stk.pop()] * (j - (stk[-1] if len(stk) > 0 else -1)))
        stk.append(j)

print(ans)
Chef and The Divisibility Queries

All submissions for this problem are available.The Chef is given a sequence of N integers A1,  A2, ..., AN. He has to process Q queries on the above sequence. Each query is represented by three integers:


L R K => report cardinality of { i : K divides Ai, L ≤ i ≤ R }. In other words, how many integers in the subsequence starting at Lth element and ending at Rth element are divisible by K.


Input
The first line of the input contains two space separated integer N  and Q.
The following line contains N space separated integers giving the sequence A1,  A2, ..., AN.
Then there will be Q lines each containing three space separated integers L R K, representing a query.
 
Output
For each query output the result in one line.
Constraints

1 ≤ N, Q  ≤ 100000 (105)
1 ≤ Ai  ≤ 100000 (105)
 1 ≤ L ≤ R ≤ N
1 ≤ K  ≤ 100000 (105)

 
Example
Input:
8 5
3 5 1 4 6 9 12 6
3 6 2
3 6 4
4 8 3
2 6 7
8 8 6

Output:
2
1
4
0
1

Example input:

Example output:


Solution:

    #include<bits/stdc++.h>
    using namespace std;
    #define N 100001
    vector<int> divisor[N],X[N];
    int main()
    {
    	int n,q,i,l,r,k,c,j;
    	for(i=1;i<=N;i++)
    	for(j=i;j<=N;j+=i)
    	divisor[j].push_back(i);
    	cin >> n >> q;
    	vector<int> :: iterator it;
    	for(i=1;i<=n;i++)
    	{
    	cin >> c;
    	for(it=divisor[c].begin();it!=divisor[c].end();it++)
    	X[*it].push_back(i);
       }	
    	while(q--)
    	{
    		cin >> l >> r >> k;
    		c=upper_bound(X[k].begin(),X[k].end(),r)- upper_bound(X[k].begin(),X[k].end(),l-1);
    		cout << c << endl;
    	}
    	return 0;
    }
375_D. Tree and Queries

You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will assume that the tree vertices are numbered by integers from 1 to n. Then we represent the color of vertex v as cv. The tree root is a vertex with number 1.

In this problem you need to answer to m queries. Each query is described by two integers vj, kj. The answer to query vj, kj is the number of such colors of vertices x, that the subtree of vertex vj contains at least kj vertices of color x.

You can find the definition of a rooted tree by the following link: http://en.wikipedia.org/wiki/Tree_(graph_theory).

Input

The first line contains two integers n and m (2 ≤ n ≤ 105; 1 ≤ m ≤ 105). The next line contains a sequence of integers c1, c2, ..., cn (1 ≤ ci ≤ 105). The next n - 1 lines contain the edges of the tree. The i-th line contains the numbers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — the vertices connected by an edge of the tree.

Next m lines contain the queries. The j-th line contains two integers vj, kj (1 ≤ vj ≤ n; 1 ≤ kj ≤ 105).

Output

Print m integers — the answers to the queries in the order the queries appear in the input.

Examples

Input

8 5
1 2 2 3 3 2 3 3
1 2
1 5
2 3
2 4
5 6
5 7
5 8
1 2
1 3
1 4
2 3
5 3


Output

2
2
1
0
1


Input

4 1
1 2 3 4
1 2
2 3
3 4
1 1


Output

4

Note

A subtree of vertex v in a rooted tree with root r is a set of vertices {u : dist(r, v) + dist(v, u) = dist(r, u)}. Where dist(x, y) is the length (in edges) of the shortest path between vertices x and y.

Example input:
8 5
1 2 2 3 3 2 3 3
1 2
1 5
2 3
2 4
5 6
5 7
5 8
1 2
1 3
1 4
2 3
5 3

4 1
1 2 3 4
1 2
2 3
3 4
1 1


Example output:
2
2
1
0
1

4


Tags: data structures
dfs and similar
trees
Difficulty rating: 2400

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int a[N], re[N];
vector<int> g[N];
vector<pair<int, int>> q[N];
map<int, int> cnt[N], frq[N];
void dfs_solve(int u, int p) {
  cnt[u][a[u]] = 1;
  frq[u][1] = 1;
  for (int v : g[u])
    if (v != p) {
      dfs_solve(v, u);
      if (cnt[u].size() < cnt[v].size()) {
        swap(cnt[u], cnt[v]);
        swap(frq[u], frq[v]);
      }
      for (auto& j : cnt[v])
        while (j.second--) ++frq[u][++cnt[u][j.first]];
      cnt[v] = {};
    }
  for (auto& j : q[u]) {
    int k, i;
    tie(k, i) = j;
    re[i] = frq[u][k];
  }
}
int main() {
  int n, m, u, v, k;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  for (int i = 1; i < n; ++i) {
    scanf("%d %d", &u, &v);
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  for (int i = 0; i < m; ++i) {
    scanf("%d %d", &u, &k);
    q[u].emplace_back(k, i);
  }
  dfs_solve(1, 1);
  for (int i = 0; i < m; ++i) printf("%d\n", re[i]);
  return 0;
}
1042_F. Leaf Sets

You are given an undirected tree, consisting of n vertices.

The vertex is called a leaf if it has exactly one vertex adjacent to it.

The distance between some pair of vertices is the number of edges in the shortest path between them.

Let's call some set of leaves beautiful if the maximum distance between any pair of leaves in it is less or equal to k.

You want to split all leaves into non-intersecting beautiful sets. What is the minimal number of sets in such a split?

Input

The first line contains two integers n and k (3 ≤ n ≤ 10^6, 1 ≤ k ≤ 10^6) — the number of vertices in the tree and the maximum distance between any pair of leaves in each beautiful set.

Each of the next n - 1 lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ n) — the description of the i-th edge. 

It is guaranteed that the given edges form a tree.

Output

Print a single integer — the minimal number of beautiful sets the split can have. 

Examples

Input

9 3
1 2
1 3
2 4
2 5
3 6
6 7
6 8
3 9


Output

2


Input

5 3
1 2
2 3
3 4
4 5


Output

2


Input

6 1
1 2
1 3
1 4
1 5
1 6


Output

5

Note

Here is the graph for the first example:

<image>

Example input:
6 1
1 2
1 3
1 4
1 5
1 6

9 3
1 2
1 3
2 4
2 5
3 6
6 7
6 8
3 9

5 3
1 2
2 3
3 4
4 5


Example output:
5
2
2

Tags: data structures
dfs and similar
dsu
graphs
greedy
sortings
trees
Difficulty rating: 2400

Solution:
#include <bits/stdc++.h>
using namespace std;
vector<int> g[10000005];
int n, k, ans;
int dfs(int u, int f) {
  if (g[u].size() == 1) return 1;
  vector<int> t;
  for (int v = 0; v < g[u].size(); ++v)
    if (g[u][v] != f) t.push_back(dfs(g[u][v], u));
  sort(t.begin(), t.end());
  while (t.size() >= 2) {
    if (t.back() + t[t.size() - 2] <= k) break;
    ++ans;
    t.pop_back();
  }
  return t.back() + 1;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].push_back(v), g[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i)
    if (g[i].size() > 1) {
      dfs(i, 0);
      break;
    }
  printf("%d", ans + 1);
}
149_E. Martian Strings

During the study of the Martians Petya clearly understood that the Martians are absolutely lazy. They like to sleep and don't like to wake up. 

Imagine a Martian who has exactly n eyes located in a row and numbered from the left to the right from 1 to n. When a Martian sleeps, he puts a patch on each eye (so that the Martian morning doesn't wake him up). The inner side of each patch has an uppercase Latin letter. So, when a Martian wakes up and opens all his eyes he sees a string s consisting of uppercase Latin letters. The string's length is n. 

"Ding dong!" — the alarm goes off. A Martian has already woken up but he hasn't opened any of his eyes. He feels that today is going to be a hard day, so he wants to open his eyes and see something good. The Martian considers only m Martian words beautiful. Besides, it is hard for him to open all eyes at once so early in the morning. So he opens two non-overlapping segments of consecutive eyes. More formally, the Martian chooses four numbers a, b, c, d, (1 ≤ a ≤ b < c ≤ d ≤ n) and opens all eyes with numbers i such that a ≤ i ≤ b or c ≤ i ≤ d. After the Martian opens the eyes he needs, he reads all the visible characters from the left to the right and thus, he sees some word.

Let's consider all different words the Martian can see in the morning. Your task is to find out how many beautiful words are among them.

Input

The first line contains a non-empty string s consisting of uppercase Latin letters. The strings' length is n (2 ≤ n ≤ 105). The second line contains an integer m (1 ≤ m ≤ 100) — the number of beautiful words. Next m lines contain the beautiful words pi, consisting of uppercase Latin letters. Their length is from 1 to 1000. All beautiful strings are pairwise different.

Output

Print the single integer — the number of different beautiful strings the Martian can see this morning.

Examples

Input

ABCBABA
2
BAAB
ABBA


Output

1

Note

Let's consider the sample test. There the Martian can get only the second beautiful string if he opens segments of eyes a = 1, b = 2 and c = 4, d = 5 or of he opens segments of eyes a = 1, b = 2 and c = 6, d = 7. 

Example input:
ABCBABA
2
BAAB
ABBA


Example output:
1


Tags: string suffix structures
strings
Difficulty rating: 2300

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3, M = 1003;
int a[N], b[N], p[M], n, m;
char s[N], r[N], t[M];
bool v[M];
void work(char* s, int* a) {
  int i = 2, j = 0;
  for (; i <= m; p[i++] = j) {
    while (j && t[j + 1] != t[i]) j = p[j];
    if (t[j + 1] == t[i]) ++j;
  }
  for (i = 1, j = 0; i <= n; a[i++] = j) {
    while (j && t[j + 1] != s[i]) j = p[j];
    if (t[j + 1] == s[i]) ++j;
  }
}
int main() {
  int i, q, w = 0;
  scanf("%s%d", s + 1, &q), n = strlen(s + 1), memcpy(r, s, n + 1),
                            reverse(r + 1, r + n + 1);
  while (q--) {
    if (scanf("%s", t + 1), m = strlen(t + 1), m > 1) {
      memset(v, 0, m + 1), work(s, a), reverse(t + 1, t + m + 1), work(r, b);
      for (i = 1; i <= n; v[a[i]] = 1, ++i)
        if (v[m - b[n - i + 1]]) {
          ++w;
          break;
        }
    }
  }
  printf("%d", w);
  return 0;
}