405957: GYM102191 H Convex Polygons

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

H. Convex Polygonstime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

You are given a string of digits from 1 to 8, each digit represents one of the 8 directions in clockwise order as shown in the following picture. The picture also states the changes in the X and Y coordinates for each direction.

Count the number of substrings such that if you start at (0, 0) and follow the directional moves in the substring, you will draw one closed convex polygon, that is, you need to finish at (0, 0), all interior angles should be less than or equal to 180°, and the polygon should not intersect itself.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 3 × 105), the length of the string.

The second line contains a string of n digits from 1 to 8.

Output

Output on a single line with the number of substrings that draw out one closed convex polygon.

ExamplesInput
8
31753317
Output
3
Input
8
33228666
Output
1
Input
8
28863535
Output
0
Note

In the first sample test, substrings (1, 4), (2, 5), and (3, 6) draws the three convex polygons from left to right, respectively.

In the second sample test, the whole string represents a convex polygon, any other substring will be open, so the answer is 1.

In the third sample test, the whole string represents a closed polygon but it is not convex, other substrings draw open polygons, so the answer is 0.

加入题单

算法标签: