Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations:   U — move from $(x, y)$ to $(x, y + 1)$;  D — move from $(x, y)$ to $(x, y - 1)$;  L — move from $(x, y)$ to $(x - 1, y)$;  R — move from $(x, y)$ to $(x + 1, y)$. 

Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.

Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$. 

If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.

Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.


-----Input-----

The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ — the number of operations.

The second line contains the sequence of operations — a string of $n$ characters. Each character is either U, D, L or R.

The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ — the coordinates of the cell where the robot should end its path.


-----Output-----

Print one integer — the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.


-----Examples-----
Input
5
RURUU
-2 3

Output
3

Input
4
RULR
1 1

Output
0

Input
3
UUU
100 100

Output
-1



-----Note-----

In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.

In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.

In the third example the robot can't end his path in the cell $(x, y)$.

Difficulty: MEDIUM_HARD
Tags: Sorting

Amortized analysis
Skill types: Sorting

Amortized analysis

Starter code:

Solutions:
def doable(n, x, y, m, prefixLR, prefixUD):
	for i in range(n - m + 1):
		j = i + m - 1
		dx = prefixLR[i] + prefixLR[-1] - prefixLR[j + 1]
		dy = prefixUD[i] + prefixUD[-1] - prefixUD[j + 1]
		if abs(x - dx) + abs(y - dy) <= m:
			return True
	return False

def main():
	n = int(input())
	s = list(input().strip())
	(x, y) = map(int, input().strip().split())
	k = abs(x) + abs(y)
	if k > n or k % 2 != n % 2:
		print(-1)
		return
	prefixLR = [0] * (n + 1)
	prefixUD = [0] * (n + 1)
	for i in range(n):
		prefixLR[i + 1] = prefixLR[i]
		prefixUD[i + 1] = prefixUD[i]
		if s[i] == 'L':
			prefixLR[i + 1] -= 1
		elif s[i] == 'R':
			prefixLR[i + 1] += 1
		elif s[i] == 'D':
			prefixUD[i + 1] -= 1
		else:
			prefixUD[i + 1] += 1
	left = 0
	right = n
	while left < right:
		mid = left + (right - left) // 2
		if doable(n, x, y, mid, prefixLR, prefixUD):
			right = mid
		else:
			left = mid + 1
	print(left)
main()


import collections, atexit, math, sys, bisect
sys.setrecursionlimit(1000000)

def getIntList():
	return list(map(int, input().split()))
try:
	import numpy

	def dprint(*args, **kwargs):
		print(*args, **kwargs, file=sys.stderr)
	dprint('debug mode')
except Exception:

	def dprint(*args, **kwargs):
		pass
inId = 0
outId = 0
if inId > 0:
	dprint('use input', inId)
	sys.stdin = open('input' + str(inId) + '.txt', 'r')
if outId > 0:
	dprint('use output', outId)
	sys.stdout = open('stdout' + str(outId) + '.txt', 'w')
	atexit.register(lambda : sys.stdout.close())
(N,) = getIntList()
S = input()
(X, Y) = getIntList()
dd = ((0, 1), (0, -1), (-1, 0), (1, 0))
pp = 'UDLR'
zz = {}
for i in range(4):
	zz[pp[i]] = dd[i]
if abs(X) + abs(Y) > N:
	print(-1)
	return
if abs(X + Y - N) % 2 == 1:
	print(-1)
	return
fromLeft = [None for i in range(N)]
fromRight = fromLeft.copy()
x0 = 0
y0 = 0
for i in range(N):
	x = S[i]
	fromLeft[i] = (x0, y0)
	g = zz[x]
	x0 += g[0]
	y0 += g[1]
if x0 == X and y0 == Y:
	print(0)
	return
x0 = 0
y0 = 0
for i in range(N - 1, -1, -1):
	x = S[i]
	fromRight[i] = (x0, y0)
	g = zz[x]
	x0 += g[0]
	y0 += g[1]
up = N
down = 0
dprint(fromLeft)
dprint(fromRight)
while down + 1 < up:
	mid = (up + down) // 2
	dprint('mid', mid)
	ok = False
	for i in range(N - mid + 1):
		tx = fromLeft[i][0] + fromRight[i + mid - 1][0]
		ty = fromLeft[i][1] + fromRight[i + mid - 1][1]
		gg = abs(X - tx) + abs(Y - ty)
		if gg <= mid:
			ok = True
			break
	if ok:
		up = mid
	else:
		down = mid
print(up)


n = int(input())
dirs = input()
goal = list(map(int, input().split(' ')))

def can(start, end, steps):
	dist = abs(start[0] - end[0]) + abs(start[1] - end[1])
	return dist <= steps and (steps - dist) % 2 == 0
if not can((0, 0), goal, n):
	print(-1)
	return
diffs = {'U': (0, 1), 'D': (0, -1), 'L': (-1, 0), 'R': (1, 0)}
pos = [(0, 0)] + [None] * n
for (i, dir) in enumerate(dirs):
	old_pos = pos[i]
	diff = diffs[dir]
	pos[i + 1] = (old_pos[0] + diff[0], old_pos[1] + diff[1])
final_pos = pos[n]
best = (abs(final_pos[0] - goal[0]) + abs(final_pos[1] - goal[1])) // 2
if best == 0:
	print(0)
	return
start = 0
end = best
current_best = float('inf')
while end <= n:
	cur_pos = (pos[start][0] + final_pos[0] - pos[end][0], pos[start][1] + final_pos[1] - pos[end][1])
	if can(cur_pos, goal, end - start):
		current_best = min(current_best, end - start)
		if current_best == best:
			break
		start += 1
	else:
		end += 1
print(current_best)


import math as ma
import sys
from decimal import Decimal as dec
from itertools import permutations

def li():
	return list(map(int, sys.stdin.readline().split()))

def num():
	return map(int, sys.stdin.readline().split())

def nu():
	return int(sys.stdin.readline())

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
n = nu()
s = input()
(x, y) = num()
uu = [0] * n
rr = [0] * n
pu = []
pr = []
for i in range(n):
	if s[i] == 'U':
		uu[i] = 1
	if s[i] == 'D':
		uu[i] = -1
	if s[i] == 'R':
		rr[i] = 1
	if s[i] == 'L':
		rr[i] = -1
pu.append(uu[0])
pr.append(rr[0])
for i in range(1, n):
	pu.append(pu[i - 1] + uu[i])
for i in range(1, n):
	pr.append(pr[i - 1] + rr[i])
pu = [0] + pu
pr = [0] + pr
zu = pu[len(pu) - 1]
zr = pr[len(pr) - 1]
if abs(x - zr) + abs(y - zu) == 0:
	print(0)
	return
if (abs(x) + abs(y)) % 2 != n % 2 or abs(x) + abs(y) > n:
	print(-1)
	return
lo = 1
hi = n
while lo <= hi:
	mid = (lo + hi) // 2
	fl = False
	for i in range(0, n - mid + 1):
		lu = zu - pu[i + mid] + pu[i]
		lr = zr - pr[i + mid] + pr[i]
		if abs(x - lr) + abs(y - lu) <= mid:
			fl = True
			break
	if fl == True:
		hi = mid - 1
	else:
		lo = mid + 1
print(lo)


def go(pos, k):
	if 'U' == k:
		pos[1] += 1
	elif 'D' == k:
		pos[1] -= 1
	elif 'L' == k:
		pos[0] -= 1
	else:
		pos[0] += 1
	return pos

def relapos(a, b):
	c = [b[0] - a[0], b[1] - a[1]]
	return c

def stdis(a, b):
	return abs(b[1] - a[1]) + abs(b[0] - a[0])
n = int(input())
s = input()
(x, y) = [int(i) for i in input().split()]
poss = [[0, 0]]
pos = [0, 0]
for i in range(0, n):
	poss.append(tuple(go(pos, s[i])))
if abs(x) + abs(y) > n or abs(n - x - y) % 2 != 0:
	print(-1)
else:
	lpos = poss[-1]
	dd = stdis(lpos, [x, y])
	if dd == 0:
		print(0)
	else:
		q1 = (dd - 1) // 2
		q2 = n
		j = True
		lpos1 = [0, 0]
		while q2 - q1 != 1:
			q = (q2 + q1) // 2
			for k1 in range(n - q + 1):
				r = relapos(poss[k1], poss[k1 + q])
				lpos1[0] = lpos[0] - r[0]
				lpos1[1] = lpos[1] - r[1]
				if stdis(lpos1, [x, y]) <= q:
					q2 = q
					break
			else:
				q1 = q
		print(q2)


n = int(input())
a = input()
(x, y) = map(int, input().split())
locs = [(0, 0)]
for i in range(n):
	if a[i] == 'U':
		locs.append((locs[-1][0], locs[-1][1] + 1))
	elif a[i] == 'D':
		locs.append((locs[-1][0], locs[-1][1] - 1))
	elif a[i] == 'R':
		locs.append((locs[-1][0] + 1, locs[-1][1]))
	else:
		locs.append((locs[-1][0] - 1, locs[-1][1]))
if abs(x) + abs(y) > n or (x + y - n) % 2 == 1:
	print(-1)
elif locs[-1] == (x, y):
	print(0)
else:
	a = 0
	b = 0
	best = n
	end = locs[-1]
	while True:
		(c, d) = (locs[a][0] + end[0] - locs[b][0], locs[a][1] + end[1] - locs[b][1])
		if abs(c - x) + abs(d - y) <= b - a:
			best = min(best, b - a)
			a += 1
		else:
			b += 1
			if b > n:
				print(best)
				break


def is_check(x):
	i = 1
	j = x
	(h, v) = (0, 0)
	while j <= n:
		h = sum_x[n] - sum_x[j] + (sum_x[i - 1] - sum_x[0])
		v = sum_y[n] - sum_y[j] + (sum_y[i - 1] - sum_y[0])
		i += 1
		j += 1
		if abs(x_f - h) + abs(y_f - v) <= x:
			return 1
	return 0
n = int(input())
st = input()
(x_f, y_f) = map(int, input().split())
sum_x = [0]
sum_y = [0]
(add_x, add_y) = (0, 0)
for i in range(n):
	if st[i] == 'R':
		add_x += 1
	elif st[i] == 'L':
		add_x -= 1
	elif st[i] == 'U':
		add_y += 1
	else:
		add_y -= 1
	sum_x.append(add_x)
	sum_y.append(add_y)
i = 0
j = n
while i < j:
	m = (i + j) // 2
	if is_check(m):
		j = m - 1
	else:
		i = m + 1
if abs(x_f) + abs(y_f) > n or (abs(x_f) + abs(y_f) + n) % 2 == 1:
	print(-1)
elif is_check(i):
	print(i)
else:
	print(i + 1)


import os, sys
from io import BytesIO, IOBase

def change(res, el, fac):
	if el == 'U':
		res[1] += fac
	elif el == 'D':
		res[1] -= fac
	elif el == 'R':
		res[0] += fac
	else:
		res[0] -= fac

def check(mid, s, res, x, y, n):
	for i in range(mid):
		change(res, s[i], -1)
	if abs(res[0] - x) + abs(res[1] - y) <= mid:
		return 1
	for i in range(mid, n):
		change(res, s[i], -1)
		change(res, s[i - mid], 1)
		if abs(res[0] - x) + abs(res[1] - y) <= mid:
			return 1
	return 0

def main():
	n = int(input())
	s = list(input().strip())
	(x, y) = map(int, input().split())
	if x + y & 1 != n & 1:
		return -1
	res = [0, 0]
	for i in range(n):
		change(res, s[i], 1)
	if res == [x, y]:
		return 0
	(hi, lo, ans) = (n, 1, -1)
	while hi >= lo:
		mid = (hi + lo) // 2
		if check(mid, s, res[:], x, y, n):
			ans = mid
			hi = mid - 1
		else:
			lo = mid + 1
	return ans
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
print(main())


import sys
from array import array
import typing as Tp

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def main():
	n = int(input())
	s = input().rstrip()
	(tx, ty) = map(int, input().split())
	(acc_x, acc_y) = ([0] * (n + 1), [0] * (n + 1))
	for (i, c) in enumerate(s, start=1):
		acc_x[i] = acc_x[i - 1] + (1 if c == 'R' else -1 if c == 'L' else 0)
		acc_y[i] = acc_y[i - 1] + (1 if c == 'U' else -1 if c == 'D' else 0)
	if acc_x[-1] == tx and acc_y[-1] == ty:
		print(0)
		exit()
	if abs(acc_x[-1] - tx) + abs(acc_y[-1] - ty) & 1:
		print(-1)
		exit()
	ans = inf = 10 ** 9
	for left in range(n):
		(ok, ng) = (n + 1, left - 1)
		while abs(ok - ng) > 1:
			mid = ok + ng >> 1
			dist = abs(acc_x[-1] - acc_x[mid] + acc_x[left] - tx) + abs(acc_y[-1] - acc_y[mid] + acc_y[left] - ty)
			if dist <= mid - left:
				ok = mid
			else:
				ng = mid
		if ok < n + 1:
			ans = min(ans, ok - left)
	print(ans if ans < inf else -1)
main()


import math, sys, bisect, heapq
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
int1 = lambda x: int(x) - 1
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))
ilelec = lambda : map(int1, input().split())
alelec = lambda : list(map(int1, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])
n = int(input())
s = input()
(x, y) = ilele()
t = abs(x) + abs(y)
if t > n or t % 2 != n % 2:
	print(-1)
	exit(0)
dpLR = [0] * (n + 1)
dpUD = [0] * (n + 1)
for i in range(n):
	dpLR[i + 1] = dpLR[i]
	dpUD[i + 1] = dpUD[i]
	if s[i] == 'L':
		dpLR[i + 1] -= 1
	elif s[i] == 'R':
		dpLR[i + 1] += 1
	elif s[i] == 'U':
		dpUD[i + 1] += 1
	else:
		dpUD[i + 1] -= 1

def fun(m):
	for i in range(n - m + 1):
		j = i + m
		xdif = dpLR[i] + dpLR[-1] - dpLR[j]
		ydif = dpUD[i] + dpUD[-1] - dpUD[j]
		if abs(x - xdif) + abs(y - ydif) <= m:
			return True
	return False
l = 0
r = n
while l < r:
	m = (l + r) // 2
	if fun(m):
		r = m
	else:
		l = m + 1
print(l)


n = int(input())
s = list(input())
(a, b) = list(map(int, input().split()))
L = s.count('L')
U = s.count('U')
R = s.count('R')
D = s.count('D')
x = 0
y = 0
xmin = 0
ymin = 0
minn = 2 * n
while x + y < 2 * n:
	if abs(a - (R - L)) + abs(b - (U - D)) > y - x and y != n:
		i = s[y]
		if i == 'L':
			L -= 1
		elif i == 'R':
			R -= 1
		elif i == 'D':
			D -= 1
		elif i == 'U':
			U -= 1
		y += 1
	elif abs(a - (R - L)) + abs(b - (U - D)) <= y - x or y == n:
		if y - x < minn and abs(a - (R - L)) + abs(b - (U - D)) <= y - x:
			minn = y - x
		i = s[x]
		if i == 'L':
			L += 1
		elif i == 'R':
			R += 1
		elif i == 'D':
			D += 1
		elif i == 'U':
			U += 1
		x += 1
if abs(a) + abs(b) > len(s):
	print(-1)
elif (len(s) - (abs(a) + abs(b))) % 2 != 0:
	print(-1)
else:
	print(minn)


import sys
fin = sys.stdin.readline
n = int(fin())
commands = list(fin())[:-1]
(x, y) = [int(elem) for elem in fin().split(' ')]
move_map = {'L': (-1, 0), 'R': (1, 0), 'U': (0, 1), 'D': (0, -1)}
if (x + y) % 2 != n % 2:
	print(-1)
	exit(0)
cuml_coord = [None] * n
(cuml_x, cuml_y) = (0, 0)
for (i, command) in enumerate(commands):
	(dx, dy) = move_map[command]
	cuml_x += dx
	cuml_y += dy
	cuml_coord[i] = (cuml_x, cuml_y)
(left, right) = (0, 0)
min_len = 2 ** 32 - 1
(org_x, org_y) = cuml_coord[-1]
if org_x == x and org_y == y:
	min_len = 0
while right <= n - 1:
	if left == 0:
		left_cuml = (0, 0)
	else:
		left_cuml = cuml_coord[left - 1]
	right_cuml = cuml_coord[right]
	(movable_x, movable_y) = (right_cuml[0] - left_cuml[0], right_cuml[1] - left_cuml[1])
	(fixed_x, fixed_y) = (org_x - movable_x, org_y - movable_y)
	sub_length = right - left + 1
	if abs(x - fixed_x) + abs(y - fixed_y) <= sub_length and (abs(x - fixed_x) + abs(y - fixed_y)) % 2 == sub_length % 2:
		min_len = min(min_len, sub_length)
		if left != right:
			left += 1
		else:
			right += 1
	else:
		right += 1
if min_len == 2 ** 32 - 1:
	print(-1)
else:
	print(min_len)


def valid(step, tx, ty, nx, ny, s, d):
	fx = 0
	fy = 0
	for i in range(len(s)):
		c = s[i]
		fx += d[c][0]
		fy += d[c][1]
		if i >= step:
			c = s[i - step]
			fx -= d[c][0]
			fy -= d[c][1]
		if i >= step - 1:
			diff = abs(nx - fx - tx) + abs(ny - fy - ty)
			if diff <= step and (step - diff) % 2 == 0:
				return True
	return False

def main():
	d = {'U': (0, 1), 'D': (0, -1), 'L': (-1, 0), 'R': (1, 0)}
	nx = 0
	ny = 0
	n = int(input())
	s = input()
	(tx, ty) = map(lambda x: int(x), input().split(' '))
	diff = abs(tx) + abs(ty)
	if diff > len(s) or (diff - len(s)) % 2 == 1:
		print(-1)
		return
	for c in s:
		nx += d[c][0]
		ny += d[c][1]
	if (nx, ny) == (tx, ty):
		print(0)
		return
	l = 0
	r = len(s)
	ans = r
	while l < r:
		m = (l + r) // 2
		if valid(m, tx, ty, nx, ny, s, d):
			ans = m
			r = m
		else:
			l = m + 1
	print(ans)
main()


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
dx = [0 for i in range(n + 1)]
dy = [0 for i in range(n + 1)]
for (i, ch) in enumerate(input()):
	dx[i + 1] = dx[i]
	dy[i + 1] = dy[i]
	if ch == 'U':
		dy[i + 1] += 1
	elif ch == 'D':
		dy[i + 1] -= 1
	elif ch == 'R':
		dx[i + 1] += 1
	else:
		assert ch == 'L'
		dx[i + 1] -= 1
(x, y) = map(int, input().split())

def canChange(left, right):
	dx1 = dx[left]
	dy1 = dy[left]
	dx3 = dx[-1] - dx[right]
	dy3 = dy[-1] - dy[right]
	dx2 = x - dx1 - dx3
	dy2 = y - dy1 - dy3
	length = right - left
	free = length - (abs(dx2) + abs(dy2))
	return free >= 0 and free % 2 == 0
result = n + 1
ptr = n + 1
for i in reversed(range(n)):
	while ptr - 1 >= i and canChange(i, ptr - 1):
		ptr -= 1
		result = min(result, ptr - i)
if ptr == n + 1:
	print(-1)
else:
	print(result)


def check(length: int):
	global n, x, y, cur, hor, ver
	hor = 0
	ver = 0
	for i in range(length, n):
		upd(i)
	for i in range(n - length + 1):
		if abs(x - hor) + abs(y - ver) <= length:
			return True
		if i + length < n:
			upd(i)
			minus_upd(i + length)
	return False

def upd(pos: int):
	global s, ver, hor
	if s[pos] == 'U':
		ver += 1
	elif s[pos] == 'D':
		ver -= 1
	elif s[pos] == 'R':
		hor += 1
	else:
		hor -= 1

def minus_upd(pos: int):
	global s, ver, hor
	if s[pos] == 'U':
		ver -= 1
	elif s[pos] == 'D':
		ver += 1
	elif s[pos] == 'R':
		hor -= 1
	else:
		hor += 1
n = int(input())
s = list(input())
(x, y) = map(int, input().split())
if (x + y) % 2 != n % 2 or abs(x) + abs(y) > n:
	print(-1)
	exit()
ver = 0
hor = 0
cur = 0
for i in range(n):
	upd(i)
left = -1
r = n
while r - left > 1:
	length = (r + left) // 2
	if check(length):
		r = length
	else:
		left = length
print(r)


import functools
n = int(input())
dct = {'U': (0, 1), 'D': (0, -1), 'L': (-1, 0), 'R': (1, 0)}
moves_list = list([dct[x] for x in [*input()]])
(dest_x, dest_y) = list(map(int, input().split()))

def add_vectors(a, b):
	return (a[0] + b[0], a[1] + b[1])

def subtract_vectors(a, b):
	return (a[0] - b[0], a[1] - b[1])
total = functools.reduce(add_vectors, moves_list)

def czy(dl) -> bool:
	sumo = total
	for v in moves_list[:dl]:
		sumo = subtract_vectors(sumo, v)
	for i in range(1, n - dl + 2):
		(dx, dy) = list(map(abs, subtract_vectors((dest_x, dest_y), sumo)))
		if dx + dy <= dl and (dx + dy) % 2 == dl % 2:
			return True
		if i + dl - 1 >= n:
			break
		sumo = subtract_vectors(sumo, moves_list[i + dl - 1])
		sumo = add_vectors(sumo, moves_list[i - 1])
	return False
pocz = 0
kon = n
while pocz < kon:
	sr = (pocz + kon) // 2
	if czy(sr):
		kon = sr
	else:
		pocz = sr + 1
if czy(pocz):
	print(pocz)
else:
	print(-1)


import itertools

def solve(L, n, px, py, x, y):
	for i in range(n):
		j = i + L
		if j > n:
			break
		dx = px[i] + px[-1] - px[j] - x
		dy = py[i] + py[-1] - py[j] - y
		if abs(dx) + abs(dy) <= L:
			return True
	return False

def main(args):
	n = int(input())
	d = input()
	(x, y) = list(map(int, input().split()))
	py = [0] + [1 if c == 'U' else -1 if c == 'D' else 0 for c in d]
	px = [0] + [1 if c == 'R' else -1 if c == 'L' else 0 for c in d]
	if abs(x + y) % 2 != n % 2:
		print(-1)
		return 0
	py = list(itertools.accumulate(py))
	px = list(itertools.accumulate(px))
	if px[-1] == x and py[-1] == y:
		print(0)
		return 0
	if abs(x) + abs(y) > n:
		print(-1)
		return 0
	left = 0
	right = n
	while left + 1 < right:
		mid = (left + right) // 2
		if solve(mid, n, px, py, x, y):
			(left, right) = (left, mid)
		else:
			(left, right) = (mid, right)
	print(right)
	return 0

def __starting_point():
	import sys
	return main(sys.argv)
__starting_point()


import sys
input = sys.stdin.readline
n = int(input())
s = [i for i in input() if i != '\n']
(x, y) = map(int, input().split())
if abs(x) + abs(y) > n or (abs(x) + abs(y)) % 2 != n % 2:
	print(-1)
else:
	(dx, dy) = ([], [])
	for i in range(n):
		if s[i] == 'R':
			dx.append(1)
			dy.append(0)
		elif s[i] == 'L':
			dx.append(-1)
			dy.append(0)
		elif s[i] == 'U':
			dy.append(1)
			dx.append(0)
		else:
			dy.append(-1)
			dx.append(0)
	(xx, yy) = (sum(dx), sum(dy))
	if xx == x and yy == y:
		print(0)
	else:
		(l, res) = (n - 1, n)
		for r in range(n - 1, -1, -1):
			xx -= dx[r]
			yy -= dy[r]
			while l - r + 1 >= abs(x - xx) + abs(y - yy):
				xx += dx[l]
				yy += dy[l]
				l -= 1
			if l < n - 1:
				res = min(res, l - r + 2)
		print(res)


N = int(input())
ops = [x for x in input()]
(X, Y) = map(int, input().split())
dd = abs(X) + abs(Y)
lops = len(ops)
if dd > lops or (lops - dd) % 2 != 0:
	print(-1)
	exit(0)
[ll, lr, lu, ld, rl, rr, ru, rd] = [[0 for _ in range(lops + 2)] for _ in range(8)]
(l, r, u, d) = (0, 0, 0, 0)
for i in range(lops):
	op = ops[i]
	if op == 'L':
		l += 1
	elif op == 'R':
		r += 1
	elif op == 'U':
		u += 1
	else:
		d += 1
	ll[i + 1] = l
	lr[i + 1] = r
	ld[i + 1] = d
	lu[i + 1] = u
(l, r, u, d) = (0, 0, 0, 0)
for i in range(lops - 1, -1, -1):
	op = ops[i]
	if op == 'L':
		l += 1
	elif op == 'R':
		r += 1
	elif op == 'U':
		u += 1
	else:
		d += 1
	rl[i] = l
	rr[i] = r
	rd[i] = d
	ru[i] = u

def check(lsub):
	for i in range(lops - lsub + 1):
		j = i + lsub - 1
		(l0, r0, u0, d0) = (ll[i], lr[i], lu[i], ld[i])
		(l1, r1, u1, d1) = (rl[j + 1], rr[j + 1], ru[j + 1], rd[j + 1])
		x = r0 + r1 - (l0 + l1)
		y = u0 + u1 - (d0 + d1)
		dx = abs(X - x)
		dy = abs(Y - y)
		if dx + dy <= lsub and (lsub - dx - dy) % 2 == 0:
			return True
	return False
(sl, sr) = (0, lops + 1)
while sl < sr:
	m = (sl + sr) // 2
	if check(m):
		sr = m
	else:
		sl = m + 1
sl = -1 if sl > lops else sl
print(sl)


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

def compute_delta(s, head_idx, tail_idx):
	x = y = 0
	for i in range(head_idx, tail_idx):
		(x, y) = (x + d[s[i]][0], y + d[s[i]][1])
	return [x, y]
n = int(input())
s = input()
dsc = list(map(int, input().split()))
total = compute_delta(s, 0, n)
(l, r) = (0, n)
current_sol = -1
while l <= r:
	local_len = (r + l) // 2
	initial_diff = compute_delta(s, 0, local_len)
	local = [total[0] - initial_diff[0], total[1] - initial_diff[1]]
	is_possible = False
	diff = abs(dsc[0] - local[0]) + abs(dsc[1] - local[1])
	if diff <= local_len and (diff + local_len) % 2 == 0:
		is_possible = True
	for i in range(local_len, n):
		if is_possible:
			break
		(d_old, d_new) = (d[s[i]], d[s[i - local_len]])
		local = [local[0] - d_old[0] + d_new[0], local[1] - d_old[1] + d_new[1]]
		diff = abs(dsc[0] - local[0]) + abs(dsc[1] - local[1])
		if diff <= local_len and (diff + local_len) % 2 == 0:
			is_possible = True
	if is_possible:
		current_sol = local_len
		r = local_len - 1
	else:
		l = local_len + 1
print(current_sol)


def check(x, y, fx, fy, num_moves):
	if abs(fx - x) + abs(fy - y) - num_moves <= 0 and (abs(fx - x) + abs(fy - y) - num_moves) % 2 == 0:
		return True
	return False
N = int(input())
mm = {'U': 0, 'D': 1, 'L': 2, 'R': 3}
dpmat = [[0] for i in range(4)]
ops = str(input())
for op in ops:
	dpmat[0].append(dpmat[0][-1])
	dpmat[1].append(dpmat[1][-1])
	dpmat[2].append(dpmat[2][-1])
	dpmat[3].append(dpmat[3][-1])
	dpmat[mm[op]][-1] = dpmat[mm[op]][-1] + 1
fpos = list(map(int, input().split()))
if N < fpos[0] + fpos[1]:
	print('-1')
else:
	(x, y) = (0, 0)
	ans = 100000000000.0
	while y <= N and x <= y:
		if y == 0 and x == 0:
			num_moves = 0
			xr = dpmat[3][N]
			xl = dpmat[2][N]
			yu = dpmat[0][N]
			yd = dpmat[1][N]
		else:
			num_moves = y - x + 1
			xr = dpmat[3][x - 1] + dpmat[3][N] - dpmat[3][y]
			xl = dpmat[2][x - 1] + dpmat[2][N] - dpmat[2][y]
			yu = dpmat[0][x - 1] + dpmat[0][N] - dpmat[0][y]
			yd = dpmat[1][x - 1] + dpmat[1][N] - dpmat[1][y]
		if check(xr - xl, yu - yd, fpos[0], fpos[1], num_moves) == True:
			x += 1
			ans = min(ans, num_moves)
		else:
			if x == 0:
				x += 1
			y += 1
	if ans == 100000000000.0:
		print('-1')
	else:
		print(max(0, ans))


n = int(input().strip())
s = str(input().strip())
(x, y) = list(map(int, input().strip().split()))
sumx = []
sumy = []
sx = 0
sy = 0
sumx.append(sx)
sumy.append(sy)
for i in s:
	if i == 'U':
		sy += 1
	elif i == 'D':
		sy -= 1
	elif i == 'R':
		sx += 1
	elif i == 'L':
		sx -= 1
	sumx.append(sx)
	sumy.append(sy)

def check(mid):
	i = 0
	while i + mid <= n:
		dx = sumx[i + mid] - sumx[i]
		dy = sumy[i + mid] - sumy[i]
		cx = sx - dx
		cy = sy - dy
		gdx = x - cx
		gdy = y - cy
		t = abs(gdx) + abs(gdy)
		if t % 2 == mid % 2 and t <= mid:
			return True
		i += 1
	return False
hi = n
lo = 0
mid = (hi + lo) // 2
while hi - lo > 1:
	mid = (hi + lo) // 2
	if check(mid):
		hi = mid
	else:
		lo = mid
if check(lo):
	print(lo)
elif check(mid):
	print(mid)
elif check(hi):
	print(hi)
else:
	print(-1)


n = int(input())
s = list(input())
(x, y) = map(int, input().split())
it = [0, 0, 0, 0]
for i in range(len(s)):
	if s[i] == 'U':
		s[i] = 0
	elif s[i] == 'R':
		s[i] = 1
	elif s[i] == 'D':
		s[i] = 2
	else:
		s[i] = 3
	it[s[i]] += 1

def distance(x, y, xx, yy):
	return abs(x - xx) + abs(y - yy)

def yes(steps, ost, x, y):
	xx = steps[1] - steps[3]
	yy = steps[0] - steps[2]
	return distance(x, y, xx, yy) <= ost
ans = 0
if distance(x, y, 0, 0) > n or (x + y + n) % 2 == 1:
	print(-1)
elif yes(it, 0, x, y):
	print(0)
else:
	i = -1
	cur_ans = 0
	max_ans = 0
	steps = [0, 0, 0, 0]
	while yes(steps, n - cur_ans, x, y):
		i += 1
		steps[s[i]] += 1
		cur_ans += 1
	steps[s[i]] -= 1
	i -= 1
	cur_ans -= 1
	max_ans = cur_ans
	j = n
	ok = True
	while j > 0 and ok:
		j = j - 1
		steps[s[j]] += 1
		cur_ans += 1
		while i >= 0 and (not yes(steps, n - cur_ans, x, y)):
			steps[s[i]] -= 1
			i -= 1
			cur_ans -= 1
		if yes(steps, n - cur_ans, x, y) and cur_ans > max_ans:
			max_ans = cur_ans
		ok = i >= 0 or yes(steps, n - cur_ans, x, y)
	print(n - max_ans)


n = int(input())
dx = [0 for i in range(n + 1)]
dy = [0 for i in range(n + 1)]
for (i, ch) in enumerate(input()):
	dx[i + 1] = dx[i]
	dy[i + 1] = dy[i]
	if ch == 'U':
		dy[i + 1] += 1
	elif ch == 'D':
		dy[i + 1] -= 1
	elif ch == 'R':
		dx[i + 1] += 1
	else:
		assert ch == 'L'
		dx[i + 1] -= 1
(x, y) = map(int, input().split())

def canChange(left, right):
	dx1 = dx[left]
	dy1 = dy[left]
	dx3 = dx[-1] - dx[right]
	dy3 = dy[-1] - dy[right]
	dx2 = x - dx1 - dx3
	dy2 = y - dy1 - dy3
	length = right - left
	free = length - (abs(dx2) + abs(dy2))
	return free >= 0 and free % 2 == 0
result = n + 1
ptr = n + 1
for i in reversed(range(n)):
	while ptr - 1 >= i and canChange(i, ptr - 1):
		ptr -= 1
		result = min(result, ptr - i)
if ptr == n + 1:
	print(-1)
else:
	print(result)


from collections import Counter

def readint():
	return int(input())

def readline():
	return [int(c) for c in input().split()]

def update(pos, mv, d):
	if mv == 'U':
		pos[1] += d
	elif mv == 'D':
		pos[1] -= d
	elif mv == 'L':
		pos[0] -= d
	elif mv == 'R':
		pos[0] += d

def can(u, v, length):
	d = abs(u[0] - v[0]) + abs(u[1] - v[1])
	if d % 2 != length % 2:
		return False
	return length >= d

def ok(length, n, x, y, s):
	pos = [0, 0]
	for i in range(length, n):
		update(pos, s[i], 1)
	(l, r) = (0, length)
	while True:
		if can(pos, [x, y], length):
			return True
		if r == n:
			break
		update(pos, s[l], 1)
		l += 1
		update(pos, s[r], -1)
		r += 1
	return False

def main():
	n = readint()
	s = input()
	(x, y) = readline()
	if not ok(n, n, x, y, s):
		print(-1)
		return
	(l, r) = (-1, n)
	while r - l > 1:
		mid = (l + r) // 2
		if ok(mid, n, x, y, s):
			r = mid
		else:
			l = mid
	print(r)
main()


import os
import sys
from math import *
from collections import *
from heapq import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 998244353
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]

def forward(x, y, ch):
	if ch == 'R':
		x += 1
	elif ch == 'L':
		x -= 1
	elif ch == 'U':
		y += 1
	elif ch == 'D':
		y -= 1
	return (x, y)

def backward(x, y, ch):
	if ch == 'R':
		x -= 1
	elif ch == 'L':
		x += 1
	elif ch == 'U':
		y -= 1
	elif ch == 'D':
		y += 1
	return (x, y)

def ok(sz):
	x = 0
	y = 0
	l = 0
	if sz == 0:
		for i in range(n):
			(x, y) = forward(x, y, s[i])
		return (x, y) == (need_x, need_y)
	for i in range(sz - 1, n):
		(x, y) = forward(x, y, s[i])
	for r in range(sz - 1, n):
		(x, y) = backward(x, y, s[r])
		steps_required = abs(need_x - x) + abs(need_y - y)
		if steps_required <= sz and (sz - steps_required) % 2 == 0:
			return True
		(x, y) = forward(x, y, s[l])
		l += 1
	return False
for _ in range(1):
	n = Int()
	s = input()
	n = len(s)
	(need_x, need_y) = value()
	l = 0
	r = n
	ans = -1
	while l <= r:
		m = l + (r - l) // 2
		if ok(m):
			ans = m
			r = m - 1
		else:
			l = m + 1
	print(ans)


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

def compute_delta(s, head_idx, tail_idx):
	x = y = 0
	for i in range(head_idx, tail_idx):
		(x, y) = (x + d[s[i]][0], y + d[s[i]][1])
	return (x, y)

def compute_rest(s, n, head_idx, tail_idx):
	x = y = 0
	for i in range(0, head_idx):
		(x, y) = (x + d[s[i]][0], y + d[s[i]][1])
	for i in range(tail_idx, n):
		(x, y) = (x + d[s[i]][0], y + d[s[i]][1])
	return (x, y)
n = int(input())
s = input()
(x_d, y_d) = list(map(int, input().split()))
(x_t, y_t) = compute_delta(s, 0, n)
(l, r) = (0, n)
current_sol = -1
while l <= r:
	local_len = (r + l) // 2
	(x_l, y_l) = compute_rest(s, n, 0, local_len)
	is_possible = False
	diff = abs(x_d - x_l) + abs(y_d - y_l)
	if diff <= local_len and (diff + local_len) % 2 == 0:
		is_possible = True
	for i in range(local_len, n):
		if is_possible:
			break
		(d_old, d_new) = (d[s[i]], d[s[i - local_len]])
		(x_l, y_l) = (x_l - d_old[0] + d_new[0], y_l - d_old[1] + d_new[1])
		diff = abs(x_d - x_l) + abs(y_d - y_l)
		if diff <= local_len and (diff + local_len) % 2 == 0:
			is_possible = True
	if is_possible:
		current_sol = local_len
		r = local_len - 1
	else:
		l = local_len + 1
print(current_sol)


n = int(input())
root = input()
(x, y) = list(map(int, input().split()))
(curx, cury) = (0, 0)
(dx, dy) = ([0], [0])
for i in range(n):
	if root[i] == 'L':
		curx -= 1
	if root[i] == 'R':
		curx += 1
	if root[i] == 'U':
		cury += 1
	if root[i] == 'D':
		cury -= 1
	dx.append(curx)
	dy.append(cury)
if curx == x and cury == y:
	print(0)
else:
	ans = n + 1
	for i in range(1, n + 1):
		(l, r) = (i, n)
		while l <= r:
			mid = (l + r) // 2
			dx1 = dx[i - 1]
			dy1 = dy[i - 1]
			dx2 = dx[n] - dx[mid]
			dy2 = dy[n] - dy[mid]
			ddx = x - (dx1 + dx2)
			ddy = y - (dy1 + dy2)
			z = mid - i + 1 - abs(ddx) - abs(ddy)
			if z >= 0 and z % 2 == 0:
				ans = min(ans, mid - i + 1)
				r = mid - 1
			else:
				l = mid + 1
	if ans == n + 1:
		print(-1)
	else:
		print(ans)


from sys import stdin
n = int(stdin.readline())
s = stdin.readline().strip()
(x, y) = [int(z) for z in stdin.readline().split()]
x2 = s.count('R') - s.count('L')
y2 = s.count('U') - s.count('D')
if x == x2 and y == y2:
	print(0)
elif (x - x2 + y - y2) % 2 == 1:
	print(-1)
elif x + y > n:
	print(-1)
else:
	low = n * 2
	xD = x2 - x
	yD = y2 - y
	flex = 0
	left = 0
	right = 0
	while right < n:
		while abs(xD) + abs(yD) > flex and right < n:
			char = s[right]
			if char == 'R':
				xD -= 1
			elif char == 'L':
				xD += 1
			elif char == 'U':
				yD -= 1
			else:
				yD += 1
			flex += 1
			right += 1
		while abs(xD) + abs(yD) <= flex:
			low = min(low, right - left)
			char = s[left]
			if char == 'R':
				xD += 1
			elif char == 'L':
				xD -= 1
			elif char == 'U':
				yD += 1
			else:
				yD -= 1
			flex -= 1
			left += 1
	if low == n * 2:
		print(-1)
	else:
		print(low)


import sys
n = int(input())
S = input()
(x, y) = map(int, input().split())
if abs(x) + abs(y) > n or (abs(x) + abs(y)) % 2 != n % 2:
	print(-1)
	sys.exit()
now = [0, 0]
LISTL = [(0, 0)]
for s in S:
	if s == 'R':
		now[0] += 1
	elif s == 'L':
		now[0] -= 1
	elif s == 'U':
		now[1] += 1
	else:
		now[1] -= 1
	LISTL.append((now[0], now[1]))
LISTR = [(0, 0)]
now = [0, 0]
for s in S[::-1]:
	if s == 'R':
		now[0] += 1
	elif s == 'L':
		now[0] -= 1
	elif s == 'U':
		now[1] += 1
	else:
		now[1] -= 1
	LISTR.append((now[0], now[1]))

def su(a, b, x, y):
	return abs(x - (a[0] + b[0])) + abs(y - (a[1] + b[1]))
ANS = 0
for i in range(n + 1):
	for j in range(max(0, ANS - i), n + 1):
		if su(LISTR[i], LISTL[j], x, y) <= n - i - j:
			if ANS < i + j:
				ANS = i + j
		else:
			break
print(n - ANS)


n = int(input())
subtract = lambda t1, t2: (t1[0] - t2[0], t1[1] - t2[1])
add = lambda t1, t2: (t1[0] + t2[0], t1[1] + t2[1])

def conv(ch):
	if ch == 'L':
		return (-1, 0)
	elif ch == 'R':
		return (1, 0)
	elif ch == 'U':
		return (0, 1)
	elif ch == 'D':
		return (0, -1)
ops = [conv(ch) for ch in input()]
(x, y) = list(map(int, input().split()))
lsum = [ops[0]] * n
for i in range(1, n):
	lsum[i] = add(lsum[i - 1], ops[i])
rsum = [ops[n - 1]] * n
i = n - 2
while i >= 0:
	rsum[i] = add(rsum[i + 1], ops[i])
	i = i - 1

def check(L):
	for i in range(0, n - L + 1):
		moves = (x, y)
		if i > 0:
			moves = subtract(moves, lsum[i - 1])
		if i + L < n:
			moves = subtract(moves, rsum[i + L])
		turns = abs(moves[0]) + abs(moves[1])
		if turns <= L and (L - turns) % 2 == 0:
			return True
	return False
if abs(x) + abs(y) > n or (abs(x) + abs(y) - n) % 2 != 0:
	print(-1)
else:
	(st, en) = (0, n)
	while st < en:
		md = (st + en) // 2
		if check(md):
			en = md
		else:
			st = md + 1
	print(st)


def check(num):
	r = R[n - 1] - R[num - 1]
	l = L[n - 1] - L[num - 1]
	u = U[n - 1] - U[num - 1]
	d = D[n - 1] - D[num - 1]
	if abs(x - (r - l)) + abs(y - (u - d)) <= num:
		return True
	for i in range(num, n):
		r = R[n - 1] - (R[i] - R[i - num])
		l = L[n - 1] - (L[i] - L[i - num])
		u = U[n - 1] - (U[i] - U[i - num])
		d = D[n - 1] - (D[i] - D[i - num])
		if abs(x - (r - l)) + abs(y - (u - d)) <= num:
			return True
	return False
n = int(input())
s = input()
(x, y) = map(int, input().split())
(R, L, U, D) = ({}, {}, {}, {})
for i in range(n):
	R[i] = R[i - 1] if i > 0 else 0
	L[i] = L[i - 1] if i > 0 else 0
	U[i] = U[i - 1] if i > 0 else 0
	D[i] = D[i - 1] if i > 0 else 0
	if s[i] == 'R':
		R[i] += 1
	elif s[i] == 'L':
		L[i] += 1
	elif s[i] == 'U':
		U[i] += 1
	else:
		D[i] += 1
if abs(x - R[n - 1] + L[n - 1]) + abs(y - (U[n - 1] - D[n - 1])) == 0:
	print(0)
	exit(0)
if (abs(x) + abs(y)) % 2 != n % 2:
	print(-1)
	exit(0)
low = 1
high = n
while low < high:
	mid = (low + high) // 2
	if check(mid):
		high = mid - 1
	else:
		low = mid + 1
if check(low):
	print(low)
else:
	if low == n:
		print(-1)
		exit(0)
	print(low + 1)


import sys
import bisect
t = 1
mod = 10 ** 9 + 7

def opx(c):
	if c == 'L':
		return -1
	if c == 'R':
		return 1
	return 0

def opy(c):
	if c == 'D':
		return -1
	if c == 'U':
		return 1
	return 0

def utilch(n1, x1, y1):
	x1 = xi - x1
	y1 = yi - y1
	diffx = abs(x1 - xf)
	diffy = abs(y1 - yf)
	if diffy + diffx <= n1 and (diffy + diffx) % 2 == n1 % 2:
		return True
	return False

def ch(n1):
	(x1, y1) = (0, 0)
	for i in range(n1):
		x1 += opx(s[i])
		y1 += opy(s[i])
	if utilch(n1, x1, y1):
		return True
	for i in range(n1, n):
		x1 -= opx(s[i - n1])
		x1 += opx(s[i])
		y1 -= opy(s[i - n1])
		y1 += opy(s[i])
		if utilch(n1, x1, y1):
			return True
	return False
for _ in range(t):
	n = int(input())
	s = input()
	(xf, yf) = map(int, input().split())
	(xi, yi) = (0, 0)
	for i in range(len(s)):
		xi += opx(s[i])
		yi += opy(s[i])
	if abs(xf) + abs(yf) <= n and (abs(xf) + abs(yf)) % 2 == n % 2:
		(l, r) = (-1, n)
		ans = n
		while r - l > 1:
			mid = (l + r) // 2
			if ch(mid):
				ans = min(ans, mid)
				r = mid
			else:
				l = mid
		print(ans)
	else:
		print(-1)


import sys
input = sys.stdin.readline
n = int(input())
s = input().rstrip()
(x, y) = map(int, input().split())
(curx, cury) = (0, 0)
dx = [0]
dy = [0]
for i in range(n):
	if s[i] == 'L':
		curx -= 1
	if s[i] == 'R':
		curx += 1
	if s[i] == 'U':
		cury += 1
	if s[i] == 'D':
		cury -= 1
	dx.append(curx)
	dy.append(cury)
if curx == x and cury == y:
	print(0)
	exit()
ans = n + 1
for i in range(1, n + 1):
	(l, r) = (i, n)
	while l <= r:
		mid = (l + r) // 2
		dx1 = dx[i - 1]
		dy1 = dy[i - 1]
		dx2 = dx[n] - dx[mid]
		dy2 = dy[n] - dy[mid]
		ddx = x - (dx1 + dx2)
		ddy = y - (dy1 + dy2)
		z = mid - i + 1 - abs(ddx) - abs(ddy)
		if z % 2 == 0 and z >= 0:
			ans = min(ans, mid - i + 1)
			r = mid - 1
		else:
			l = mid + 1
if ans == n + 1:
	print(-1)
else:
	print(ans)


n = int(input())
s = input()
(p, q) = input().split()
if p[0] == '-':
	x = -1 * int(p[1:])
else:
	x = int(p)
if q[0] == '-':
	y = -1 * int(q[1:])
else:
	y = int(q)
cur = [0, 0]
if abs(x) + abs(y) > n:
	print(-1)
elif (x + y) % 2 != n % 2:
	print(-1)
else:
	end = n
	for i in range(n):
		if s[i] == 'R':
			cur[0] += 1
		if s[i] == 'L':
			cur[0] -= 1
		if s[i] == 'U':
			cur[1] += 1
		if s[i] == 'D':
			cur[1] -= 1
		if abs(x - cur[0]) + abs(y - cur[1]) >= n - i:
			end = i
			break
	if end == n:
		print(0)
	else:
		m = [0] * (end + 1)
		start = n
		for i in range(end, -1, -1):
			if s[i] == 'R':
				cur[0] -= 1
			if s[i] == 'L':
				cur[0] += 1
			if s[i] == 'U':
				cur[1] -= 1
			if s[i] == 'D':
				cur[1] += 1
			while abs(x - cur[0]) + abs(y - cur[1]) <= start - i:
				start -= 1
				if s[start] == 'R':
					x -= 1
				if s[start] == 'L':
					x += 1
				if s[start] == 'U':
					y -= 1
				if s[start] == 'D':
					y += 1
			m[i] = start - i + 1
		minn = n
		for i in m:
			minn = min(minn, i)
		print(minn)


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n
avl = AvlTree()

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
n = int(input())
s = input()
(a, b) = map(int, input().split())
dy = 0
dx = 0
for i in range(n):
	if s[i] == 'U':
		dy += 1
	elif s[i] == 'D':
		dy -= 1
	elif s[i] == 'L':
		dx -= 1
	else:
		dx += 1
if (a - dx) % 2 != (b - dy) % 2:
	print(-1)
	sys.exit(0)
elif abs(a) + abs(b) > n:
	print(-1)
	sys.exit(0)
elif dx == a and dy == b:
	print(0)
	sys.exit(0)

def calc(dy1, dx1, n1):
	a1 = a - (dx - dx1)
	b1 = b - (dy - dy1)
	if (a1 - dx1) % 2 != (b1 - dy1) % 2:
		return False
	elif abs(a1) + abs(b1) > n1:
		return False
	return True

def check(n1):
	dy1 = 0
	dx1 = 0
	for i in range(n1):
		if s[i] == 'U':
			dy1 += 1
		elif s[i] == 'D':
			dy1 -= 1
		elif s[i] == 'L':
			dx1 -= 1
		else:
			dx1 += 1
	if calc(dy1, dx1, n1) == True:
		return True
	for i in range(0, n - n1):
		if s[i] == 'U':
			dy1 -= 1
		elif s[i] == 'D':
			dy1 += 1
		elif s[i] == 'L':
			dx1 += 1
		else:
			dx1 -= 1
		if s[i + n1] == 'U':
			dy1 += 1
		elif s[i + n1] == 'D':
			dy1 -= 1
		elif s[i + n1] == 'L':
			dx1 -= 1
		else:
			dx1 += 1
		if calc(dy1, dx1, n1) == True:
			return True
	return False
st = 1
end = n
ans = n
while st <= end:
	mid = (st + end) // 2
	if check(mid) == True:
		ans = mid
		end = mid - 1
	else:
		st = mid + 1
print(ans)


import sys

def input():
	return sys.stdin.readline().rstrip()

def slv():
	n = int(input())
	s = list(input())
	(x, y) = map(int, input().split())
	if not (abs(x) + abs(y) <= n and (abs(x) + abs(y) - n) % 2 == 0):
		print(-1)
		return
	(tot_x, tot_y) = (0, 0)
	for c in s:
		if c == 'D':
			tot_y -= 1
		elif c == 'U':
			tot_y += 1
		elif c == 'R':
			tot_x += 1
		else:
			tot_x -= 1

	def possible(R):
		if R < 0:
			return False
		(idxl, idxr) = (0, 0)
		(rx, ry) = (0, 0)
		addition = 0
		while idxr < n:
			c = s[idxr]
			if c == 'D':
				ry -= 1
			elif c == 'U':
				ry += 1
			elif c == 'R':
				rx += 1
			else:
				rx -= 1
			addition += 1
			idxr += 1
			if addition > R:
				c = s[idxl]
				if c == 'D':
					ry += 1
				elif c == 'U':
					ry -= 1
				elif c == 'R':
					rx -= 1
				else:
					rx += 1
				idxl += 1
				addition -= 1
			if addition == R:
				(move_x, move_y) = (x - (tot_x - rx), y - (tot_y - ry))
				if abs(move_x) + abs(move_y) <= R and R % 2 == (abs(move_x) + abs(move_y)) % 2:
					return True
		return False
	ans = n + 100
	if possible(2 * (n // 2)):
		l = -1
		r = n // 2
		while r - l > 1:
			med = (l + r) // 2
			if possible(2 * med):
				r = med
			else:
				l = med
		ans = min(ans, 2 * r)
	if possible(2 * ((n - 1) // 2) + 1):
		l = -1
		r = (n - 1) // 2
		while r - l > 1:
			med = (l + r) // 2
			if possible(2 * med + 1):
				r = med
			else:
				l = med
		ans = min(ans, 2 * r + 1)
	print(ans)
	return

def main():
	t = 1
	for i in range(t):
		slv()
	return
main()


n = int(input())
s = input()
(x, y) = [int(x) for x in input().split()]
if abs(x) + abs(y) > n or (n - abs(x) - abs(y)) % 2 != 0:
	print(-1)
	exit()
left = [(0, 0)]
for i in range(n):
	(last_x, last_y) = left[-1]
	if s[i] == 'U':
		left.append((last_x, last_y + 1))
	elif s[i] == 'R':
		left.append((last_x + 1, last_y))
	elif s[i] == 'L':
		left.append((last_x - 1, last_y))
	elif s[i] == 'D':
		left.append((last_x, last_y - 1))
if left[-1] == (x, y):
	print(0)
	exit()
right = [(0, 0)]
for i in reversed(range(n)):
	(last_x, last_y) = right[-1]
	if s[i] == 'U':
		right.append((last_x, last_y + 1))
	elif s[i] == 'R':
		right.append((last_x + 1, last_y))
	elif s[i] == 'L':
		right.append((last_x - 1, last_y))
	elif s[i] == 'D':
		right.append((last_x, last_y - 1))
best = n
j = 1

def not_good(x1, y1, x2, y2, len):
	d = abs(x - (x1 + x2)) + abs(y - (y1 + y2))
	return len < d or (len - d) % 2 != 0
for i in range(n + 1):
	(x1, y1) = left[i]
	j -= 2
	if j <= i:
		j = i + 1
	(x2, y2) = right[n - j]
	while n - j >= 0 and not_good(x1, y1, x2, y2, j - i):
		j += 1
		if n - j >= 0:
			(x2, y2) = right[n - j]
	if j < n + 1:
		best = min(best, j - i)
print(best)


n = int(input())
a = input()
(x, y) = map(int, input().split())
if abs(x) + abs(y) > n or (x + y + n) % 2 != 0:
	print(-1)
else:
	dx = [0] * n
	dy = [0] * n
	for i in range(n):
		if a[i] == 'R':
			dx[i] = 1
		elif a[i] == 'L':
			dx[i] = -1
		elif a[i] == 'U':
			dy[i] = 1
		else:
			dy[i] = -1
	xx = sum(dx)
	yy = sum(dy)
	if xx == x and yy == y:
		print(0)
	else:
		l = 0
		res = n
		for r in range(n):
			xx -= dx[r]
			yy -= dy[r]
			while r - l + 1 >= abs(x - xx) + abs(y - yy):
				xx += dx[l]
				yy += dy[l]
				l += 1
			if l > 0:
				res = min(res, r - l + 2)
		print(res)


class Vec(object):

	def __init__(self, x, y):
		(self.x, self.y) = (x, y)

	def __add__(self, other):
		return Vec(self.x + other.x, self.y + other.y)

	def __sub__(self, other):
		return Vec(self.x - other.x, self.y - other.y)

	def len(self):
		return abs(self.x) + abs(self.y)

	def __repr__(self):
		return f'{self.x}, {self.y}'
md = {'U': Vec(0, 1), 'D': Vec(0, -1), 'L': Vec(-1, 0), 'R': Vec(1, 0)}
n = int(input())
s = input()
(u, v) = map(int, input().split())
if Vec(u, v).len() > n or Vec(u, v).len() % 2 != n % 2:
	print('-1')
	exit()
pre = [Vec(0, 0) for _ in range(n + 2)]
suf = [Vec(0, 0) for _ in range(n + 2)]
for i in range(1, n + 1):
	pre[i] = pre[i - 1] + md[s[i - 1]]
for i in range(n, 0, -1):
	suf[i] = suf[i + 1] + md[s[i - 1]]

def check(p, q):
	p += 1
	q += 1
	return (pre[p - 1] + suf[q] - Vec(u, v)).len() <= q - p
ans = n
(l, r) = (0, 0)
while l < n:
	while r < n and (not check(l, r)):
		r += 1
	if check(l, r):
		ans = min(ans, r - l)
	l += 1
print(ans)


from __future__ import print_function
from itertools import permutations
from sys import stdin
from heapq import *
from math import *
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')
(n,) = gil()
s = g()
(x, y) = gil()
(xm, ym) = ({'L': -1, 'R': 1}, {'U': 1, 'D': -1})

def update(x, y, ch):
	return (x + xm.get(ch, 0), y + ym.get(ch, 0))
if n < abs(x) + abs(y) or n - abs(x) - abs(y) & 1:
	print(-1)
	exit()
(rx, ry) = ([0] * (n + 1), [0] * (n + 1))
for i in reversed(range(n)):
	(rx[i], ry[i]) = update(rx[i + 1], ry[i + 1], s[i])

def isPos(k):
	(bx, by) = (0, 0)
	for i in range(k - 1, n):
		req = abs(x - (bx + rx[i + 1])) + abs(y - (by + ry[i + 1]))
		have = k
		if have >= req and have - req & 1 == 0:
			return True
		(bx, by) = update(bx, by, s[i - k + 1])
	return False
if (rx[0], ry[0]) == (x, y):
	print(0)
	exit()
(l, r) = (1, n)
ans = n
while l <= r:
	mid = (l + r) // 2
	if isPos(mid):
		ans = mid
		r = mid - 1
	else:
		l = mid + 1
print(ans)