Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

sx,sy,gx,gy=map(int,input().split())
n=int(input())
g = [[] for i in range(n+2)]
data=[(sx,sy,0)]
for i in range(n):
    data.append(tuple(map(int,input().split())))
data.append((gx,gy,0))
for i in range(n+2):
    for j in range(i+1,n+2):
        tmp=max(0,((data[i][0]-data[j][0])**2+(data[i][1]-data[j][1])**2)**0.5-data[i][2]-data[j][2])
        g[i].append((tmp,j))
        g[j].append((tmp,i))

def dijkstra(g,st,ed):
    from heapq import heappush, heappop, heapify
    dist=[2828427127 for i in range(len(g))]
    dist[st]=0
    que=[]
    heappush(que,(0,st))
    while que:
        c,v=heappop(que)
        if dist[v]<c:
            continue
        for i in range(len(g[v])):
            if g[v][i][0]+c<dist[g[v][i][1]]:
                dist[g[v][i][1]]=g[v][i][0]+c
                heappush(que,(g[v][i][0]+c,g[v][i][1]))
    return dist[ed]

print(dijkstra(g,0,n+1))

### FIXED CODE ###

from heapq import heappush, heappop
sx,sy,gx,gy=map(int,input().split())
n=int(input())
g = [[] for i in range(n+2)]
data=[(sx+sy*1j,0)]
for i in range(n):
    a,b,c=map(int,input().split())
    data.append((a+b*1j,c))
data.append((gx+gy*1j,0))

for i in range(n+2):
    for j in range(i+1,n+2):
        tmp=abs(data[i][0]-data[j][0])-data[i][1]-data[j][1]
        g[i].append((tmp,j))
        g[j].append((tmp,i))

dist=[2828427127 for i in range(n+2)]
visited=[False for i in range(n+2)]
dist[0]=0
que=[]
heappush(que,(0,0))

while que:
    c,v=heappop(que)
    if visited[v]:
        continue
    visited[v]=True
    for x in g[v]:
        if visited[x[1]]:
            continue
        tmp=x[0]+c if not x[0]<0 else c
        if tmp<dist[x[1]]:
            dist[x[1]]=tmp
            heappush(que,(tmp,x[1]))
print(dist[n+1])
### BUGGY CODE ###

import bisect

x,y,z,k = map(int, input().split())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
cc = list(map(int, input().split()))

# A,Bでソート
score_ab = []
for a in aa:
    for b in bb:
        bisect.insort_right(score_ab, a+b)
# print(score_ab[::-1])
# A,B合計値の上位K個とCでソート
score = []
for s in score_ab[::-1][:k]:
    for c in cc:
        bisect.insort_right(score, s+c)
for s in score[::-1][:k]:
    print(s)

### FIXED CODE ###

x,y,z,k = map(int, input().split())
aa = sorted(list(map(int, input().split())))
bb = sorted(list(map(int, input().split())))
cc = sorted(list(map(int, input().split())))

ab = sorted(a+b for a in aa for b in bb)[:-k-1:-1]
abc = sorted(s+c for s in ab[:k] for c in cc)[:-k-1:-1]
print(*abc, sep='\n')
### BUGGY CODE ###

n = int(input())
s = input()
q = int(input())
k = list(map(int, input().split()))
for ki in k:
    D=M=DM=C=0
    #for i, c in enumerate(s):
    for i in range(n):
        c = s[i]
        if c == "D":
            D += 1
        elif c == "M":
            M += 1
            DM += D
        elif c == "C":
            C += DM
        if i >= ki-1:
            j = i-ki+1
            if s[j] == "D":
                D -= 1
                DM -= M
            elif s[j] == "M":
                M -= 1
    print(C)

### FIXED CODE ###

# コピペ https://atcoder.jp/contests/dwacon5th-prelims/submissions/7670569
# 自分の https://atcoder.jp/contests/dwacon5th-prelims/submissions/7822126
# とほぼ変わらないのに自分のものはTLEするので検証
n=int(input())
s=input()
q=int(input())
for i in list(map(int,input().split())):
    a=0
    v=[0,0,0]
    for j in range(n):
        if s[j]=="D":
            v[0]+=1
        elif s[j]=="M":
            v[1]+=1
            v[2]+=v[0]
        if j>=i:
            if s[j-i]=="D":
                v[0]-=1
                v[2]-=v[1]
            elif s[j-i]=="M":
                v[1]-=1
        if s[j]=="C":
            a+=v[2]
    print(a)
### BUGGY CODE ###

count = int(input())
a = list(map(int, input().split()))
total = 1
for i in a:
    total = total*i
if total>10**18:
    print(-1)
else:
    print(total)


### FIXED CODE ###

import sys
count = int(input())
a = list(map(int, input().split()))
#0が出た時点で終了する
for i in a:
    if i == 0:
        print(0)
        sys.exit()
#合計の計算        
total = 1
for j in a:
    total = total*j
    #10^18超えた時点で終了する
    if total>10**18:
        print(-1)
        sys.exit()

print(total)
### BUGGY CODE ###

import sys
from sys import stdin
input = stdin.readline
N,A=map(int, input().split())
x=[int(x) for x in input().split()]
x.append(0)
x.sort()
W=sum(x)
dp=[[[0]*(W+1) for _ in range(N+1)] for _ in range(N+1)]
for j in range(N+1):
    dp[j][0][0]=1
for j in range(1,N+1):
    xj=x[j]
    for k in range(1, N+1):
        for s in range(1, W+1):
            if s<xj:
                dp[j][k][s]=dp[j-1][k][s]
            else:
                dp[j][k][s]=dp[j-1][k][s]+dp[j-1][k-1][s-xj]

ans=0
for k in range(1,N+1):
    if k*A<=W:
        ans+=dp[N][k][k*A]
print(ans)


### FIXED CODE ###

import sys
from sys import stdin
input = stdin.readline

def solve(N, A, x):
    W=sum(x)
    dp=[[[0]*(W+1) for _ in range(N+1)] for _ in range(N+1)]
    for j in range(N+1):
        dp[j][0][0]=1
    for j in range(1,N+1):
        xj=x[j-1]
        for k in range(1, N+1):
            for s in range(1, W+1):
                if s<xj:
                    dp[j][k][s]=dp[j-1][k][s]
                else:
                    dp[j][k][s]=dp[j-1][k][s]+dp[j-1][k-1][s-xj]

    ans=0
    for k in range(1,N+1):
        if k*A<=W:
            ans+=dp[N][k][k*A]
        else:
            break
    return ans


def main(args):
    N,A=map(int, input().split())
    x=[int(x) for x in input().split()]
    ans=solve(N,A,x)
    print(ans)

if __name__ == '__main__':
    main(sys.argv[1:])
### BUGGY CODE ###

K = int(input())
a = 7
count = 1
for i in range(K):
    if a % K == 0:
        break
    a = (a * 10) + 7
    count += 1
if count == K + 1:
    print(-1)
else:
    print(count)

### FIXED CODE ###

K = int(input())
a = 7
count = 1
for i in range(K):
    if a % K == 0:
        break
    a = (a * 10 + 7) % K
    count += 1
if count == K + 1:
    print(-1)
else:
    print(count)
### BUGGY CODE ###

N = int(input())
L = list(map(int,input().split()))
ans = 0

for i in range(N-2) :
    for j in range(i+1 ,N-1) :
        for k in range(j+1 , N) :
            if (L[i] < L[j] + L[k]) and (L[j] < L[i] + L[k]) and(L[k] < L[i] + L[j]) :
                ans += 1

print(ans)

### FIXED CODE ###

import bisect

N = int(input())
L = list(map(int,input().split()))
L = sorted(L)
ans = 0

for i in range(N-2) :
    for j in range(i+1,N-1) :
        a = L[i] + L[j]
        X =bisect.bisect_left(L,a)
        ans += X- j - 1
        
    
print(ans)
### BUGGY CODE ###

from math import log10 as log

N = int(input())
A = [int(a) for a in input().split()]
if min(A) == 0:
    B = [0]
else:
    B = [log(b) for b in A]

ans = 1
if sum(B) > 18:
    ans = -1
else:
    for a in A:
        ans *= a
if ans > 10 **18:
    ans = -1
print(ans)


### FIXED CODE ###

from math import log10 as log

N = int(input())
A = [int(a) for a in input().split()]
ans = 1
if min(A) == 0:
    ans = 0
else:
    for a in A:
        ans *= a
        if ans > 10**18:
            ans = -1
            break
print(ans)
### BUGGY CODE ###

N,M = map(int,input().split())
A = [0]
C = [0] 
for i in range(M):
    a,b = map(int,input().split())
    A.append(a)
    c = list(map(int,input().split()))
    x = 0
    for j in range(1,N+1):
        if j in c:
            x += 2**(j-1)
    C.append(x)
inf = 10**9
dp = [[inf for i in range(M+1)] for i in range(2**N)]
dp[0][0] = 0
for i in range(2**N):
    for j in range(1,M+1):
        if i - C[j] < 0:
            dp[i][j] = min(dp[i][j-1],dp[i][j])
            continue
        for k in range(0,i+1):
            if k|C[j] == i:
                dp[i][j] = min(dp[i][j],dp[k][j-1]+A[j])
            dp[i][j] = min(dp[i][j],dp[i][j-1])
if dp[2**N-1][M] == inf:
    dp[2**N-1][M] = -1
print(dp[2**N-1][M])

### FIXED CODE ###

N,M = map(int,input().split())
A = [0]
C = [0] 
for i in range(M):
    a,b = map(int,input().split())
    A.append(a)
    c = list(map(int,input().split()))
    x = 0
    for j in range(1,N+1):
        if j in c:
            x += 2**(j-1)
    C.append(x)
inf = 10**9
dp = [inf for i in range(2**N)]
dp[0] = 0
for i in range(1,M+1):
    dp[C[i]] = min(dp[C[i]],A[i])
    for j in range(2**N):
        k =  j|C[i]
        dp[k] = min(dp[k],dp[j]+A[i])
if dp[2**N-1] == inf:
    dp[2**N-1] = -1
print(dp[2**N-1])
### BUGGY CODE ###


import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()

from collections import deque
def resolve():
    drc4 = [(-1, 0), (0, 1), (1, 0), (0, -1)]
    warp = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if (i, j) not in [(0, 0)] + drc4]

    H, W = map(int, inp().split())
    sy, sx = map(lambda x: int(x) - 1, inp().split())
    gy, gx = map(lambda x: int(x) - 1, inp().split())
    G = [inpS() for _ in range(H)]

    INF = 10 ** 7
    ans = [[INF] * W for _ in range(H)]

    q = deque()
    q.append((sy, sx, 0))
    ans[sy][sx] = 0

    while q:
        sy, sx, cost = q.popleft()
        for dy, dx in drc4:
            ny, nx = dy + sy, dx + sx
            if 0 <= ny < H and 0 <= nx < W and G[ny][nx] == "." and ans[ny][nx] > cost:
                ans[ny][nx] = cost
                q.append((ny, nx, cost))

        for dy8, dx8 in warp:
            ny8, nx8 = dy8 + sy, dx8 + sx
            if 0 <= ny8 < H and 0 <= nx8 < W and G[ny8][nx8] == "." and ans[ny8][nx8] > cost + 1:
                ans[ny8][nx8] = cost + 1
                q.append((ny8, nx8, cost + 1))

    res = ans[gy][gx] if ans[gy][gx] < INF else -1
    print(res)


if __name__ == "__main__":
    resolve()


### FIXED CODE ###


from collections import deque
def resolve():
    H, W = map(int, input().split())
    Ch, Cw = map(lambda x: int(x) - 1, input().split())
    Dh, Dw = map(lambda x: int(x) - 1, input().split())
    maze = [input() for _ in range(H)]

    INF = 10 ** 12
    path = [[INF] * W for _ in range(H)]

    q = deque()
    path[Ch][Cw] = 0
    q.append((Ch, Cw, 0))

    walk = [(0, 1), (0, -1), (-1, 0), (1, 0)]
    warp = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if (i, j) not in [(0, 0)] + walk]

    while q:
        x, y, s = q.popleft()
        for dx, dy in walk:
            nx = x + dx
            ny = y + dy
            if 0 <= nx < H and 0 <= ny < W and maze[nx][ny] == '.' and path[nx][ny] > s:
                path[nx][ny] = s
                q.appendleft((nx, ny, s))
        for dx, dy in warp:
            nx = x + dx
            ny = y + dy
            if 0 <= nx < H and 0 <= ny < W and maze[nx][ny] == '.' and path[nx][ny] > s + 1:
                path[nx][ny] = s + 1
                q.append((nx, ny, s + 1))

    ans = path[Dh][Dw] if path[Dh][Dw] < INF else -1
    print(ans)


if __name__ == "__main__":
    resolve()
### BUGGY CODE ###

from bisect import bisect_right
def main():
    n = int(input())
    A = sorted(list(map(int, input().split())))
    B = sorted(list(map(int, input().split())))
    C = sorted(list(map(int, input().split())))
    AB = [0] * n
    BC = [0] * n
    for i in range(n):
        AB[i] = n - bisect_right(B, A[i])
        BC[i] = n - bisect_right(C, B[i])
    ans = 0
    for ab in AB:
        if ab != 0:
            ans += sum(BC[-ab:])
    print(ans)

if __name__ == '__main__':
    main()


### FIXED CODE ###

from bisect import bisect_right, bisect_left
def main():
    n = int(input())
    A = sorted(list(map(int, input().split())))
    B = list(map(int, input().split()))
    C = sorted(list(map(int, input().split())))
    ans = 0
    for b in B:
        ans += bisect_left(A, b) * (n - bisect_right(C, b))
    print(ans)


if __name__ == '__main__':
    main()
### BUGGY CODE ###

N,K = map(int,input().split())
b = K
r = N-K
mod = 10**9+7
# rのしきりr+1にi個いれる→(r+1)Ci
# bはしきりb-1にi-1個入れる→(b-1)C(i-1)

for i in range(1,K+1):
    ans = 1
    for j in range(1,i):
        ans *= (r+1)-(j-1)
        ans %= mod
        ans *= (b-1)-(j-1)
        ans %= mod
        ans *= pow(j,mod-2,mod)
        ans *= pow(j,mod-2,mod)
        ans %= mod
    ans *= (r+1)-(i-1)
    ans *= pow(i,mod-2,mod)
    ans %= mod
    print(ans)

### FIXED CODE ###

N,K = map(int,input().split())
b = K
r = N-K
mod = 10**9+7
# rのしきりr+1にi個いれる→(r+1)Ci
# bはしきりb-1にi-1個入れる→(b-1)C(i-1)

ans = (r+1)
print(ans)
for i in range(2,K+1):
    ans *= (r+1)-(i-1)
    ans *= (b-1)-(i-2)
    ans *= pow(i,mod-2,mod)
    ans *= pow(i-1,mod-2,mod)
    ans %= mod
    print(ans)
### BUGGY CODE ###

import sys
from collections import defaultdict
from copy import deepcopy

N = int(input())
edge = []
color_dict = {}
repn = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edge.append((a, b))
    color_dict[(a, b)] = None
    repn[a].append(b)
    repn[b].append(a)

K = 0
for i in range(N):
    K = max(K, len(repn[i]))
color_set = set([i for i in range(1, K + 1)])
print(K)

q = [(0, K + 1)]
while q:
    u, color = q.pop()
    c_set = deepcopy(color_set)
    c_set.discard(color)
    for v in repn[u]:
        if color_dict[(u, v)] == None:
            c = c_set.pop()
            color_dict[(u, v)] = c
            color_dict[(v, u)] = c
            q.append((v, c))

for pair in edge:
    print(color_dict[pair])

### FIXED CODE ###

import sys
from collections import defaultdict
from copy import deepcopy
input = sys.stdin.readline

N = int(input())
edge = []
color_dict = {}
repn = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edge.append((a, b))
    color_dict[(a, b)] = None
    repn[a].append(b)
    repn[b].append(a)

K = 1
q = [(0, None)]
while q:
    u, color = q.pop()
    if color == 1:
        c = 2
    else:
        c = 1
    for v in repn[u]:
        if color_dict[(u, v)] == None:
            color_dict[(u, v)] = c
            color_dict[(v, u)] = c
            q.append((v, c))
            K = max(K, c)
            c += 1
            if c == color:
                c += 1
                

print(K)
for pair in edge:
    print(color_dict[pair])
### BUGGY CODE ###

n,w=map(int,input().split())
li=[]
for i in range(n):
    temp=[int(x) for x in input().split()]
    li.append(temp)
dp=[[[-1,-1] for j in range(w+1)] for i in range(n)]
dp[n-1][0]=[li[n-1][1],li[n-1][1]]
for i in range(n-1,-1,-1):
    for j in range(w,-1,-1):
        if j==0:
            dp[i][j]=li[i][1],li[i][1]
        if i==(n-1):
            if li[i][0]<=j:
                dp[i][j]=li[i][1],li[i][1]
            else:
                dp[i][j]=0,0
        else:
            mx=li[i][1]
            for k in range(i+1,n):
                if (j-li[i][0])>=li[k][0]:
                    val=dp[k][j-li[i][0]]
                    mx=max(mx,val[0]+li[i][1])
            val=dp[i+1][j]
            overall=max(mx,val[1])
            dp[i][j]=[mx,overall]
print(dp[0][w][1])

### FIXED CODE ###

from sys import stdin, gettrace
import sys
sys.setrecursionlimit(1000000)
if not gettrace():
    def input():
        return next(stdin)[:-1]
 
 
# def input():
#    return stdin.buffer.readline()

def recursion(li,i,n,w,dp):
    if i==n or w==0:
        return 0
    if li[i][0]>w:
        if dp[i+1][w]==-1:
            x=recursion(li,i+1,n,w,dp)
            dp[i+1][w]=x            
        else:
            x=dp[i+1][w]
        return x
    else:
        if dp[i+1][w]==-1:
            x=recursion(li,i+1,n,w,dp)
            dp[i+1][w]=x            
        else:
            x=dp[i+1][w]
        if dp[i+1][w-li[i][0]]==-1:
            y=recursion(li,i+1,n,w-li[i][0],dp)
            dp[i+1][w-li[i][0]]=y
        else:
            y=dp[i+1][w-li[i][0]]            
        return max(li[i][1]+y,x)

def main():
    n,w=[int(x) for x in input().split()]
    li=[]
    for i in range(n):
        temp=[int(x) for x in input().split()]
        li.append(temp)
    dp=[[-1 for j in range(w+1)] for i in range(n+1)]
    print(recursion(li,0,n,w,dp))
 
 
if __name__ == "__main__":
    main()
### BUGGY CODE ###

import math
n = int(input())
a = list(map(int,input().split()))
t = [0]*1000
s = []
for i in range(n):
    for j in range(2,1000):
        if(a[i]%j == 0):
            t[j] += 1
            a[i] //= j
            if(a[i] == 1):
                break
    if(a[i] != 1):
        s.append(a[i])
if(max(t) == n):
    print('not coprime')
elif(max(t) <= 1):
    z = 0
    for i in range(1,len(s)):
        for j in range(i):
            if(math.gcd(s[i],s[j]) > 1):
                z = 1
                break
    if(z == 1):
        print('setwise coprime')
    else:
        print('pairwise coprime')
else:
    print('setwise coprime')

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
r = max(a)+1
s = []
t = [0]*r
for i in range(r):
    s.append([])
for i in range(2,r):
    if(s[i] == []):
        for j in range(1,-(-r//i)):
            s[i*j].append(i)
for i in range(n):
    for j in range(len(s[a[i]])):
        t[s[a[i]][j]] += 1
if(max(t) == n):
    print('not coprime')
elif(max(t) <= 1):
    print('pairwise coprime')
else:
    print('setwise coprime')
### BUGGY CODE ###

N , M = map(int,input().split())

A = list(map(int,input().split()))

A.sort()

for i in range(M) :
    B , C = map(int,input().split())

    for j in range(B) :
        if A[j] < C :
            A[j] = C
        else :
            break

    A.sort()

print(int(sum(A)))

### FIXED CODE ###

N , M = map(int,input().split())

A = list(map(int,input().split()))

A.sort()

conv = []

for i in range(M) :
    B , C = map(int,input().split())
    conv.append([B,C])

sec = lambda A : A[1]
conv.sort(key=sec,reverse=True)

change = []
add = 0

# 大きいものからN個とる
for c in conv :
    for i in range(c[0]) :
        change.append(c[1])
        add += 1

    if(add >= N) :
        break

for i in range(min(N,len(change))) :
    if A[i] >= change[i] :
        break
    A[i] = change[i]

print(int(sum(A)))
### BUGGY CODE ###

import sys
input = sys.stdin.readline
import heapq

H,W=map(int,input().split())
sh,sw=map(int,input().split())
gh,gw=map(int,input().split())
MAP=[input().strip() for i in range(H)]

ANS=[[1<<30]*W for i in range(H)]

sh-=1
sw-=1
gh-=1
gw-=1

ANS[sh][sw]=0

Q=[(0,sh,sw)]

while Q:
    sc,x,y=heapq.heappop(Q)

    if sc>ANS[x][y]:
        continue

    for i,j in [(x-2,y-2),(x-2,y-1),(x-2,y),(x-2,y+1),(x-2,y+2),(x-1,y-2),(x-1,y-1),(x-1,y),(x-1,y+1),(x-1,y+2),(x,y-2),(x,y-1),(x,y+1),(x,y+2),(x+1,y-2),(x+1,y-1),(x+1,y),(x+1,y+1),(x+1,y+2),(x+2,y-2),(x+2,y-1),(x+2,y),(x+2,y+1),(x+2,y+2)]:
        if 0<=i<H and 0<=j<W:
            if MAP[i][j]=="." and abs(i-x)+abs(j-y)==1 and ANS[i][j]>sc:
                ANS[i][j]=sc
                heapq.heappush(Q,(ANS[i][j],i,j))
            elif MAP[i][j]=="." and abs(i-x)+abs(j-y)>1 and ANS[i][j]>sc+1:
                ANS[i][j]=sc+1
                heapq.heappush(Q,(ANS[i][j],i,j))

if ANS[gh][gw]==1<<30:
    print(-1)
else:
    print(ANS[gh][gw])



### FIXED CODE ###

import sys
input = sys.stdin.readline
import heapq

H,W=map(int,input().split())
sh,sw=map(int,input().split())
gh,gw=map(int,input().split())
MAP=[input().strip() for i in range(H)]

ANS=[[1<<30]*W for i in range(H)]

sh-=1
sw-=1
gh-=1
gw-=1

ANS[sh][sw]=0

def ca(sc,x,y):
    return sc*(10**8)+x*(10**4)+y

def inv(w):
    sc=w//(10**8)
    w%=(10**8)
    x=w//(10**4)
    y=w%(10**4)

    return sc,x,y

Q=[ca(0,sh,sw)]

while Q:
    sc,x,y=inv(heapq.heappop(Q))

    if sc>ANS[x][y]:
        continue

    for i,j in [(x-2,y-2),(x-2,y-1),(x-2,y),(x-2,y+1),(x-2,y+2),(x-1,y-2),(x-1,y-1),(x-1,y),(x-1,y+1),(x-1,y+2),(x,y-2),(x,y-1),(x,y+1),(x,y+2),(x+1,y-2),(x+1,y-1),(x+1,y),(x+1,y+1),(x+1,y+2),(x+2,y-2),(x+2,y-1),(x+2,y),(x+2,y+1),(x+2,y+2)]:
        if 0<=i<H and 0<=j<W:
            if MAP[i][j]=="." and abs(i-x)+abs(j-y)==1 and ANS[i][j]>sc:
                ANS[i][j]=sc
                heapq.heappush(Q,ca(ANS[i][j],i,j))
            elif MAP[i][j]=="." and abs(i-x)+abs(j-y)>1 and ANS[i][j]>sc+1:
                ANS[i][j]=sc+1
                heapq.heappush(Q,ca(ANS[i][j],i,j))

if ANS[gh][gw]==1<<30:
    print(-1)
else:
    print(ANS[gh][gw])
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10 ** 7)
readline = sys.stdin.readline


def main():
    n,m = map(int, readline().split())
    val = str(readline().strip())
    ans = 0
    for i in range(n):
        for j in range(i,n):
            if int(val[i:j+1]) % m == 0:
                ans += 1
    print(ans)


if __name__ == '__main__':
    main()
    

### FIXED CODE ###

from collections import Counter

N, P = map(int, input().split())
S = list(map(int, input()))

def solve_2(S):
    ind = (i for i, x in enumerate(S, 1) if x % 2 == 0)
    return sum(ind)

def solve_5(S):
    ind = (i for i, x in enumerate(S, 1) if x % 5 == 0)
    return sum(ind)

def solve(S,P):
    if P == 2:
        return solve_2(S)
    if P == 5:
        return solve_5(S)
    S = S[::-1]
    T = [0] * len(S)
    T[0] = S[0] % P
    power = 1
    for i in range(1, len(S)):
        power *= 10
        power %= P
        T[i] = T[i-1] + power * S[i]
        T[i] %= P
    counter = Counter(T)
    return sum(x * (x - 1) // 2 for x in counter.values()) + counter[0]


print(solve(S,P))
### BUGGY CODE ###

p = int(input())


def div(a, b):
    n = len(a)
    m = len(b)
    res = [0] * (n - m + 1)
    tmp = pow(b[-1], p - 2, p)
    for i in range(n - m, -1, -1):
        t = a[i + m - 1] * tmp % p
        res[i] = t
        for j in range(m):
            a[i + j] -= t * b[j]
            a[i + j] %= p
    return res


a = list(map(int, input().split()))
ans = [0] * p
f = [0] * (p + 1)
f[0] = 1
for i in range(p):
    for j in range(p, 0, -1):
        f[j] += f[j - 1]
        f[j - 1] *= -i
        f[j] %= p
for i in range(p):
    if a[i] == 1:
        fm = div(f[:], [-i, 1])
        t = 1
        r = 0
        for j, v in enumerate(fm):
            r += v * t
            t = t * i % p
        t = pow(r, p - 2, p)
        for j, v in enumerate(fm):
            ans[j] += v * t % p
for i in range(p):
    ans[i] %= p
print(' '.join(map(str, ans)))


### FIXED CODE ###

p = int(input())


def div(a, b):
    n = len(a)
    m = len(b)
    res = [0] * (n - m + 1)
    tmp = pow(b[-1], p - 2, p)
    for i in range(n - m, -1, -1):
        t = a[i + m - 1] * tmp % p
        res[i] = t
        for j in range(m):
            a[i + j] = (a[i + j] - t * b[j]) % p
    return res


a = list(map(int, input().split()))
ans = [0] * p
f = [0] * (p + 1)
f[0] = 1
for i in range(p):
    for j in range(p, 0, -1):
        f[j] += f[j - 1]
        f[j - 1] = -f[j - 1] * i
        f[j] %= p
for i in range(p):
    if a[i] == 1:
        fm = div(f[:], [-i, 1])
        t = 1
        r = 0
        for v in fm:
            r += v * t
            t = t * i % p
        t = pow(r, p - 2, p)
        for j in range(len(fm)):
            ans[j] += fm[j] * t
for i in range(p):
    ans[i] %= p
print(' '.join(map(str, ans)))
### BUGGY CODE ###

n = int(input())
l = list(map(int,input().split()))
ans = 0
l.sort()
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            if l[i]+l[j]>l[k]:
                ans += 1
            else:
                break
print(ans)

### FIXED CODE ###

n = int(input())
l = list(map(int,input().split()))
ans = 0
l.sort()

def bisec(i,k): #ギリギリokなj<=j_を探す
    ci = i
    ck = k
    i+=1
    k-=1
    j = int((i+k)/2)
    while k-i>1:
        if l[ci]+l[j]>l[ck]:
            k = j
            j = int((i+k)/2)
        else:
            i = j+1
            j = int((i+k)/2)
    if l[ci]+l[i]>l[ck]:
        return True,i
    elif l[ci]+l[i+1]>l[ck]:
        return True,i+1
    else:
        return False,-1

for i in range(n):
    for k in range(i+2,n):
        f,j = bisec(i,k)
        if f:
            ans += k-j
print(ans)
### BUGGY CODE ###

n, m = map(int, input().split())
a = list(map(int, input().split()))
acs = [0] * (n + 1)

for i in range(1, n + 1):
    acs[i] = (acs[i - 1] + a[i - 1]) % m
cnt = 0
for l in range(n):
    for r in range(l, n):
        if acs[r] == acs[l - 1]:
            cnt += 1
print(cnt)

### FIXED CODE ###

n, m = map(int, input().split())
a = list(map(int, input().split()))
table = {0: 0}
buf = 0
cnt = 0
for i in range(n):
    buf = (buf + a[i]) % m
    if buf in table:
        cnt += table[buf]
        table[buf] += 1
    else:
        table[buf] = 1
print(cnt + table[0])