An avid hiker keeps meticulous records of their hikes. During the last hike that took exactly $\textit{steps}$ steps, for every step it was noted if it was an uphill, $\mbox{U}$, or a downhill, $\mbox{D}$ step. Hikes always start and end at sea level, and each step up or down represents a $1$ unit change in altitude. We define the following terms:

A mountain is a sequence of consecutive steps above sea level, starting with a step up from sea level and ending with a step down to sea level.  
A valley is a sequence of consecutive steps below sea level, starting with a step down from sea level and ending with a step up to sea level.

Given the sequence of up and down steps during a hike, find and print the number of valleys walked through. 

Example  

$steps=8$
$path=[DDUUUUDD]$  

The hiker first enters a valley $2$ units deep.  Then they climb out and up onto a mountain $2$ units high.  Finally, the hiker returns to sea level and ends the hike.  

Function Description  

Complete the countingValleys function in the editor below.  

countingValleys has the following parameter(s):  

int steps: the number of steps on the hike  
string path: a string describing the path  

Returns  

int:  the number of valleys traversed 

Input Format

The first line contains an integer $\textit{steps}$, the number of steps in the hike. 

The second line contains a single string $\textit{path}$, of $\textit{steps}$ characters that describe the path.

Constraints

$2\leq steps\leq10^6$  
$path[i]\in\{UD\}$

Sample Input
8
UDDDUDUU

Sample Output
1

Explanation

If we represent _ as sea level, a step up as /, and a step down as \, the hike can be drawn as:

_/\      _
   \    /
    \/\/

The hiker enters and leaves one valley.

Source: hackerrank | Difficulty: EASY
Tags: Implementation


Solutions:
N = int(input())
S = input()
L = 0
V = 0
for s in S:
	if s == 'U':
		L += 1
		if L == 0:
			V += 1
	else:
		L -= 1
print(V)


n = int(input())
steps = input()
(num_valleys, curr_level) = (0, 0)
for step in steps:
	if curr_level == 0 and step == 'D':
		num_valleys += 1
	if step == 'U':
		curr_level += 1
	else:
		curr_level -= 1
print(num_valleys)


N = int(input().strip())
String = input().strip()
level = 0
count = 0
for i in String:
	if i == 'U':
		level += 1
	elif i == 'D':
		level -= 1
	if level == 0:
		if prev < level:
			count += 1
	prev = level
print(count)


N = int(input())
steps = input().strip()
valleys = 0
altitude = 0
for step in steps:
	if step == 'U':
		altitude += 1
		if altitude == 0:
			valleys += 1
	else:
		altitude -= 1
print(valleys)


n = int(input())
s = input().lower()
mar = 0
v = 0
i = 0
a = 0
for step in s:
	if step == 'u':
		i += 1
	else:
		i -= 1
	if i < mar and a == 0:
		v += 1
		a = 1
	elif i >= mar:
		a = 0
print(v)


n = input().strip()
trip = input().strip()
curlevel = 0
valleys = 0
for c in trip:
	if c == 'U':
		curlevel += 1
	else:
		curlevel -= 1
	if curlevel == 0 and c == 'U':
		valleys = valleys + 1
print(str(valleys))


l = 0
v = 0
input()
for i in input():
	if i == 'D':
		l -= 1
	else:
		l += 1
	if l == 0 and i == 'U':
		v += 1
print(v)


n = int(input())
a = input()
direction = 0
height = 0
mountains = 0
valleys = 0
for s in a:
	if direction == 0:
		direction = 1 if s == 'U' else -1
	height += 1 if s == 'U' else -1
	if height == 0:
		if direction == -1:
			valleys += 1
		direction = 0
print(valleys)


import sys
n = int(input().strip())
a = input().strip()
sea = 0
count = 0
for el in a:
	prev = sea
	if el == 'U':
		sea += 1
	if el == 'D':
		sea -= 1
	if prev < 0 and sea == 0:
		count += 1
print(count)


n = int(input())
steps = input()
level = 0
valleys = 0
height = []
for step in steps:
	if step == 'U':
		level += 1
	elif step == 'D':
		level -= 1
	height.append(level)
for x in range(1, len(height)):
	if height[x - 1] < 0 and height[x] == 0:
		valleys += 1
print(valleys)


n = int(input())
t = input()
(height, valleys) = (0, 0)
for i in t:
	if i == 'U':
		height += 1
		if height == 0:
			valleys += 1
	else:
		height -= 1
print(valleys)


n = int(input())
b = input()
c = 0
g = 0
j = g
v = 0
a = []
for t in range(len(b)):
	a.append(b[t])
for i in range(len(a)):
	if a[i] == 'U':
		g = j
		j = j + 1
	if a[i] == 'D':
		g = j
		j = j - 1
	if j == 0 and g < 0:
		v += 1
print(v)


num = int(input())
valley = list(input())
count = 0
deep = [0, 1]
for i in range(num):
	if valley[i].lower() == 'd':
		count -= 1
	elif valley[i].lower() == 'u':
		count += 1
	if count < 0 and deep[1] == 1:
		deep[0] += 1
	if count >= 0:
		deep[1] = 1
	else:
		deep[1] = 0
print(deep[0])


n = int(input())
chars = input()
valleys = 0
cur_level = 0
for char in chars:
	if char == 'U':
		cur_level += 1
		if cur_level == 0:
			valleys += 1
	elif char == 'D':
		cur_level -= 1
print(valleys)


n = int(input().strip())
l = list(input().strip())[:n]
counter = level = 0
inv = 0
for char in l:
	if char == 'U':
		level += 1
	if char == 'D':
		level -= 1
	if char == 'D' and level == -1 and (inv == 0):
		inv = 1
	if char == 'U' and inv == 1:
		inv = 0
		counter += 1
print(counter)


n = int(input())
steps = input()
sea_level = 0
last_sea_level = 0
is_in_valley = False
valleys = 0
for x in range(len(steps)):
	last_sea_level = sea_level
	if steps[x] == 'D':
		sea_level -= 1
	else:
		sea_level += 1
	if sea_level == 0 and last_sea_level == -1:
		valleys += 1
print(valleys)


n = int(input().strip())
steps = input().strip()
counter = 0
valleys = 0
for x in range(0, n):
	previousCounter = counter
	if steps[x] == 'D':
		counter -= 1
	else:
		counter += 1
	if counter == 0:
		if previousCounter < 0:
			valleys += 1
print(str(valleys))


a = input()
b = input()
counter = 0
level = 0
for x in range(0, len(b)):
	if b[x] is 'U':
		level += 1
		if level is 0:
			counter += 1
	elif b[x] is 'D':
		level -= 1
print(counter)


input()
l = 0
v = 0
for s in input():
	if s == 'U':
		l += 1
	else:
		l -= 1
		if l == -1:
			v += 1
print(v)


steps = int(input())
steps_path = input()
current_height = 0
valley_counter = 0
for step in steps_path:
	if step == 'U':
		current_height += 1
	else:
		if current_height == 0:
			valley_counter += 1
		current_height -= 1
print(valley_counter)


steps = int(input())
characters = input()
level = 0
res = 0
for i in characters:
	if i == 'U':
		level += 1
	elif i == 'D':
		level -= 1
	if level == 0 and i == 'U':
		res += 1
print(res)


n = int(input())
steps = input()
l = []
for x in steps:
	l.append(x)
level = 0
mountain = 0
valley = 0
for step in l:
	if step == 'U':
		if level == 0:
			mountain += 1
		level += 1
	if step == 'D':
		if level == 0:
			valley += 1
		level -= 1
print(valley)


import sys
buf = []
while True:
	line = sys.stdin.readline().strip('\n')
	if line:
		buf.append(line)
	else:
		break
if buf:
	n = int(buf[0])
	num_down = 0
	num_up = 0
	prev_step = ''
	val = 0
	for i in range(n):
		step = buf[1][i]
		if step == 'D':
			num_down += 1
			if num_down == 1 and (not num_up):
				val += 1
		if step == 'U':
			num_up += 1
		if num_up == num_down:
			num_up = 0
			num_down = 0
		prev_step = step
	print(val)


n = int(input().strip())
arr = str(input().strip())
level = 0
count = 0
for i in range(0, n):
	if arr[i] == 'U':
		level += 1
	elif arr[i] == 'D':
		if level == 0:
			count += 1
		level -= 1
print(count)


n = input()
e = 0
v = 0
inv = False
for c in input():
	if (e == 0) & (inv == False) & (c == 'D'):
		inv = True
		v = v + 1
	if (e == -1) & (c == 'U'):
		inv = False
	if c == 'D':
		e = e - 1
	if c == 'U':
		e = e + 1
print(v)


n = int(input())
steps = input()
height = 0
valley = 0
for i in range(len(steps)):
	if steps[i] == 'D':
		height -= 1
	else:
		height += 1
		if height == 0:
			valley += 1
print(valley)


n = int(input())
steps = input().strip()
pos = 0
valleys = 0
for c in steps:
	if c == 'U':
		npos = pos + 1
	else:
		npos = pos - 1
	if pos == -1 and npos == 0:
		valleys += 1
	pos = npos
print(valleys)


input()
steps = input()
valleys = 0
elevation = 0
was_below_sea_level = False
for step in steps:
	elevation += 1 if step == 'U' else -1
	if elevation < 0:
		was_below_sea_level = True
	elif elevation is 0 and was_below_sea_level:
		valleys += 1
		was_below_sea_level = False
print(valleys)


n = int(input())
l = input()
sum = 0
count = 0
i = 0
while i < n:
	if l[i] == 'U':
		sum += 1
	else:
		sum -= 1
		if sum == -1:
			for j in range(i + 1, n):
				if l[j] == 'D':
					sum -= 1
				else:
					sum += 1
					if sum == 0:
						count += 1
						i = j
						break
	i += 1
print(count)


numSteps = int(input())
steps = input()
(valleys, currentLevel) = (0, 0)
for i in range(numSteps):
	if steps[i] == 'U':
		currentLevel += 1
	else:
		currentLevel -= 1
	if currentLevel == 0 and steps[i] == 'U':
		valleys += 1
print(valleys)


N = int(input())
steps = input()
count = 0
valleys = 0
for c in steps:
	if c == 'U':
		count += 1
		if count == 0:
			valleys += 1
	elif c == 'D':
		count -= 1
print(valleys)


n = int(input())
path = input()
seaLevel = 0
prevHeight = 0
height = 0
i = 0
numValleys = 0
while i < n:
	prevHeight = height
	if path[i] == 'U':
		height += 1
	else:
		height -= 1
	if height == seaLevel and prevHeight == -1:
		numValleys += 1
	i += 1
print(numValleys)


n = input()
s = input()
x = 0
v = 0
c = -1
for i in s:
	x = x + 1 if i == 'U' else x - 1
	if x < 0:
		c = 0
	if c == 0 and x == 0:
		v += 1
		c = -1
else:
	print(v)


import sys
n = int(input().strip())
steps = input().strip()
valley_count = 0
elevation = 0
is_mountain = False
is_valley = False
for step in steps:
	if elevation == 0:
		if step == 'U':
			elevation += 1
			is_mountain = True
		else:
			elevation -= 1
			is_valley = True
	elif elevation > 0:
		if step == 'U':
			elevation += 1
		else:
			elevation -= 1
			if elevation == 0:
				is_mountain = False
	elif step == 'U':
		elevation += 1
		if elevation == 0:
			is_valley = False
			valley_count += 1
	else:
		elevation -= 1
print(valley_count)


input()
steps = input()
altitude = 0
valleys = 0
for step in steps:
	if altitude == 0 and step == 'D':
		valleys += 1
	altitude = altitude + 1 if step == 'U' else altitude - 1
print(valleys)


n = int(input())
l = input()
steps = []
count = 0
valley = 0
steps.append(0)
for i in range(len(l)):
	if l[i] == 'U':
		count = count + 1
		steps.append(count)
	else:
		count = count - 1
		steps.append(count)
for j in range(n):
	if steps[j] == 0 and steps[j + 1] == -1:
		valley = valley + 1
print(valley)


n = int(input().strip())
walk = input().strip()
sealevel = 0
valleycount = 0
for x in walk:
	if x == 'U':
		sealevel += 1
	elif x == 'D':
		if sealevel == 0:
			valleycount += 1
		sealevel -= 1
print(valleycount)


n = int(input())
arr = [x for x in input()]
c = 0
x = 0
flag = 0
for i in range(n):
	if arr[i] == 'U':
		c = c + 1
	else:
		c = c - 1
	if c < 0 and flag == 0:
		flag = 1
		x = x + 1
	elif c >= 0:
		flag = 0
print(x)


import re
a = input()
data = input()
walk = data.replace('U', ' +1').replace('D', ' -1').split(' ')
f = [int(x) for x in walk[1:]]
sum = 0
valeys = 0
mountains = 0
for i in range(len(f)):
	sum = sum + f[i]
	if sum == 0 and f[i] > 0:
		valeys += 1
	if sum == 0 and f[i] < 0:
		mountains += 1
print(valeys)


steps = int(input())
walk = str(input().strip())
track = 0
change = 0
level = 0
val = 0
for i in walk:
	if i == 'U':
		if track < 0:
			level = -1
		elif track > 0:
			level = 1
		else:
			level = 0
		track = track + 1
		if track == 0:
			change += 1
	if i == 'D':
		if track < 0:
			level = -1
		elif track > 0:
			level = 1
		else:
			level = 0
		track = track - 1
		if track == 0:
			change = 1
		else:
			change = 0
	if change == 1 and level == -1:
		val = val + 1
print(val)


n = int(input())
line = input()
currentLevel = 0
valleys = 0
for i in range(n):
	if line[i] == 'U':
		if currentLevel == -1:
			valleys += 1
		currentLevel += 1
	elif line[i] == 'D':
		currentLevel -= 1
print(valleys)


n = int(input())
seq = input()
steps = 0
valleys = 0
in_valley = False
for i in range(n):
	if seq[i] == 'U':
		steps += 1
	else:
		steps -= 1
	if steps == 0 and seq[i] == 'U':
		valleys += 1
print(valleys)


import sys
n = n = int(input().strip())
walk = list(str(input()))
current = 0
num_valleys = 0
is_valley = False
for step in walk:
	if step == 'U':
		if is_valley:
			if current == -1:
				num_valleys += 1
				is_valley = False
		current += 1
	elif step == 'D':
		if current == 0:
			is_valley = True
		current -= 1
print(num_valleys)


import sys
n = int(input().strip())
steps = input().upper()
level = 0
valley = 0
above = True
for char in steps:
	if char == 'D':
		level -= 1
	else:
		level += 1
	if above and level < 0:
		above = False
		valley += 1
	elif not above and level == 0:
		above = True
print(valley)


elevation = 0
valleys = 0
n = int(input().strip())
height = input()
for movement in height:
	if movement == 'D':
		elevation -= 1
	if movement == 'U':
		elevation += 1
		if elevation == 0:
			valleys += 1
print(valleys)


import sys
n = int(input().strip())
steps = input().strip()
valleys = 0
height = 0
lastheight = 0
for step in steps:
	if step == 'U':
		height += 1
	else:
		height -= 1
	if height == -1 and lastheight == 0:
		valleys += 1
	lastheight = height
print(valleys)


numElements = int(input().strip())
steps = input()
altitude = 0
numValleys = 0
valleyFlag = 0
for step in steps:
	if step == 'D':
		if altitude == 0:
			valleyFlag = 1
		altitude = altitude - 1
	elif step == 'U':
		if altitude == -1:
			valleyFlag = 0
			numValleys = numValleys + 1
		altitude = altitude + 1
print(int(numValleys))


input()
steps = input()
height = 0
v = 0
for step in steps:
	if step == 'U':
		height += 1
		if height == 0:
			v += 1
	elif step == 'D':
		height -= 1
print(v)


input()
journey = input()
(height, valleys) = (0, 0)
for step in journey:
	if step == 'U':
		height += 1
	elif step == 'D':
		if height == 0:
			valleys += 1
		height -= 1
print(valleys)


num = input()
string = input()
list1 = list(string)

def mon(num, list1):
	key = 0
	count = 0
	list2 = [0]
	new_list = []
	list3 = []
	for i in list1:
		if i == 'D':
			key = key - 1
			new_list += [key]
		if i == 'U':
			key = key + 1
			new_list += [key]
	for a in new_list:
		if a <= 0:
			list2 += [a]
	for b in range(len(list2) - 1):
		if list2[b] == 0 and list2[b + 1] < 0:
			count += 1
	return count
print(mon(num, list1))


n = int(input().strip())
steps = [1 if x == 'U' else -1 for x in list(input())]
cumsum = []
current_level = 0
for s in steps:
	current_level += s
	cumsum.append(current_level)
num_valley = 0
valley_start = True
for (ind, l) in enumerate(cumsum):
	if ind == 0:
		continue
	last_l = cumsum[ind - 1]
	if l < 0 and last_l >= 0:
		valley_start = True
	elif l >= 0 and last_l < 0 and (valley_start is True):
		num_valley += 1
		valley_start = False
print(num_valley)


N = int(input())
steps = input()
height = 0
v_count = 0
for step in steps:
	if step == 'U':
		height += 1
	else:
		height -= 1
	if step == 'D' and height == -1:
		v_count += 1
print(v_count)


n = int(input())
arr = list(input())[:n]
level = 0
count = 0
for i in range(n):
	if arr[i] == 'U':
		level += 1
	else:
		level -= 1
	if level == -1 and arr[i] == 'D':
		count += 1
print(count)


n = int(input().strip())
N = input().strip()
elev = 0
valleys = 0
for s in N:
	if elev == 0 and s == 'D':
		valleys = valleys + 1
		elev = elev - 1
	elif s == 'U':
		elev = elev + 1
	elif s == 'D':
		elev = elev - 1
print(valleys)


import sys
n = int(input().strip())
seq = input().strip()
pos = 0
val_flag = 0
cnt = 0
for i in range(n):
	if seq[i] == 'U':
		pos += 1
	else:
		pos -= 1
	if val_flag == 0 and pos == -1 and (seq[i] == 'D'):
		val_flag = 1
		cnt += 1
	if val_flag and pos == 0 and (seq[i] == 'U'):
		val_flag = 0
print(cnt)


n = int(input().strip())
hike = input().strip()
valley = 0
level = 0
for step in hike:
	if step == 'U':
		level += 1
		if level == 0:
			valley += 1
	else:
		level -= 1
		if level == 0:
			pass
print(valley)


n = int(input())
ls = [0]
valleys = 0
for i in input():
	ls.append(ls[-1] + (1 if i == 'U' else -1))
	if ls[-2:] == [-1, 0]:
		valleys += 1
print(valleys)


import sys
n = int(input().strip())
seq = input().strip()
height = 0
valley = False
num_valley = 0
for i in seq:
	if height == 0:
		if i == 'U':
			valley = False
			height += 1
		else:
			valley = True
			height -= 1
	elif valley and height < 0:
		if i == 'U':
			height += 1
			if height == 0:
				num_valley += 1
		else:
			height -= 1
	elif not valley and height > 0:
		if i == 'U':
			height += 1
		else:
			height -= 1
print(num_valley)


import sys
n = int(input().strip())
steps = list(input().strip())
valleys = 0
elevation = 0
for step in steps:
	prev_elevation = elevation
	if step == 'U':
		elevation += 1
	else:
		elevation -= 1
	if prev_elevation >= 0 and elevation < 0:
		valleys += 1
print(valleys)


steps = input()
direction = input()
valley = 0
count = 0
last = 0
for d in direction:
	if d == 'U':
		last = count
		count += 1
	else:
		last = count
		count -= 1
	if last < 0 and count == 0:
		valley += 1
print(valley)


count = int(input())
steps = list(input())
start = 0
valleys = 0
mountain = True
for i in range(count):
	if steps[i] == 'U':
		start += 1
	if steps[i] == 'D':
		start -= 1
	if start < 0:
		mountain = False
	if mountain == False and start >= 0:
		mountain = True
		valleys += 1
print(valleys)


num_steps = int(input().strip())
log = input().strip()
steps = list()
for step in range(0, num_steps):
	steps.append(log[step])
elevation = 0
prev_elevation = None
valleys = 0
for step in steps:
	if step == 'U':
		prev_elevation = elevation
		elevation += 1
	else:
		prev_elevation = elevation
		elevation -= 1
	if prev_elevation < 0 and elevation == 0:
		valleys += 1
print(valleys)


N = int(input().strip())
hike = list(input().strip())
valleys = 0
level = 0
for i in hike:
	if i == 'U':
		level += 1
		if level == 0:
			valleys += 1
	else:
		level -= 1
print(valleys)


n = int(input().strip())
travels = input().strip()
current_level = 0
under_the_sea = False
valleys = 0
for char in travels:
	last_level = current_level
	if char == 'U':
		current_level += 1
	elif char == 'D':
		current_level -= 1
	if last_level < 0:
		under_the_sea = True
	else:
		under_the_sea = False
	if under_the_sea is True and current_level == 0:
		valleys += 1
print(valleys)


import sys
s = int(input().strip())
steps = list(input().strip())
pos = 0
(mc, vc) = (0, 0)
valley = False
mountain = False
for ss in steps:
	if ss == 'U':
		pos += 1
	elif ss == 'D':
		pos -= 1
	else:
		0
	if pos == 0:
		if mountain:
			mc += 1
		elif valley:
			vc += 1
		else:
			0
	if pos > 0:
		mountain = True
		valley = False
	elif pos < 0:
		valley = True
		mountain = False
	else:
		0
print(vc)


num = int(input().strip())
steps = input().strip()
(valleyCnt, level) = (0, 0)
for step in steps:
	if step == 'D':
		level = level - 1
	elif step == 'U':
		level = level + 1
		if level == 0:
			valleyCnt += 1
print(valleyCnt)


n_steps = int(input().strip())
steps = input().strip()
level = 0
n_valley = 0
for char in steps:
	if char == 'U':
		level += 1
	else:
		level -= 1
		if level == -1:
			n_valley += 1
print(n_valley)


n = int(input())
steps = input()
list = []
counter = 0
num_groups = 0
for s in steps:
	if s == 'U':
		counter += 1
	else:
		counter -= 1
	if counter >= 0:
		list.append('p')
	else:
		list.append('n')
for i in range(len(list) - 1):
	if list[i] == 'n' and list[i + 1] == 'p':
		num_groups += 1
print(num_groups)


n = int(input())
track = input()

def find_number_of_valleys(track):
	current_level = 0
	number_of_valleys = 0
	for move in track:
		current_level = process_move(current_level, move)
		if move == 'U' and current_level == 0:
			number_of_valleys += 1
	return number_of_valleys

def process_move(current_level, move):
	return current_level + 1 if move == 'U' else current_level - 1
print(find_number_of_valleys(track))


n = int(input())
t = list(input())
level = 0
count = 0
for i in t:
	if i == 'U':
		level += 1
		if level == 0:
			count += 1
	elif i == 'D':
		level -= 1
print(count)


n = eval(input())
a = input()
w = int(n / 2)
c = 0
for i in range(0, len(a)):
	if a[i] == 'D':
		w -= 1
	elif a[i] == 'U':
		w += 1
		if w == int(n / 2):
			c += 1
print(c)


x = input()
steps = input()
count = 0
d = 0
place = []
for s in steps:
	if s == 'D':
		count -= 1
	else:
		count += 1
	place.append(count)
for i in range(1, len(place)):
	if place[i] == 0 and place[i - 1] == -1:
		d += 1
print(d)


import sys
N = int(input().strip())
Steps = input().strip()
N = 0
Count = 0
for step in Steps:
	if N == 0:
		Start = step
	if step == 'U':
		N += 1
	else:
		N += -1
	if N == 0 and Start == 'D':
		Count += 1
print(Count)


import sys
n = int(input().strip())
s = input('')
l = []
l.append(0)
s_l = 0
l1 = []
temp = []
c_v = 0
for i in s:
	if i == 'U':
		s_l += 1
	else:
		s_l -= 1
	l.append(s_l)
for i in range(0, len(l)):
	if l[i] == 0:
		l1.append(i)
for i in range(0, len(l1) - 1):
	temp = l[l1[i]:l1[i + 1]]
	if all((i <= 0 for i in temp)):
		c_v += 1
print(c_v)


first = input()
second = input()
count_1 = 0
count_2 = 0
below_sea = False
for element in range(0, len(second)):
	if second[element] == 'U':
		count_1 += 1
	if second[element] == 'D':
		count_1 -= 1
	if count_1 < 0:
		if not below_sea:
			count_2 += 1
			below_sea = True
	else:
		below_sea = False
print(count_2)


c = 0
z = 0
a = int(input())
b = input()
for i in b:
	if i == 'U':
		c += 1
	else:
		c -= 1
	if c == 0 and i == 'U':
		z += 1
print(z)


steps = int(input().strip())
s = input().strip()
res = 0
stepsum = 0
beginwith = ''
for step in s:
	if step == 'U':
		stepsum += 1
		if not len(beginwith):
			beginwith = 'U'
	else:
		stepsum -= 1
		if not len(beginwith):
			beginwith = 'D'
	if stepsum == 0:
		if beginwith == 'D':
			res += 1
		stepsum = 0
		beginwith = ''
print(res)


input()
path = input().strip()
from itertools import groupby
sub_path = groupby(path)
h = 0
res = 0
for (key, value) in sub_path:
	h_old = h
	if key == 'U':
		h += len(list(value))
	else:
		h -= len(list(value))
	if h_old >= 0 and h < 0:
		res += 1
print(res)


N = int(input())
s = input()
h = 0
valleys = 0
prev_h = 0
for c in s:
	if c == 'D':
		h -= 1
	else:
		h += 1
	if prev_h == 0 and h == -1:
		valleys += 1
	prev_h = h
print(valleys)


n = int(input())
steps = input()
valleys = 0
c = 0
for s in steps:
	o = c
	if s == 'U':
		c += 1
	elif s == 'D':
		c -= 1
	if o >= 0 and c < 0:
		valleys += 1
print(valleys)


n = input()
steps = list(input())
sea_level = 0
valleys = 0
for step in steps:
	if step == 'D' and sea_level == 0:
		valleys = valleys + 1
	if step == 'U':
		sea_level = sea_level + 1
	if step == 'D':
		sea_level = sea_level - 1
print(valleys)


n = input()
inp = input()
alg = 0
l = []
for i in inp:
	if i == 'U':
		alg += 1
		l.append(alg)
	else:
		alg -= 1
		l.append(alg)
count = 0
for j in range(len(l) - 1):
	if l[j] == 0 and l[j + 1] == -1 or (j == 0 and l[j] == -1):
		count += 1
print(count)


input()
route = input().strip()
height = 0
valleys = 0
for step in route:
	if step == 'U':
		height += 1
	elif step == 'D':
		if height == 0:
			valleys += 1
		height -= 1
print(valleys)


n = int(input().strip())
step_string = input().strip()
step_array = []
for i in range(len(step_string)):
	step_array.append(step_string[i])
level = 0
valley = 0
mountain = 0
for i in range(len(step_array)):
	prev_level = level
	if step_array[i] == 'U':
		level += 1
	elif step_array[i] == 'D':
		level += -1
	if level == 0:
		if prev_level == -1:
			valley += 1
		elif prev_level == 1:
			mountain += 1
print(valley)


a = int(input())
b = input()
c = 0
aa = []
i = 0
aa.append(0)
while i < a:
	if b[i] == 'U':
		c = c + 1
		aa.append(c)
		i += 1
	else:
		c -= 1
		aa.append(c)
		i += 1
r = 0
for i in range(len(aa) - 1):
	if aa[i] == 0 and aa[i + 1] < 0:
		r += 1
print(r)


n = int(input())
seq = input()
tracker = 0
valleys = 0
for c in seq:
	if c == 'U':
		tracker += 1
	elif c == 'D':
		tracker -= 1
	if tracker == 0 and c == 'U':
		valleys += 1
print(valleys)


N = int(input().strip())
steps = input().strip()
h = 0
valley = 0
inside = False
for s in steps:
	if s == 'U':
		h += 1
	else:
		h -= 1
		if h < 0:
			inside = True
	if inside:
		if h >= 0:
			inside = False
			valley += 1
print(valley)


(a, c) = (0, 0)
n = input()
s = list(input())
for x in s:
	if x == 'U':
		a = a + 1
		if a == 0:
			c = c + 1
	elif x == 'D':
		a = a - 1
print(c)


input()
coal = list(str(input()))
h = 0
v = 0
for i in coal:
	if h == -1 and i == 'U':
		h += 1
		v += 1
	elif i == 'U':
		h += 1
	elif i == 'D':
		h -= 1
print(v)


(v, l) = (0, [])
res = 0
n = int(input())
s = input()
l = list(s)
for i in range(n):
	c = 0
	c = c + v
	if s[i] == 'U':
		v = v + 1
	else:
		v = v - 1
	if c < 0 and v == 0:
		res = res + 1
print(res)


n = int(input())
last = ''
valley = False
r = 0
level = 0
for a in str(input()):
	if a == 'D':
		level -= 1
	if a == 'U':
		level += 1
		if level == 0:
			r += 1
	last = a
print(r)


n = int(input())
s = str(input())
val = False
valcount = 0
lev = 0
for i in s:
	if i == 'U':
		lev += 1
	if i == 'D':
		lev -= 1
	if lev < 0:
		val = True
	if lev == 0 and val:
		valcount += 1
		val = False
print(valcount)


n = input()
HikingTracker = list(input())
status_ = 'sea'
level_ = 0
valley_counter = 0
for i in HikingTracker:
	if i == 'U':
		level_ += 1
		if level_ == 1:
			status_ = 'mountain'
	else:
		level_ -= 1
		if level_ == -1:
			status_ = 'valley'
	if status_ == 'valley' and level_ == 0:
		valley_counter += 1
print(valley_counter)


n = int(input())
seq = input()
level = 0
numValleys = 0
for s in seq:
	preLevel = level
	if s == 'U':
		level += 1
	elif s == 'D':
		level -= 1
	if preLevel < 0 and level == 0:
		numValleys += 1
print(numValleys)


n = int(input())
str1 = input()
level = 0
valley = 0
for i in str1:
	if i == 'U':
		level += 1
		if level == 0:
			valley += 1
	elif i == 'D':
		level -= 1
print(valley)


n = int(input().strip())
steps = [-1 if s == 'D' else 1 for s in input().strip()]
valleys = 0
current = 0
is_valley = False
for step in steps:
	ns = current + step
	if ns == -1:
		is_valley = True
	if is_valley and ns == 0:
		valleys += 1
		is_valley = False
	current = ns
print(valleys)


steps = input()
orientation = input()
level = 0
counter = 0
for step in range(int(steps)):
	if orientation[step] == 'U':
		level += 1
		if level == 0:
			counter += 1
	if orientation[step] == 'D':
		level -= 1
print(counter)


import sys
n = int(input().strip())
path = list(input().strip())
prevpos = 0
pos = 0
numvalleys = 0
for step in path:
	if pos == -1 and step == 'U':
		numvalleys += 1
	prevpos = pos
	if step == 'U':
		pos += 1
	else:
		pos -= 1
print(numvalleys)


k = int(input())
l = input()
valey = 0
sea = 0
for i in l:
	if i == 'U':
		sea = sea + 1
		if sea == 0:
			valey += 1
	elif i == 'D':
		sea = sea - 1
	else:
		pass
print(valey)


n = input()
steps_arr = input()
steps = 0
valley = 0
for i in range(int(n)):
	level = 0
	level = level + steps
	if steps_arr[i] == 'D':
		steps = steps - 1
	else:
		steps = steps + 1
	if steps == 0 and level < 0:
		valley += 1
print(valley)