Calvin the robot lies in an infinite rectangular grid. Calvin's source code contains a list of n commands, each either 'U', 'R', 'D', or 'L' — instructions to move a single square up, right, down, or left, respectively. How many ways can Calvin execute a non-empty contiguous substrings of commands and return to the same square he starts in? Two substrings are considered different if they have different starting or ending indices.


-----Input-----

The first line of the input contains a single positive integer, n (1 ≤ n ≤ 200) — the number of commands.

The next line contains n characters, each either 'U', 'R', 'D', or 'L' — Calvin's source code.


-----Output-----

Print a single integer — the number of contiguous substrings that Calvin can execute and return to his starting square.


-----Examples-----
Input
6
URLLDR

Output
2

Input
4
DLUU

Output
0

Input
7
RLRLRLR

Output
12



-----Note-----

In the first case, the entire source code works, as well as the "RL" substring in the second and third characters.

Note that, in the third case, the substring "LR" appears three times, and is therefore counted three times to the total result.

Difficulty: interview

Starter code:

Solutions:
n = int(input())
s = input()

ans = 0
for i in range(n):
    for j in range(i + 1, n + 1):
        t = s[i:j]
        ans += t.count('U') == t.count('D') and t.count('L') == t.count('R')

print(ans)


k=0

a = int(input())
b = input()
u = [0]*205
r = [0]*205
l=[0]*205
d=[0]*205

for i in range(a):
    u[i+1] = u[i]
    r[i+1] = r[i]
    d[i+1] = d[i]
    l[i+1] = l[i]
    if (b[i] == 'U'):
        u[i+1]+=1
    elif b[i] == 'R':
        r[i+1]+=1
    elif b[i] == 'L':
        l[i+1]+=1
    else:
        d[i+1]+=1

for i in range(1, a+1):
    for j in range(i, a+1):
        if u[j]-u[i-1] == d[j]-d[i-1] and l[j]-l[i-1] == r[j]-r[i-1]:
            k+=1

print(k)


n = int(input())
s = input().strip()
ans = 0
for i in range(n):
	for j in range(i+1, n+1):
		temp = s[i:j]
		if temp.count('U') == temp.count('D') and temp.count('R') == temp.count('L'):
			ans += 1
print(ans)

def f(t):
    return t.count('U') == t.count('D') and t.count('L') == t.count('R')
n, s = int(input()), input()
print(sum(f(s[i:j]) for i in range(n) for j in range(i + 1, n + 1)))

#!/usr/bin/env python3

DIR = {
    'U': (-1, 0),
    'R': (0, 1),
    'D': (1, 0),
    'L': (0, -1),
}

try:
    while True:
        n = int(input())
        s = input()

        result = 0
        for i in range(len(s)):
            y = x = 0
            for c in s[i:]:
                t = DIR[c]
                y += t[0]
                x += t[1]
                if y == x == 0:
                    result += 1
        print(result)

except EOFError:
    pass


def main():
    from collections import defaultdict
    
    d = defaultdict(int)
    d[(0, 0)] += 1
    n = int(input())
    s = input()
    x = y = 0
    res = 0
    for i in s:
        if i == 'U': x += 1
        elif i == 'D': x -= 1
        elif i == 'L': y += 1
        else: y -= 1
        res += d[(x, y)]
        d[(x, y)] += 1
    
    print(res)
    
    
main()

n = int(input())
s = input()
ans = 0
for i in range(n):
    for t in range(i + 1):
        s1 = s[t : i + 1]
        x = 0
        y = 0
        for e in range(len(s1)):
            if s1[e] == 'U':
                y += 1
            elif s1[e] == 'R':
                x += 1
            elif s1[e] == 'D':
                y -= 1
            else:
                x -= 1
        if x == y == 0:
            ans += 1
print(ans)


n = int(input())
s = input()
result = 0
for start in range(n):
    for finish in range(start + 1, n + 1):
        tmp = s[start:finish]
        if tmp.count("U") == tmp.count("D") and tmp.count("L") == tmp.count("R"):
            result += 1
print(result)

#!/usr/bin/env python3
n = int(input())
s = input()
ans = 0
for l in range(n-1):
    for r in range(l+1,n+1):
        y, x = 0, 0
        for c in s[l:r]:
            if c == 'U':
                y += 1
            elif c == 'D':
                y -= 1
            elif c == 'R':
                x += 1
            elif c == 'L':
                x -= 1
        if y == 0 and x == 0:
            ans += 1
print(ans)


n = int(input())
s = input()
res = 0
for l in range(1, n+1):
    for beg in range(0, n - l + 1):
        hor = 0
        ver = 0
        for i in range(beg, beg + l):
            if s[i] == 'U': ver += 1
            if s[i] == 'D': ver -= 1
            if s[i] == 'R': hor += 1
            if s[i] == 'L': hor -= 1
        if hor==0 and ver==0:res += 1
        
print(res)

n = int(input())
inst = input()

dir_ = dict(U=(0,1), D=(0,-1), L=(-1, 0), R=(1, 0))
ct = 0
for i in range(n):
    x, y = (0, 0)
    for j in range(i, n):
        dx, dy = dir_[inst[j]]
        x += dx
        y += dy
        ct += x == 0 and y == 0

print(ct)


# strs = [input() for _ in range(3)]

def main():
	n = int(input())
	s = input()

	cnt = 0
	for i in range(n):
		vert = horiz = 0
		for j in range(i, n):
			if s[j] == 'U':
				vert += 1
			elif s[j] == 'R':
				horiz += 1
			elif s[j] == 'D':
				vert -= 1
			else:
				horiz -= 1
			if vert == 0 and horiz == 0:
				cnt += 1

	print(cnt)




main()




n = int(input())
d = [(0, 0)]
for c in input():
    dpos = {'L': (-1, 0), 'R': (1, 0), 'U': (0, 1), 'D': (0, -1)}[c]
    d.append((d[-1][0] + dpos[0], d[-1][1] + dpos[1]))
print(sum(1 for i in range(n) for j in range(i + 1, n + 1) if d[i] == d[j]))


n = int(input())
ans = 0
s = input()
for i in range(n):
    pl = [0, 0]
    st = [0, 0]
    if s[i] == 'U':
        pl[0] += 1
                
    if s[i] == 'D':
        pl[0] -= 1
                
    if s[i] == 'R':
        pl[1] += 1
                
    if s[i] == 'L':
        pl[1] -= 1
    for j in range(i + 1, n):
        
        if s[j] == 'U':
            pl[0] += 1
        
        if s[j] == 'D':
            pl[0] -= 1
        
        if s[j] == 'R':
            pl[1] += 1
        
        if s[j] == 'L':
            pl[1] -= 1
        if st == pl:
            ans += 1
print(ans)   
            
            
        


n = int(input())
s = input()

ret = 0
for i in range(n):
    for j in range(i+1, n+1):
        count = {}
        for k in range(i, j):
            count[s[k]] = count.get(s[k], 0) + 1
        eq = len(count) > 1
        if "U" in count or "D" in count:
            if "D" not in count or "U" not in count:
                eq = False
            else:
                eq &= count["U"] == count["D"]
        if "L" in count or "R" in count:
            if "R" not in count or "L" not in count:
                eq = False
            else:
                eq &= count["L"] == count["R"]
        ret += eq
print(ret)


from itertools import accumulate

n = int(input())
S = {'D': (0, -1), 'U': (0, 1), 'R': (1, 0), 'L':(-1, 0)}
line = [S[c] for c in input()]
prefix_x = list(accumulate([0] + line, lambda p, q: p + q[0]))
prefix_y = list(accumulate([0] + line, lambda p, q: p + q[1]))
ans = 0
for i in range(n+1):
    for j in range(i+1, n+1):
        if prefix_x[j] == prefix_x[i] and prefix_y[j] == prefix_y[i]:
            ans += 1
print(ans)
    


n = int(input())
s = input()
ans = 0
for i in range(len(s)):
    for j in range(i + 1, len(s)):
        m = {'L':0, 'R': 0, 'U':0, 'D':0}
        s1 = s[i:j + 1]
        for x in s1:
            m[x] += 1
        if m['L'] == m['R'] and m['U'] == m['D']:
            ans += 1
print(ans)


n = int(input())
s = input().rstrip()

count = 0
for i in range(len(s)):
    for j in range(i, len(s)):
        x, y = 0, 0
        for c in s[i:j+1]:
            if c == 'U':
                y += 1
            elif c == 'D':
                y -= 1
            elif c == 'L':
                x += 1
            elif c == 'R':
                x -= 1
        if x == 0 and y == 0:
            count += 1

print(count)


import collections
import math

n = int(input())
s = input()
ans = 0
for i in range(len(s)):
    for j in range(i, len(s)):
        t = s[i:j+1]
        if t.count('U') == t.count('D') and t.count('L') == t.count('R'):
            ans += 1
print(ans)
              


n=int(input())
s=' '+input()

r=[(0,0)]
for i in range(1,n+1):
  x,y=r[i-1]
  if s[i]=="U": y+=1
  if s[i]=="D": y-=1
  if s[i]=="L": x-=1
  if s[i]=="R": x+=1
  r+=[(x,y)]
ans=0
for i in range(1,n):
  for j in range(i+1,n+1):
    if (0,0)==(r[j][0]-r[i-1][0],r[j][1]-r[i-1][1]): ans+=1
print(ans)


n = int(input())
l = input()
v = []
h = []
for c in l:
    if c == 'U':
        v += [1]
    elif c == 'D':
        v += [-1]
    else:
        v += [0]
    if c == 'R':
        h += [1]
    elif c == 'L':
        h += [-1]
    else:
        h += [0]
for i in range(1,n):
    v[i] += v[i-1]
    h[i] += h[i-1]

v = [0] + v
h = [0] + h
a = 0
for i in range(n):
    for j in range(i,n):
        if h[j+1] == h[i] and v[j+1] == v[i]:
            a += 1
print(a)
    


le = int(input())
s = input()
l = [[0,0]]
curr = [0,0]
for i in range(le):
    c = s[i]
    if c == 'U':
        curr[1] += 1
    elif c == 'R':
        curr[0] += 1
    elif c == 'L':
        curr[0] -= 1
    else:
        curr[1] -= 1
    l.append(curr[:])
l2 = []
for i in range(len(l)):
    if not(l[i] in l2):
        l2.append(l[i][:])
l3 = [0]*len(l2)

for i in range(len(l2)):
    for j in range(len(l)-1,-1,-1):
        if l2[i] == l[j]:
            l3[i]+=1
            l.pop(j)
out = 0
for val in l3:
    out += (val)*(val-1)//2
print(out)


n = int(input())
data = input()
answer = 0
for i in range(n):
    for j in range(n):
        if i <= j:
            help = data[i:j + 1]
            L = help.count('L')
            R = help.count('R')
            U = help.count('U')
            D = help.count('D')
            if L == R and U == D:
                answer += 1
print(answer)

# You lost the game.
n = int(input())
ch = str(input())
r = 0
a = 0
b = 0
T = [[0,0]]
for k in range(n):
    if ch[k] == "U":
        a += 1
    elif ch[k] == "D":
        a -= 1
    elif ch[k] == "R":
        b += 1
    else:
        b -= 1
    T += [[a,b]]

for i in range(1,n+1):
    for j in range(i):
        if T[j] == T[i]:
            r+=1
            
print(r)



def check(st):
    cnt = [st.count(ch) for ch in "UDRL"]
    return cnt[0] == cnt[1] and cnt[2] == cnt[3]

n = int(input())
s = input().strip()

ans = int(0)
for l in range(2, n+1):
    for i in range(0, n-l+1):
        if check(s[i:i+l]):
            ans += 1

print(ans)