#include <bits/stdc++.h>

using namespace std;

int xcur, ycur, n, x, y, xt, yt;
string s;

void add(int& xx, int& yy, char v) {
    switch (v) {
        case 'U':
            yy++;
            break;
        case 'D':
            yy--;
            break;
        case 'L':
            xx--;
            break;
        case 'R':
            xx++;
            break;
    }
}

bool try_to_change(int l, int r) {
    for (int i = l; i < r; ++i) {
        if (s[i] == 'U' && yt > 0) {
            if (xt == 0) {
                yt -= 2;
            } else {
                yt -= 1;
                if (xt > 0) {
                    xt--;
                } else {
                    xt++;
                }
            }
        }
        if (s[i] == 'D' && yt < 0) {
            if (xt == 0) {
                yt += 2;
            } else {
                yt += 1;
                if (xt > 0) {
                    xt--;
                } else {
                    xt++;
                }
            }
        }
        if (s[i] == 'L' && xt > 0) {
            if (yt == 0) {
                xt -= 2;
            } else {
                xt -= 1;
                if (yt > 0) {
                    yt--;
                } else {
                    yt++;
                }
            }
        }
        if (s[i] == 'R' && xt < 0) {
            if (yt == 0) {
                xt += 2;
            } else {
                xt += 1;
                if (yt > 0) {
                    yt--;
                } else {
                    yt++;
                }
            }
        }
    }
    return !xt && !yt;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin >> n >> s;
    cin >> x >> y;
    for (int i = 0; i < n; ++i) {
        add(xcur, ycur, s[i]);
    }
    xt = x - xcur;
    yt = y - ycur;
    if ((xt + yt) % 2) {
        cout << -1 << endl;
        return 0;
    }
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= n - i; ++j) {
            xt = x - xcur;
            yt = y - ycur;
            if (try_to_change(j, i + j)) {
                cout << i << endl;
                return 0;
            }
        }
    }
    cout << -1 << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int xcur, ycur, n, x, y, xt, yt;
string s;

void add(int& xx, int& yy, char v) {
    switch (v) {
        case 'U':
            yy++;
            break;
        case 'D':
            yy--;
            break;
        case 'L':
            xx--;
            break;
        case 'R':
            xx++;
            break;
    }
}

bool try_to_change(int l, int r) {
    for (int i = l; i < r; ++i) {
        if (s[i] == 'U' && yt > 0) {
            if (xt == 0) {
                yt -= 2;
            } else {
                yt -= 1;
                if (xt > 0) {
                    xt--;
                } else {
                    xt++;
                }
            }
        }
        if (s[i] == 'D' && yt < 0) {
            if (xt == 0) {
                yt += 2;
            } else {
                yt += 1;
                if (xt > 0) {
                    xt--;
                } else {
                    xt++;
                }
            }
        }
        if (s[i] == 'L' && xt > 0) {
            if (yt == 0) {
                xt -= 2;
            } else {
                xt -= 1;
                if (yt > 0) {
                    yt--;
                } else {
                    yt++;
                }
            }
        }
        if (s[i] == 'R' && xt < 0) {
            if (yt == 0) {
                xt += 2;
            } else {
                xt += 1;
                if (yt > 0) {
                    yt--;
                } else {
                    yt++;
                }
            }
        }
    }
    return !xt && !yt;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin >> n >> s;
    cin >> x >> y;
    for (int i = 0; i < n; ++i) {
        add(xcur, ycur, s[i]);
    }
    xt = x - xcur;
    yt = y - ycur;
    if ((xt + yt) % 2) {
        cout << -1 << endl;
        return 0;
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= n - i; ++j) {
            xt = x - xcur;
            yt = y - ycur;
            if (try_to_change(j, i + j)) {
                cout << i << endl;
                return 0;
            }
        }
    }
    cout << -1 << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int xcur, ycur, n, x, y, xt, yt;
string s;

void add(int& xx, int& yy, char v) {
    switch (v) {
        case 'U':
            yy++;
            break;
        case 'D':
            yy--;
            break;
        case 'L':
            xx--;
            break;
        case 'R':
            xx++;
            break;
    }
}

bool try_to_change(int l, int r) {
    int xx = 0, yy = 0;
    for (int i = l; i < r; ++i) {
        add(xx, yy, s[i]);
    }
    //cout << l << " " << r << " " << xx << " " << yy << " " << xt << " " << yt << " ";
    xt += xx;
    yt += yy;
    //cout << xt << " " << yt << endl;
    return r - l >= abs(xt) + abs(yt);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin >> n >> s;
    cin >> x >> y;
    for (int i = 0; i < n; ++i) {
        add(xcur, ycur, s[i]);
    }
    xt = x - xcur;
    yt = y - ycur;
    if ((xt + yt) % 2 || abs(xt) + abs(yt) > n) {
        cout << -1 << endl;
        return 0;
    }
    int l = -1, r = n;
    while(r - l > 1) {
        int m = (l + r) / 2;
        for (int j = 0; j <= n - m; ++j) {
            xt = x - xcur;
            yt = y - ycur;
            if (try_to_change(j, m + j)) {
                r = m;
                break;
            }
            if (j == n - m) {
                l = m;
            }
        }
    }
    cout << r << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

// for debug
//#define cin myCin
//ifstream cin("test.txt");
//#define cout myCout
//ofstream cout("res.txt");
#define STOP system("pause");

#define FORU(i, a, b) for (int i = a; i < b; i++)
#define FORD(i, a, b) for (int i = a - 1; i >= b; i--)
#define ALL(v) v.begin(), v.end()
#define MAXEL(x, n) *max_element(x, x + n)
#define MAXELS(v) *max_element(ALL(v))
#define MINEL(x, n) *min_element(x, x + n)
#define MINELS(v) *min_element(ALL(v))
#define LB(x, n, a) int (lower_bound(x, x + n, a) - x)
#define LBS(v, a) int (lower_bound(ALL(v), a) - v.begin())
#define UB(x, n, a) int (upper_bound(x, x + n, a) - x)
#define UBS(v, a) int (upper_bound(ALL(v), a) - v.begin())
#define SORT(x, n) sort(x, x + n)
#define SORTS(v) sort(ALL(v))
#define PB push_back
#define MP make_pair
#define CINVI(v) { int cur; cin >> cur; v.PB(cur); }
#define BSS(v, a) (LBS(v, a) == v.size() || v[LBS(v, a)] != a ? -1 : LBS(v, a))
#define REV(v) reverse(ALL(v))
#define SQR(a) (a) * (a)

typedef long long li;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;
typedef map <int, int> mii;
typedef map <string, int> msi;
typedef vector <int> vi;
typedef set <int> si;

const int SHINF = 1 << 15;
const int INF = 1 << 30;
const li LINF = 1LL << 62;

// types, constants and variables
const int MAXN = 201010;

int n, x, y, l[MAXN], r[MAXN], d[MAXN], u[MAXN], minL;
string s;

// solving
int getC(int* arr, int a, int b) {
	return a == 0 ? arr[b] : arr[b] - arr[a - 1];
}

bool ok(int a, int b) {
	int xout = getC(r, 0, a - 1) - getC(l, 0, a - 1) + getC(r, b + 1, n - 1) - getC(l, b + 1, n - 1);
	int yout = getC(u, 0, a - 1) - getC(d, 0, a - 1) + getC(u, b + 1, n - 1) - getC(d, b + 1, n - 1);
	int lin = getC(l, a, b), rin = getC(r, a, b), din = getC(d, a, b), uin = getC(u, a, b);
	int xin = rin - lin;
	int yin = uin - din;
	int xinok = x - xout;
	int yinok = y - yout;
	int nab = b - a + 1;
	if (abs(xinok) + abs(yinok) > nab  || (nab - (abs(xinok) + abs(yinok))) % 2) {
		return false;
	}
	return true;
}

void solve() {
	if (abs(x) + abs(y) > n || (n - (abs(x) + abs(y))) % 2) {
		cout << -1 << endl;
		return;
	}
	l[0] = s[0] == 'L';
	r[0] = s[0] == 'R';
	d[0] = s[0] == 'D';
	u[0] = s[0] == 'U';
	for (int i = 1; i < n; i++) {
		l[i] = l[i - 1] + (s[i] == 'L');
		r[i] = r[i - 1] + (s[i] == 'R');
		d[i] = d[i - 1] + (s[i] == 'D');
		u[i] = u[i - 1] + (s[i] == 'U');
	}
	if (r[n - 1] - l[n - 1] == x && u[n - 1] - d[n - 1] == y) {
		cout << 0 << endl;
		return;
	}
	minL = n;
	for (int i = 0; i < n; i++) {
		for (int j = i; j < n; j++) {
			if (ok(i, j)) {
				minL = min(minL, j - i + 1);
			}
		}
	}
	cout << minL << endl;
}

// reading
int main() {
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> s >> x >> y;

	//-----------------------------------------------

	solve();
}


#include <bits/stdc++.h>

using namespace std;

// for debug
//#define cin myCin
//ifstream cin("test.txt");
//#define cout myCout
//ofstream cout("res.txt");
#define STOP system("pause");

#define FORU(i, a, b) for (int i = a; i < b; i++)
#define FORD(i, a, b) for (int i = a - 1; i >= b; i--)
#define ALL(v) v.begin(), v.end()
#define MAXEL(x, n) *max_element(x, x + n)
#define MAXELS(v) *max_element(ALL(v))
#define MINEL(x, n) *min_element(x, x + n)
#define MINELS(v) *min_element(ALL(v))
#define LB(x, n, a) int (lower_bound(x, x + n, a) - x)
#define LBS(v, a) int (lower_bound(ALL(v), a) - v.begin())
#define UB(x, n, a) int (upper_bound(x, x + n, a) - x)
#define UBS(v, a) int (upper_bound(ALL(v), a) - v.begin())
#define SORT(x, n) sort(x, x + n)
#define SORTS(v) sort(ALL(v))
#define PB push_back
#define MP make_pair
#define CINVI(v) { int cur; cin >> cur; v.PB(cur); }
#define BSS(v, a) (LBS(v, a) == v.size() || v[LBS(v, a)] != a ? -1 : LBS(v, a))
#define REV(v) reverse(ALL(v))
#define SQR(a) (a) * (a)

typedef long long li;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;
typedef map <int, int> mii;
typedef map <string, int> msi;
typedef vector <int> vi;
typedef set <int> si;

const int SHINF = 1 << 15;
const int INF = 1 << 30;
const li LINF = 1LL << 62;

// types, constants and variables
const int MAXN = 201010;

int n, x, y, l[MAXN], r[MAXN], d[MAXN], u[MAXN], minL;
string s;

// solving
int getC(int* arr, int a, int b) {
	return a == 0 ? arr[b] : arr[b] - arr[a - 1];
}

bool ok(int a, int b) {
	int xout = getC(r, 0, a - 1) - getC(l, 0, a - 1) + getC(r, b + 1, n - 1) - getC(l, b + 1, n - 1);
	int yout = getC(u, 0, a - 1) - getC(d, 0, a - 1) + getC(u, b + 1, n - 1) - getC(d, b + 1, n - 1);
	int lin = getC(l, a, b), rin = getC(r, a, b), din = getC(d, a, b), uin = getC(u, a, b);
	int xin = rin - lin;
	int yin = uin - din;
	int xinok = x - xout;
	int yinok = y - yout;
	int nab = b - a + 1;
	if (abs(xinok) + abs(yinok) > nab  || (nab - (abs(xinok) + abs(yinok))) % 2) {
		return false;
	}
	return true;
}

void solve() {
	if (abs(x) + abs(y) > n || (n - (abs(x) + abs(y))) % 2) {
		cout << -1 << endl;
		return;
	}
	l[0] = s[0] == 'L';
	r[0] = s[0] == 'R';
	d[0] = s[0] == 'D';
	u[0] = s[0] == 'U';
	for (int i = 1; i < n; i++) {
		l[i] = l[i - 1] + (s[i] == 'L');
		r[i] = r[i - 1] + (s[i] == 'R');
		d[i] = d[i - 1] + (s[i] == 'D');
		u[i] = u[i - 1] + (s[i] == 'U');
	}
	int rall = r[n - 1] - l[n - 1], uall = u[n - 1] - d[n - 1];
	if (rall == x && uall == y) {
		cout << 0 << endl;
		return;
	}
	minL = n;
	int chmax = abs(x - rall) + abs(y - uall), chmin = chmax / 2 - 1;
	for (int k = chmin; k <= chmax; k++) {
		for (int i = 0; i <= n - 1 - k; i++) {
			if (ok(i, i + k)) {
				minL = min(minL, k + 1);
 			}
		}
	}
//	for (int i = 0; i < n; i++) {
//		for (int j = i; j < n; j++) {
//			if (ok(i, j)) {
//				minL = min(minL, j - i + 1);
//			}
//		}
//	}
	cout << minL << endl;
}

// reading
int main() {
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> s >> x >> y;

	//-----------------------------------------------

	solve();
}


#include <bits/stdc++.h>

using namespace std;

// for debug
//#define cin myCin
//ifstream cin("test.txt");
//#define cout myCout
//ofstream cout("res.txt");
#define STOP system("pause");

#define FORU(i, a, b) for (int i = a; i < b; i++)
#define FORD(i, a, b) for (int i = a - 1; i >= b; i--)
#define ALL(v) v.begin(), v.end()
#define MAXEL(x, n) *max_element(x, x + n)
#define MAXELS(v) *max_element(ALL(v))
#define MINEL(x, n) *min_element(x, x + n)
#define MINELS(v) *min_element(ALL(v))
#define LB(x, n, a) int (lower_bound(x, x + n, a) - x)
#define LBS(v, a) int (lower_bound(ALL(v), a) - v.begin())
#define UB(x, n, a) int (upper_bound(x, x + n, a) - x)
#define UBS(v, a) int (upper_bound(ALL(v), a) - v.begin())
#define SORT(x, n) sort(x, x + n)
#define SORTS(v) sort(ALL(v))
#define PB push_back
#define MP make_pair
#define CINVI(v) { int cur; cin >> cur; v.PB(cur); }
#define BSS(v, a) (LBS(v, a) == v.size() || v[LBS(v, a)] != a ? -1 : LBS(v, a))
#define REV(v) reverse(ALL(v))
#define SQR(a) (a) * (a)

typedef long long li;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;
typedef map <int, int> mii;
typedef map <string, int> msi;
typedef vector <int> vi;
typedef set <int> si;

const int SHINF = 1 << 15;
const int INF = 1 << 30;
const li LINF = 1LL << 62;

// types, constants and variables
const int MAXN = 201010;

int n, x, y, l[MAXN], r[MAXN], d[MAXN], u[MAXN], minL;
string s;

// solving
int getC(int* arr, int a, int b) {
	return a == 0 ? arr[b] : arr[b] - arr[a - 1];
}

bool ok(int a, int b) {
	int xout = getC(r, 0, a - 1) - getC(l, 0, a - 1) + getC(r, b + 1, n - 1) - getC(l, b + 1, n - 1);
	int yout = getC(u, 0, a - 1) - getC(d, 0, a - 1) + getC(u, b + 1, n - 1) - getC(d, b + 1, n - 1);
	int lin = getC(l, a, b), rin = getC(r, a, b), din = getC(d, a, b), uin = getC(u, a, b);
	int xin = rin - lin;
	int yin = uin - din;
	int xinok = x - xout;
	int yinok = y - yout;
	int nab = b - a + 1;
	if (abs(xinok) + abs(yinok) > nab  || (nab - (abs(xinok) + abs(yinok))) % 2) {
		return false;
	}
	return true;
}

void solve() {
	if (abs(x) + abs(y) > n || (n - (abs(x) + abs(y))) % 2) {
		cout << -1 << endl;
		return;
	}
	l[0] = s[0] == 'L';
	r[0] = s[0] == 'R';
	d[0] = s[0] == 'D';
	u[0] = s[0] == 'U';
	for (int i = 1; i < n; i++) {
		l[i] = l[i - 1] + (s[i] == 'L');
		r[i] = r[i - 1] + (s[i] == 'R');
		d[i] = d[i - 1] + (s[i] == 'D');
		u[i] = u[i - 1] + (s[i] == 'U');
	}
	if (r[n - 1] - l[n - 1] == x && u[n - 1] - d[n - 1] == y) {
		cout << 0 << endl;
		return;
	}
	minL = n;
	int p1, p2;
	p1 = p2 = 0;
	while (p2 < n) {
		if (ok(p1, p2)) {
			while (p2 >= p1 && ok(p1, p2)) p1++;
			if (p2 < p1) p1--;
			minL = min(minL, p2 - p1 + 2);
		}
		p2++;
	}
	cout << minL << endl;
}

// reading
int main() {
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> s >> x >> y;

	//-----------------------------------------------

	solve();
}


#include <bits/stdc++.h>

using namespace std;

// for debug
//#define cin myCin
//ifstream cin("test.txt");
//#define cout myCout
//ofstream cout("res.txt");
#define STOP system("pause");

#define FORU(i, a, b) for (int i = a; i < b; i++)
#define FORD(i, a, b) for (int i = a - 1; i >= b; i--)
#define ALL(v) v.begin(), v.end()
#define MAXEL(x, n) *max_element(x, x + n)
#define MAXELS(v) *max_element(ALL(v))
#define MINEL(x, n) *min_element(x, x + n)
#define MINELS(v) *min_element(ALL(v))
#define LB(x, n, a) int (lower_bound(x, x + n, a) - x)
#define LBS(v, a) int (lower_bound(ALL(v), a) - v.begin())
#define UB(x, n, a) int (upper_bound(x, x + n, a) - x)
#define UBS(v, a) int (upper_bound(ALL(v), a) - v.begin())
#define SORT(x, n) sort(x, x + n)
#define SORTS(v) sort(ALL(v))
#define PB push_back
#define MP make_pair
#define CINVI(v) { int cur; cin >> cur; v.PB(cur); }
#define BSS(v, a) (LBS(v, a) == v.size() || v[LBS(v, a)] != a ? -1 : LBS(v, a))
#define REV(v) reverse(ALL(v))
#define SQR(a) (a) * (a)

typedef long long li;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;
typedef map <int, int> mii;
typedef map <string, int> msi;
typedef vector <int> vi;
typedef set <int> si;

const int SHINF = 1 << 15;
const int INF = 1 << 30;
const li LINF = 1LL << 62;

// types, constants and variables
const int MAXN = 201010;

int n, x, y, l[MAXN], r[MAXN], d[MAXN], u[MAXN], minL;
string s;

// solving
int getC(int* arr, int a, int b) {
	return a == 0 ? arr[b] : arr[b] - arr[a - 1];
}

bool ok(int a, int b) {
	int xout = getC(r, 0, a - 1) - getC(l, 0, a - 1) + getC(r, b + 1, n - 1) - getC(l, b + 1, n - 1);
	int yout = getC(u, 0, a - 1) - getC(d, 0, a - 1) + getC(u, b + 1, n - 1) - getC(d, b + 1, n - 1);
	int lin = getC(l, a, b), rin = getC(r, a, b), din = getC(d, a, b), uin = getC(u, a, b);
	int xin = rin - lin;
	int yin = uin - din;
	int xinok = x - xout;
	int yinok = y - yout;
	int nab = b - a + 1;
	if (abs(xinok) + abs(yinok) > nab  || (nab - (abs(xinok) + abs(yinok))) % 2) {
		return false;
	}
	return true;
}

void solve() {
	if (abs(x) + abs(y) > n || (n - (abs(x) + abs(y))) % 2) {
		cout << -1 << endl;
		return;
	}
	l[0] = s[0] == 'L';
	r[0] = s[0] == 'R';
	d[0] = s[0] == 'D';
	u[0] = s[0] == 'U';
	for (int i = 1; i < n; i++) {
		l[i] = l[i - 1] + (s[i] == 'L');
		r[i] = r[i - 1] + (s[i] == 'R');
		d[i] = d[i - 1] + (s[i] == 'D');
		u[i] = u[i - 1] + (s[i] == 'U');
	}
	if (r[n - 1] - l[n - 1] == x && u[n - 1] - d[n - 1] == y) {
		cout << 0 << endl;
		return;
	}
	minL = n;
	int p1, p2;
	p1 = p2 = 0;
	while (p2 < n) {
		if (ok(p1, p2)) {
			while (p2 >= p1 && ok(p1, p2)) p1++;
			if (p2 < p1) p1--;
			minL = min(minL, p2 - p1 + 1);
		}
		p2++;
	}
	cout << minL << endl;
}

// reading
int main() {
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> s >> x >> y;

	//-----------------------------------------------

	solve();
}


#include <bits/stdc++.h>

using namespace std;

// for debug
//#define cin myCin
//ifstream cin("test.txt");
//#define cout myCout
//ofstream cout("res.txt");
#define STOP system("pause");

#define FORU(i, a, b) for (int i = a; i < b; i++)
#define FORD(i, a, b) for (int i = a - 1; i >= b; i--)
#define ALL(v) v.begin(), v.end()
#define MAXEL(x, n) *max_element(x, x + n)
#define MAXELS(v) *max_element(ALL(v))
#define MINEL(x, n) *min_element(x, x + n)
#define MINELS(v) *min_element(ALL(v))
#define LB(x, n, a) int (lower_bound(x, x + n, a) - x)
#define LBS(v, a) int (lower_bound(ALL(v), a) - v.begin())
#define UB(x, n, a) int (upper_bound(x, x + n, a) - x)
#define UBS(v, a) int (upper_bound(ALL(v), a) - v.begin())
#define SORT(x, n) sort(x, x + n)
#define SORTS(v) sort(ALL(v))
#define PB push_back
#define MP make_pair
#define CINVI(v) { int cur; cin >> cur; v.PB(cur); }
#define BSS(v, a) (LBS(v, a) == v.size() || v[LBS(v, a)] != a ? -1 : LBS(v, a))
#define REV(v) reverse(ALL(v))
#define SQR(a) (a) * (a)

typedef long long li;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;
typedef map <int, int> mii;
typedef map <string, int> msi;
typedef vector <int> vi;
typedef set <int> si;

const int SHINF = 1 << 15;
const int INF = 1 << 30;
const li LINF = 1LL << 62;

// types, constants and variables
const int MAXN = 201010;

int n, x, y, l[MAXN], r[MAXN], d[MAXN], u[MAXN], minL;
string s;

// solving
int getC(int* arr, int a, int b) {
    if (b == -1 || a == n) {
        return 0;
    }
	return a == 0 ? arr[b] : arr[b] - arr[a - 1];
}

bool ok(int a, int b) {
	int xout = getC(r, 0, a - 1) - getC(l, 0, a - 1) + getC(r, b + 1, n - 1) - getC(l, b + 1, n - 1);
	int yout = getC(u, 0, a - 1) - getC(d, 0, a - 1) + getC(u, b + 1, n - 1) - getC(d, b + 1, n - 1);
	int lin = getC(l, a, b), rin = getC(r, a, b), din = getC(d, a, b), uin = getC(u, a, b);
	int xin = rin - lin;
	int yin = uin - din;
	int xinok = x - xout;
	int yinok = y - yout;
	int nab = b - a + 1;
	if (abs(xinok) + abs(yinok) > nab  || (nab - (abs(xinok) + abs(yinok))) % 2) {
		return false;
	}
	return true;
}

void solve() {
	if (abs(x) + abs(y) > n || (n - (abs(x) + abs(y))) % 2) {
		cout << -1 << endl;
		return;
	}
	l[0] = s[0] == 'L';
	r[0] = s[0] == 'R';
	d[0] = s[0] == 'D';
	u[0] = s[0] == 'U';
	for (int i = 1; i < n; i++) {
		l[i] = l[i - 1] + (s[i] == 'L');
		r[i] = r[i - 1] + (s[i] == 'R');
		d[i] = d[i - 1] + (s[i] == 'D');
		u[i] = u[i - 1] + (s[i] == 'U');
	}
	if (r[n - 1] - l[n - 1] == x && u[n - 1] - d[n - 1] == y) {
		cout << 0 << endl;
		return;
	}
	minL = n;
	int p1, p2;
	p1 = p2 = 0;
	while (p2 < n) {
		if (ok(p1, p2)) {
			while (p2 >= p1 && ok(p1, p2)) p1++;
			if (p2 < p1) p1--;
			minL = min(minL, p2 - p1 + 2);
		}
		p2++;
	}
	cout << minL << endl;
}

// reading
int main() {
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> s >> x >> y;

	//-----------------------------------------------

	solve();
}


#include <bits/stdc++.h>

using namespace std;

// for debug
//#define cin myCin
//ifstream cin("test.txt");
//#define cout myCout
//ofstream cout("res.txt");
#define STOP system("pause");

#define FORU(i, a, b) for (int i = a; i < b; i++)
#define FORD(i, a, b) for (int i = a - 1; i >= b; i--)
#define ALL(v) v.begin(), v.end()
#define MAXEL(x, n) *max_element(x, x + n)
#define MAXELS(v) *max_element(ALL(v))
#define MINEL(x, n) *min_element(x, x + n)
#define MINELS(v) *min_element(ALL(v))
#define LB(x, n, a) int (lower_bound(x, x + n, a) - x)
#define LBS(v, a) int (lower_bound(ALL(v), a) - v.begin())
#define UB(x, n, a) int (upper_bound(x, x + n, a) - x)
#define UBS(v, a) int (upper_bound(ALL(v), a) - v.begin())
#define SORT(x, n) sort(x, x + n)
#define SORTS(v) sort(ALL(v))
#define PB push_back
#define MP make_pair
#define CINVI(v) { int cur; cin >> cur; v.PB(cur); }
#define BSS(v, a) (LBS(v, a) == v.size() || v[LBS(v, a)] != a ? -1 : LBS(v, a))
#define REV(v) reverse(ALL(v))
#define SQR(a) (a) * (a)

typedef long long li;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;
typedef map <int, int> mii;
typedef map <string, int> msi;
typedef vector <int> vi;
typedef set <int> si;

const int SHINF = 1 << 15;
const int INF = 1 << 30;
const li LINF = 1LL << 62;

// types, constants and variables
const int MAXN = 201010;

int n, x, y, l[MAXN], r[MAXN], d[MAXN], u[MAXN], minL;
string s;

// solving
int getC(int* arr, int a, int b) {
	return a == 0 ? arr[b] : arr[b] - arr[a - 1];
}

bool ok(int a, int b) {
	int xout = getC(r, 0, a - 1) - getC(l, 0, a - 1) + getC(r, b + 1, n - 1) - getC(l, b + 1, n - 1);
	int yout = getC(u, 0, a - 1) - getC(d, 0, a - 1) + getC(u, b + 1, n - 1) - getC(d, b + 1, n - 1);
	int lin = getC(l, a, b), rin = getC(r, a, b), din = getC(d, a, b), uin = getC(u, a, b);
	int xin = rin - lin;
	int yin = uin - din;
	int xinok = x - xout;
	int yinok = y - yout;
	int nab = b - a + 1;
	if (abs(xinok) + abs(yinok) > nab  || (nab - (abs(xinok) + abs(yinok))) % 2) {
		return false;
	}
	return true;
}

void solve() {
	if (abs(x) + abs(y) > n || (n - (abs(x) + abs(y))) % 2) {
		cout << -1 << endl;
		return;
	}
	l[0] = s[0] == 'L';
	r[0] = s[0] == 'R';
	d[0] = s[0] == 'D';
	u[0] = s[0] == 'U';
	for (int i = 1; i < n; i++) {
		l[i] = l[i - 1] + (s[i] == 'L');
		r[i] = r[i - 1] + (s[i] == 'R');
		d[i] = d[i - 1] + (s[i] == 'D');
		u[i] = u[i - 1] + (s[i] == 'U');
	}
	int rall = r[n - 1] - l[n - 1], uall = u[n - 1] - d[n - 1];
	if (rall == x && uall == y) {
		cout << 0 << endl;
		return;
	}
	minL = n;
	int chmax = abs(x - rall) + abs(y - uall), chmin = chmax / 2;
	//cout << chmin << " " << chmax << endl;
	for (int k = chmin; k <= chmax; k++) {
		for (int i = 0; i <= n - 1 - k; i++) {
			if (ok(i, i + k)) {
				minL = min(minL, k);
 			}
		}
	}
//	for (int i = 0; i < n; i++) {
//		for (int j = i; j < n; j++) {
//			if (ok(i, j)) {
//				minL = min(minL, j - i + 1);
//			}
//		}
//	}
	cout << minL << endl;
}

// reading
int main() {
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> s >> x >> y;

	//-----------------------------------------------

	solve();
}


#include <bits/stdc++.h>

using namespace std;

// for debug
//#define cin myCin
//ifstream cin("test.txt");
//#define cout myCout
//ofstream cout("res.txt");
#define STOP system("pause");

#define FORU(i, a, b) for (int i = a; i < b; i++)
#define FORD(i, a, b) for (int i = a - 1; i >= b; i--)
#define ALL(v) v.begin(), v.end()
#define MAXEL(x, n) *max_element(x, x + n)
#define MAXELS(v) *max_element(ALL(v))
#define MINEL(x, n) *min_element(x, x + n)
#define MINELS(v) *min_element(ALL(v))
#define LB(x, n, a) int (lower_bound(x, x + n, a) - x)
#define LBS(v, a) int (lower_bound(ALL(v), a) - v.begin())
#define UB(x, n, a) int (upper_bound(x, x + n, a) - x)
#define UBS(v, a) int (upper_bound(ALL(v), a) - v.begin())
#define SORT(x, n) sort(x, x + n)
#define SORTS(v) sort(ALL(v))
#define PB push_back
#define MP make_pair
#define CINVI(v) { int cur; cin >> cur; v.PB(cur); }
#define BSS(v, a) (LBS(v, a) == v.size() || v[LBS(v, a)] != a ? -1 : LBS(v, a))
#define REV(v) reverse(ALL(v))
#define SQR(a) (a) * (a)

typedef long long li;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;
typedef map <int, int> mii;
typedef map <string, int> msi;
typedef vector <int> vi;
typedef set <int> si;

const int SHINF = 1 << 15;
const int INF = 1 << 30;
const li LINF = 1LL << 62;

// types, constants and variables
const int MAXN = 201010;

int n, x, y, l[MAXN], r[MAXN], d[MAXN], u[MAXN], minL;
string s;

// solving
int getC(int* arr, int a, int b) {
    if (b == -1 || a == n) {
        return 0;
    }
	return a == 0 ? arr[b] : arr[b] - arr[a - 1];
}

bool ok(int a, int b) {
	int xout = getC(r, 0, a - 1) - getC(l, 0, a - 1) + getC(r, b + 1, n - 1) - getC(l, b + 1, n - 1);
	int yout = getC(u, 0, a - 1) - getC(d, 0, a - 1) + getC(u, b + 1, n - 1) - getC(d, b + 1, n - 1);
	int lin = getC(l, a, b), rin = getC(r, a, b), din = getC(d, a, b), uin = getC(u, a, b);
	int xin = rin - lin;
	int yin = uin - din;
	int xinok = x - xout;
	int yinok = y - yout;
	int nab = b - a + 1;
	if (abs(xinok) + abs(yinok) > nab  || (nab - (abs(xinok) + abs(yinok))) % 2) {
		return false;
	}
	return true;
}

void solve() {
	if (abs(x) + abs(y) > n || (n - (abs(x) + abs(y))) % 2) {
		cout << -1 << endl;
		return;
	}
	l[0] = s[0] == 'L';
	r[0] = s[0] == 'R';
	d[0] = s[0] == 'D';
	u[0] = s[0] == 'U';
	for (int i = 1; i < n; i++) {
		l[i] = l[i - 1] + (s[i] == 'L');
		r[i] = r[i - 1] + (s[i] == 'R');
		d[i] = d[i - 1] + (s[i] == 'D');
		u[i] = u[i - 1] + (s[i] == 'U');
	}
	if (r[n - 1] - l[n - 1] == x && u[n - 1] - d[n - 1] == y) {
		cout << 0 << endl;
		return;
	}
	minL = n;
	int p1, p2;
	p1 = p2 = 0;
	while (p2 < n) {
		if (ok(p1, p2)) {
			while (p2 >= p1 && ok(p1, p2)) p1++;
			minL = min(minL, p2 - p1 + 2);
		}
		p2++;
	}
	cout << minL << endl;
}

// reading
int main() {
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> s >> x >> y;

	//-----------------------------------------------

	solve();
}


#include <bits/stdc++.h>

using namespace std;

// for debug
//#define cin myCin
//ifstream cin("test.txt");
//#define cout myCout
//ofstream cout("res.txt");
#define STOP system("pause");

#define FORU(i, a, b) for (int i = a; i < b; i++)
#define FORD(i, a, b) for (int i = a - 1; i >= b; i--)
#define ALL(v) v.begin(), v.end()
#define MAXEL(x, n) *max_element(x, x + n)
#define MAXELS(v) *max_element(ALL(v))
#define MINEL(x, n) *min_element(x, x + n)
#define MINELS(v) *min_element(ALL(v))
#define LB(x, n, a) int (lower_bound(x, x + n, a) - x)
#define LBS(v, a) int (lower_bound(ALL(v), a) - v.begin())
#define UB(x, n, a) int (upper_bound(x, x + n, a) - x)
#define UBS(v, a) int (upper_bound(ALL(v), a) - v.begin())
#define SORT(x, n) sort(x, x + n)
#define SORTS(v) sort(ALL(v))
#define PB push_back
#define MP make_pair
#define CINVI(v) { int cur; cin >> cur; v.PB(cur); }
#define BSS(v, a) (LBS(v, a) == v.size() || v[LBS(v, a)] != a ? -1 : LBS(v, a))
#define REV(v) reverse(ALL(v))
#define SQR(a) (a) * (a)

typedef long long li;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;
typedef map <int, int> mii;
typedef map <string, int> msi;
typedef vector <int> vi;
typedef set <int> si;

const int SHINF = 1 << 15;
const int INF = 1 << 30;
const li LINF = 1LL << 62;

// types, constants and variables
const int MAXN = 201010;

int n, x, y, l[MAXN], r[MAXN], d[MAXN], u[MAXN], minL;
string s;

// solving
int getC(int* arr, int a, int b) {
	return a == 0 ? arr[b] : arr[b] - arr[a - 1];
}

bool ok(int a, int b) {
	int xout = getC(r, 0, a - 1) - getC(l, 0, a - 1) + getC(r, b + 1, n - 1) - getC(l, b + 1, n - 1);
	int yout = getC(u, 0, a - 1) - getC(d, 0, a - 1) + getC(u, b + 1, n - 1) - getC(d, b + 1, n - 1);
	int lin = getC(l, a, b), rin = getC(r, a, b), din = getC(d, a, b), uin = getC(u, a, b);
	int xin = rin - lin;
	int yin = uin - din;
	int xinok = x - xout;
	int yinok = y - yout;
	int nab = b - a + 1;
	if (abs(xinok) + abs(yinok) > nab  || (nab - (abs(xinok) + abs(yinok))) % 2) {
		return false;
	}
	return true;
}

void solve() {
	if (abs(x) + abs(y) > n || (n - (abs(x) + abs(y))) % 2) {
		cout << -1 << endl;
		return;
	}
	l[0] = s[0] == 'L';
	r[0] = s[0] == 'R';
	d[0] = s[0] == 'D';
	u[0] = s[0] == 'U';
	for (int i = 1; i < n; i++) {
		l[i] = l[i - 1] + (s[i] == 'L');
		r[i] = r[i - 1] + (s[i] == 'R');
		d[i] = d[i - 1] + (s[i] == 'D');
		u[i] = u[i - 1] + (s[i] == 'U');
	}
	if (r[n - 1] - l[n - 1] == x && u[n - 1] - d[n - 1] == y) {
		cout << 0 << endl;
		return;
	}
	minL = n;
	int p1, p2;
	p1 = p2 = 0;
	while (p2 < n) {
		while (p2 >= p1 && ok(p1, p2)) p1++;
		p1--;
		minL = min(minL, p2 - p1 + 1);
		p2++;
	}
//	for (int i = 0; i < n; i++) {
//		for (int j = i; j < n; j++) {
//			if (ok(i, j)) {
//				minL = min(minL, j - i + 1);
//			}
//		}
//	}
	cout << minL << endl;
}

// reading
int main() {
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> s >> x >> y;

	//-----------------------------------------------

	solve();
}


#include <bits/stdc++.h>
using namespace std;
const int N=201000;
const int dx[4]={0,0,-1,1},dy[4]={1,-1,0,0};
//按题意设的,U,D,L,R
int n,ist[N],edx,edy,dffx,dffy,recx[N],recy[N],ans=n+1;
int jud(int len){
	for (int i=1;i<=n-len+1;++i){
		int x=recx[i-1],y=recy[i-1];
		int nx=recx[n]-recx[i+len-1],ny=recy[n]-recy[i+len-1];
		int dfx=nx+x,dfy=ny+y;
		//if (len==3) {printf("%d:%d %d\n",i,dfx,dfy);}
		if ((abs(edx-dfx)+abs(edy-dfy))<=len) return 1;
	}
	return 0;
}
void bsh(int l,int r){
	if (l==r) {ans=l;return;}
	if (l+1==r) {if (jud(l)) ans=l;else ans=r;return;}
	int mid=(l+r)>>1;
	if (jud(mid)) bsh(l,mid);else bsh(mid+1,r);return;
}
int main(){
	int i,j;
	scanf("%d",&n);
	i=0;char ch=getchar();
	while (ch<'A'||ch>'Z') ch=getchar();
	int x=0,y=0;
	while (ch>='A'&&ch<='Z') {
		if (ch=='U') ist[++i]=0,y++;if (ch=='D') ist[++i]=1,y--;
		if (ch=='L') ist[++i]=2,x--;if (ch=='R') ist[++i]=3,x++;
		recx[i]=x;recy[i]=y;
		ch=getchar();}
	scanf("%d%d",&edx,&edy);
	if ((edx==x)&&(edy==y)) {puts("0");return 0;}
	if ((abs(edx)+abs(edy))!=n) {puts("-1");return 0;}
	dffx=edx-x,dffy=edy-y;
	bsh(1,n);
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=201000;
int n,ist[N];
LL edx,edy,recx[N],recy[N],ans=N+1;
inline LL AB(LL x) {return (x>0)?x:-x;}
int jud(int len){
	for (int i=1;i<=n-len+1;++i){
		LL x=recx[i-1],y=recy[i-1];
		LL nx=recx[n]-recx[i+len-1],ny=recy[n]-recy[i+len-1];
		LL dfx=nx+x,dfy=ny+y;
		//if (len==3) {printf("%d:%lld %lld\n",i,dfx,dfy);}
		LL dffx=edx-dfx,dffy=edy-dfy;
		LL dff=(AB(dffx)+AB(dffy));
		if ((dff<=len)&&((dff%2)==(len%2))) return 1;
	}
	return 0;
}
void bsh(int l,int r){
	if (l==r) {ans=l;return;}
	if (l+1==r) {if (jud(l)) ans=l;else ans=r;return;}
	int mid=(l+r)>>1;
	if (jud(mid)) bsh(l,mid);else bsh(mid+1,r);return;
}
int main(){
	int i,j;
	scanf("%d",&n);
	i=0;char ch=getchar();
	while (ch<'A'||ch>'Z') ch=getchar();
	int x=0,y=0;recx[0]=0;recy[0]=0;
	while (ch>='A'&&ch<='Z') {
		if (ch=='U') ist[++i]=0,y++;if (ch=='D') ist[++i]=1,y--;
		if (ch=='L') ist[++i]=2,x--;if (ch=='R') ist[++i]=3,x++;
		recx[i]=x;recy[i]=y;
		ch=getchar();}
	scanf("%I64d%I64d",&edx,&edy);
	if ((edx==x)&&(edy==y)) {puts("0");return 0;}
	if (((AB(edx)+AB(edy))%2)!=(n%2)) {puts("-1");return 0;}
	bsh(1,n);
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=201000;
const int dx[4]={0,0,-1,1},dy[4]={1,-1,0,0};
//按题意设的,U,D,L,R
int n,ist[N],edx,edy,dffx,dffy,recx[N],recy[N],ans=n+1;
int jud(int len){
	for (int i=1;i<=n-len+1;++i){
		int x=recx[i-1],y=recy[i-1];
		int nx=recx[n]-recx[i+len-1],ny=recy[n]-recy[i+len-1];
		int dfx=nx+x,dfy=ny+y;
		//if (len==3) {printf("%d:%d %d\n",i,dfx,dfy);}
		int dff=(abs(edx-dfx)+abs(edy-dfy));
		if ((dff<=len)&&((dff%2)==(len%2))) return 1;
	}
	return 0;
}
void bsh(int l,int r){
	if (l==r) {ans=l;return;}
	if (l+1==r) {if (jud(l)) ans=l;else ans=r;return;}
	int mid=(l+r)>>1;
	if (jud(mid)) bsh(l,mid);else bsh(mid+1,r);return;
}
int main(){
	int i,j;
	scanf("%d",&n);
	i=0;char ch=getchar();
	while (ch<'A'||ch>'Z') ch=getchar();
	int x=0,y=0;recx[0]=0;recy[0]=0;
	while (ch>='A'&&ch<='Z') {
		if (ch=='U') ist[++i]=0,y++;if (ch=='D') ist[++i]=1,y--;
		if (ch=='L') ist[++i]=2,x--;if (ch=='R') ist[++i]=3,x++;
		recx[i]=x;recy[i]=y;
		ch=getchar();}
	scanf("%d%d",&edx,&edy);
	if ((edx==x)&&(edy==y)) {puts("0");return 0;}
	if (((abs(edx)+abs(edy))%2)!=(n%2)) {puts("-1");return 0;}
	dffx=edx-x,dffy=edy-y;
	bsh(1,n);
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=201000;
const int dx[4]={0,0,-1,1},dy[4]={1,-1,0,0};
//按题意设的,U,D,L,R
int n,ist[N],edx,edy,dffx,dffy,recx[N],recy[N],ans=n+1;
int jud(int len){
	for (int i=1;i<=n-len+1;++i){
		int x=recx[i-1],y=recy[i-1];
		int nx=recx[n]-recx[i+len-1],ny=recy[n]-recy[i+len-1];
		int dfx=nx+x,dfy=ny+y;
		//if (len==3) {printf("%d:%d %d\n",i,dfx,dfy);}
		if ((abs(edx-dfx)+abs(edy-dfy))<=len) return 1;
	}
	return 0;
}
void bsh(int l,int r){
	if (l==r) {ans=l;return;}
	if (l+1==r) {if (jud(l)) ans=l;else ans=r;return;}
	int mid=(l+r)>>1;
	if (jud(mid)) bsh(l,mid);else bsh(mid+1,r);return;
}
int main(){
	int i,j;
	scanf("%d",&n);
	i=0;char ch=getchar();
	while (ch<'A'||ch>'Z') ch=getchar();
	int x=0,y=0;
	while (ch>='A'&&ch<='Z') {
		if (ch=='U') ist[++i]=0,y++;if (ch=='D') ist[++i]=1,y--;
		if (ch=='L') ist[++i]=2,x--;if (ch=='R') ist[++i]=3,x++;
		recx[i]=x;recy[i]=y;
		ch=getchar();}
	scanf("%d%d",&edx,&edy);
	if ((edx==x)&&(edy==y)) {puts("0");return 0;}
	if ((abs(edx)+abs(edy))>n) {puts("-1");return 0;}
	dffx=edx-x,dffy=edy-y;
	bsh(1,n);
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=201000;
const int dx[4]={0,0,-1,1},dy[4]={1,-1,0,0};
//按题意设的,U,D,L,R
int n,ist[N],edx,edy,dffx,dffy,recx[N],recy[N],ans=n+1;
int jud(int len){
	for (int i=1;i<=n-len+1;++i){
		int x=recx[i-1],y=recy[i-1];
		int nx=recx[n]-recx[i+len-1],ny=recy[n]-recy[i+len-1];
		int dfx=nx+x,dfy=ny+y;
		//if (len==3) {printf("%d:%d %d\n",i,dfx,dfy);}
		if ((abs(edx-dfx)+abs(edy-dfy))<=len) return 1;
	}
	return 0;
}
void bsh(int l,int r){
	if (l==r) {ans=l;return;}
	if (l+1==r) {if (jud(l)) ans=l;else ans=r;return;}
	int mid=(l+r)>>1;
	if (jud(mid)) bsh(l,mid);else bsh(mid+1,r);return;
}
int main(){
	int i,j;
	scanf("%d",&n);
	i=0;char ch=getchar();
	while (ch<'A'||ch>'Z') ch=getchar();
	int x=0,y=0;
	while (ch>='A'&&ch<='Z') {
		if (ch=='U') ist[++i]=0,y++;if (ch=='D') ist[++i]=1,y--;
		if (ch=='L') ist[++i]=2,x--;if (ch=='R') ist[++i]=3,x++;
		recx[i]=x;recy[i]=y;
		ch=getchar();}
	scanf("%d%d",&edx,&edy);
	if ((edx==x)&&(edy==y)) {puts("0");return 0;}
	if (((abs(edx)+abs(edy))%2)!=(n%2)) {puts("-1");return 0;}
	dffx=edx-x,dffy=edy-y;
	bsh(1,n);
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=201000;
const int dx[4]={0,0,-1,1},dy[4]={1,-1,0,0};
//按题意设的,U,D,L,R
int n,ist[N],edx,edy,dffx,dffy,recx[N],recy[N],ans=n+1;
int jud(int len){
	for (int i=1;i<=n-len+1;++i){
		int x=recx[i-1],y=recy[i-1];
		int nx=recx[n]-recx[i+len-1],ny=recy[n]-recy[i+len-1];
		int dfx=nx+x,dfy=ny+y;
		//if (len==3) {printf("%d:%d %d\n",i,dfx,dfy);}
		int dff=(abs(edx-dfx)+abs(edy-dfy));
		if ((dff<=len)&&((dff%2)==(len%2))) return 1;
	}
	return 0;
}
void bsh(int l,int r){
	if (l==r) {ans=l;return;}
	if (l+1==r) {if (jud(l)) ans=l;else ans=r;return;}
	int mid=(l+r)>>1;
	if (jud(mid)) bsh(l,mid);else bsh(mid+1,r);return;
}
int main(){
	int i,j;
	scanf("%d",&n);
	i=0;char ch=getchar();
	while (ch<'A'||ch>'Z') ch=getchar();
	int x=0,y=0;recx[0]=0;recy[0]=0;
	while (ch>='A'&&ch<='Z') {
		if (ch=='U') ist[++i]=0,y++;if (ch=='D') ist[++i]=1,y--;
		if (ch=='L') ist[++i]=2,x--;if (ch=='R') ist[++i]=3,x++;
		recx[i]=x;recy[i]=y;
		ch=getchar();}
	scanf("%d%d",&edx,&edy);
	if ((edx==x)&&(edy==y)) {puts("0");return 0;}
	if ((((abs(edx)+abs(edy))%2)!=(n%2))||((abs(edx)+abs(edy))>n)) {puts("-1");return 0;}
	dffx=edx-x,dffy=edy-y;
	bsh(1,n);
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define LL long long int
using namespace std;
char s[200007];
int a[777][2];
int sum_x[200007],sum_y[200007];
int n,x,y;
bool check(int pos)
{
    for(int i=1;i+pos-1<=n;i++)
    {
        int j=i+pos-1;
        int xx=sum_x[i-1]+sum_x[n]-sum_x[j];
        int yy=sum_y[i-1]+sum_y[n]-sum_y[j];
        int d=abs(xx-x)+abs(yy-y);
        if(d<=pos&&(pos-d)%2==0)
        {
            return true;
        }
    }
    return false;
}
int main()
{
    a['U'][1]=1;
    a['D'][1]=-1;
    a['L'][0]=-1;
    a['R'][0]=1;
    cin>>n;
    scanf("%s",s);
    cin>>x>>y;
    if(strlen(s)<(int)abs(x)+(int)abs(y))
    {
        cout<<-1;
        return 0;
    }
    if(n==1&&(x==0&&y==0))
    {
        cout<<-1;
        return 0;
    }
    int xx=0,yy=0;
    for(int i=1;i<=n;i++)
    {
        sum_x[i]=sum_x[i-1]+a[s[i-1]][0];
        sum_y[i]=sum_y[i-1]+a[s[i-1]][1];
//        cout<<sum_x[i]<<" "<<sum_y[i]<<endl;
    }
//    cout<<xx<<" "<<yy<<endl;
    if(xx!=x||yy!=y)
    {
        int head=0,tail=n,cnt=-1;
        while(head<=tail)
        {
            int mid=(head+tail)/2;
            if(check(mid))
            {
                cnt=mid;
                tail=mid-1;
            }
            else
            {
                head=mid+1;
            }
        }
        cout<<cnt;
    }
    else
    {
        cout<<0;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long int
using namespace std;
char s[200007];
int a[777][2];
int main()
{
    a['U'][1]=1;
    a['D'][1]=-1;
    a['L'][0]=-1;
    a['R'][0]=1;
    int n;
    cin>>n;
    scanf("%s",s);
    int x,y;
    cin>>x>>y;
    if(strlen(s)<(int)abs(x)+(int)abs(y))
    {
        cout<<-1;
        return 0;
    }
    int xx=0,yy=0;
    for(int i=0;i<n;i++)
    {
        xx=xx+a[s[i]][0];
        yy=yy+a[s[i]][1];
    }
    if(xx!=x||yy!=y)
    {
        int cnt_y=abs(yy-y);
        int cnt_x=abs(xx-x);
        if(cnt_y%2==1||cnt_x%2==1)
        {
            cout<<-1;
            return 0;
        }
        int k=n/2;
        if(n%2==0)
        {
            k--;
        }
        cnt_x/=2;
        cnt_y/=2;
//        cout<<cnt_x<<" "<<cnt_y<<endl;
        int cnt_xx=0,cnt_yy=0;
        char c_x='D',c_y='L';
        if(yy-y>0)
        {
            c_y='U';
        }
        if(xx-x>0)
        {
            c_x='R';
        }
        int ans=0;
        int arr[200007];
        for(int i=k;i>=0;i--)
        {
            if(s[i]==c_y&&cnt_yy<cnt_y)
            {
//                cout<<s[i]<<endl;
                cnt_yy++;
                arr[ans++]=i+1;
            }
            else if(s[n-i-1]==c_y&&cnt_yy<cnt_y)
            {
//                cout<<s[n-i-1]<<endl;
                cnt_yy++;
                arr[ans++]=n-i;
            }
            if(s[i]==c_x&&cnt_xx<cnt_x)
            {
//                cout<<s[i]<<endl;
                cnt_xx++;
                arr[ans++]=i+1;
            }
            else if(s[n-i-1]==c_x&&cnt_xx<cnt_x)
            {
//                cout<<s[n-i-1]<<endl;
                cnt_xx++;
                arr[ans++]=n-i;
            }
        }
        if(cnt_xx!=cnt_x||cnt_yy!=cnt_y)
        {
            cout<<-1;
        }
        else
        {
//            for(int i=0;i<ans;i++)
//            {
//                cout<<arr[i]<<" ";
//            }
            sort(arr,arr+ans);
            cout<<arr[ans-1]-arr[0]+1;
        }
    }
    else
    {
        cout<<0;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long int
using namespace std;
char s[200007];
int a[777][2];
int sum_x[200007],sum_y[200007];
int n,x,y;
bool check(int pos)
{
    for(int i=1;i+pos-1<=n;i++)
    {
        int j=i+pos-1;
        int xx=sum_x[i-1]+sum_x[n]-sum_x[j];
        int yy=sum_y[i-1]+sum_y[n]-sum_y[j];
        int d=abs(xx-x)+abs(yy-y);
        if(d<=pos&&(pos-d)%2==0)
        {
            return true;
        }
    }
    return false;
}
int main()
{
    a['U'][1]=1;
    a['D'][1]=-1;
    a['L'][0]=-1;
    a['R'][0]=1;
    cin>>n;
    scanf("%s",s);
    cin>>x>>y;
    if(strlen(s)<(int)abs(x)+(int)abs(y))
    {
        cout<<-1;
        return 0;
    }
    if(n==1&&x==0&&y==0)
    {
        cout<<-1;
        return 0;
    }
    int xx=0,yy=0;
    for(int i=1;i<=n;i++)
    {
        sum_x[i]=sum_x[i-1]+a[s[i-1]][0];
        sum_y[i]=sum_y[i-1]+a[s[i-1]][1];
//        cout<<sum_x[i]<<" "<<sum_y[i]<<endl;
    }
//    cout<<xx<<" "<<yy<<endl;
    if(sum_x[n]!=x||sum_y[n]!=y)
    {
        int head=0,tail=n,cnt=-1;
        while(head<=tail)
        {
            int mid=(head+tail)/2;
            if(check(mid))
            {
                cnt=mid;
                tail=mid-1;
            }
            else
            {
                head=mid+1;
            }
        }
        cout<<cnt;
    }
    else
    {
        cout<<0;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long int
using namespace std;
char s[200007];
int a[777][2];
int sum_x[200007],sum_y[200007];
int n,x,y;
bool check(int pos)
{
    for(int i=1;i+pos-1<=n;i++)
    {
        int j=i+pos-1;
        int xx=sum_x[i-1]+sum_x[n]-sum_x[j];
        int yy=sum_y[i-1]+sum_y[n]-sum_y[j];
        int d=abs(xx-x)+abs(yy-y);
        if(d<=pos&&(pos-d)%2==0)
        {
            return true;
        }
    }
    return false;
}
int main()
{
    a['U'][1]=1;
    a['D'][1]=-1;
    a['L'][0]=-1;
    a['R'][0]=1;
    cin>>n;
    scanf("%s",s);
    cin>>x>>y;
    if(strlen(s)<(int)abs(x)+(int)abs(y))
    {
        cout<<-1;
        return 0;
    }
    if(n==1)
    {
        cout<<-1;
        return 0;
    }
    int xx=0,yy=0;
    for(int i=1;i<=n;i++)
    {
        sum_x[i]=sum_x[i-1]+a[s[i-1]][0];
        sum_y[i]=sum_y[i-1]+a[s[i-1]][1];
//        cout<<sum_x[i]<<" "<<sum_y[i]<<endl;
    }
//    cout<<xx<<" "<<yy<<endl;
    if(xx!=x||yy!=y)
    {
        int head=0,tail=n,cnt=-1;
        while(head<=tail)
        {
            int mid=(head+tail)/2;
            if(check(mid))
            {
                cnt=mid;
                tail=mid-1;
            }
            else
            {
                head=mid+1;
            }
        }
        cout<<cnt;
    }
    else
    {
        cout<<0;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long int
using namespace std;
char s[200007];
int a[777][2];
int sum_x[200007],sum_y[200007];
int n,x,y;
bool check(int pos)
{
    for(int i=1;i+pos-1<=n;i++)
    {
        int j=i+pos-1;
        int xx=sum_x[i-1]+sum_x[n]-sum_x[j];
        int yy=sum_y[i-1]+sum_y[n]-sum_y[j];
        int d=abs(xx-x)+abs(yy-y);
        if(d<=pos&&(pos-d)%2==0)
        {
            return true;
        }
    }
    return false;
}
int main()
{
    a['U'][1]=1;
    a['D'][1]=-1;
    a['L'][0]=-1;
    a['R'][0]=1;
    cin>>n;
    scanf("%s",s);
    cin>>x>>y;
    if(strlen(s)<(int)abs(x)+(int)abs(y))
    {
        cout<<-1;
        return 0;
    }
    int xx=0,yy=0;
    for(int i=1;i<=n;i++)
    {
        sum_x[i]=sum_x[i-1]+a[s[i-1]][0];
        sum_y[i]=sum_y[i-1]+a[s[i-1]][1];
//        cout<<sum_x[i]<<" "<<sum_y[i]<<endl;
    }
//    cout<<xx<<" "<<yy<<endl;
    if(xx!=x||yy!=y)
    {
        int head=0,tail=n,cnt=0;
        while(head<=tail)
        {
            int mid=(head+tail)/2;
            if(check(mid))
            {
                cnt=mid;
                tail=mid-1;
            }
            else
            {
                head=mid+1;
            }
        }
        cout<<cnt;
    }
    else
    {
        cout<<0;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long int
using namespace std;
char s[200007];
int a[777][2];
int sum_x[200007],sum_y[200007];
int n,x,y;
bool check(int pos)
{
    for(int i=1;i+pos-1<=n;i++)
    {
        int j=i+pos-1;
        int xx=sum_x[i-1]+sum_x[n]-sum_x[j];
        int yy=sum_y[i-1]+sum_y[n]-sum_y[j];
        int d=abs(xx-x)+abs(yy-y);
        if(d<=pos&&(pos-d)%2==0)
        {
            return true;
        }
    }
    return false;
}
int main()
{
    a['U'][1]=1;
    a['D'][1]=-1;
    a['L'][0]=-1;
    a['R'][0]=1;
    cin>>n;
    scanf("%s",s);
    cin>>x>>y;
    if(strlen(s)<(int)abs(x)+(int)abs(y))
    {
        cout<<-1;
        return 0;
    }
    int xx=0,yy=0;
    for(int i=1;i<=n;i++)
    {
        sum_x[i]=sum_x[i-1]+a[s[i-1]][0];
        sum_y[i]=sum_y[i-1]+a[s[i-1]][1];
//        cout<<sum_x[i]<<" "<<sum_y[i]<<endl;
    }
//    cout<<xx<<" "<<yy<<endl;
    if(xx!=x||yy!=y)
    {
        int head=0,tail=n,cnt=-1;
        while(head<=tail)
        {
            int mid=(head+tail)/2;
            if(check(mid))
            {
                cnt=mid;
                tail=mid-1;
            }
            else
            {
                head=mid+1;
            }
        }
        cout<<cnt;
    }
    else
    {
        cout<<0;
    }
    return 0;
}


// Devarshi

#include<bits/stdc++.h>
using namespace std;

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i,n) for(i=0;i<n;i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it,x) for(auto it:x)
#define PI 3.14159265
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef long double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn=1000001;

int n,x,y;
string s;
int prex[maxn],prey[maxn];

void init(){
	memset(prex,0,sizeof(prex));
	memset(prey,0,sizeof(prey));
}

bool check(int mid){
	fore(i,1,n){
		int remx=prex[n]-prex[i+mid-1]+prex[i-1];
		int remy=prey[n]-prey[i+mid-1]+prey[i-1];
		int reqx=abs(x-remx);
		int reqy=abs(y-remy);
		if(mid>=(reqx+reqy)&&(mid-reqx-reqy)%2==0)
			return true;
	}
	return false;
}

int bs(){
	int l=0,r=n;
	int ans=-1;
	while(l<=r){
		int mid=(l+r)/2;
		if(check(mid)){
			ans=mid;
			r=mid-1;
		}
		else{
			l=mid+1;
		}
	}
	return ans;
}

int main(){

sync

#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("out.txt", "w", stdout);
#endif

cin>>n>>s>>x>>y;

init();

forn(i,s.size()){
	prex[i+1]=prex[i];
	prey[i+1]=prey[i];
	if(s[i]=='R') prex[i+1]++;
	if(s[i]=='L') prex[i+1]--;
	if(s[i]=='U') prey[i+1]++;
	if(s[i]=='D') prey[i+1]--;
}
cout<<bs();

return 0;
}


// Devarshi

#include<bits/stdc++.h>
using namespace std;

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i,n) for(i=0;i<n;i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it,x) for(auto it:x)
#define PI 3.14159265
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef long double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn=1000001;

int n,x,y;
string s;
int prex[maxn],prey[maxn];

void init(){
	memset(prex,0,sizeof(prex));
	memset(prey,0,sizeof(prey));
}

bool check(int mid){
	fore(i,1,i+mid-1){
		int remx=prex[n]-prex[i+mid-1]+prex[i-1];
		int remy=prey[n]-prey[i+mid-1]+prey[i-1];
		int reqx=abs(x-remx);
		int reqy=abs(y-remy);
		if(mid>=(reqx+reqy)&&(mid-reqx-reqy)%2==0)
			return true;
	}
	return false;
}

int bs(){
	int l=0,r=n;
	int ans=-1;
	while(l<=r){
		int mid=(l+r)/2;
		if(check(mid)){
			ans=mid;
			r=mid-1;
		}
		else{
			l=mid+1;
		}
	}
	return ans;
}

int main(){

sync

#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("out.txt", "w", stdout);
#endif

cin>>n>>s>>x>>y;

init();

forn(i,s.size()){
	prex[i+1]=prex[i];
	prey[i+1]=prey[i];
	if(s[i]=='R') prex[i+1]++;
	if(s[i]=='L') prex[i+1]--;
	if(s[i]=='U') prey[i+1]++;
	if(s[i]=='D') prey[i+1]--;
}

cout<<bs();

return 0;
}


// Devarshi

#include<bits/stdc++.h>
using namespace std;

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i,n) for(i=0;i<n;i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it,x) for(auto it:x)
#define PI 3.14159265
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef long double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn=1000001;

int n,x,y;
string s;
int prex[maxn],prey[maxn];

void init(){
	memset(prex,0,sizeof(prex));
	memset(prey,0,sizeof(prey));
}

bool check(int mid){
	fore(i,1,n-mid+1){
		int remx=prex[n]-prex[i+mid-1]+prex[i-1];
		int remy=prey[n]-prey[i+mid-1]+prey[i-1];
		int reqx=abs(x-remx);
		int reqy=abs(y-remy);
		if(mid>=(reqx+reqy)&&(mid-reqx-reqy)%2==0)
			return true;
	}
	return false;
}

int bs(){
	int l=0,r=n;
	int ans=-1;
	while(l<=r){
		int mid=(l+r)/2;
		if(check(mid)){
			ans=mid;
			r=mid-1;
		}
		else{
			l=mid+1;
		}
	}
	return ans;
}

int main(){

sync

#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("out.txt", "w", stdout);
#endif

cin>>n>>s>>x>>y;

init();

forn(i,s.size()){
	prex[i+1]=prex[i];
	prey[i+1]=prey[i];
	if(s[i]=='R') prex[i+1]++;
	if(s[i]=='L') prex[i+1]--;
	if(s[i]=='U') prey[i+1]++;
	if(s[i]=='D') prey[i+1]--;
}

cout<<bs();

return 0;
}


// Devarshi

#include<bits/stdc++.h>
using namespace std;

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i,n) for(i=0;i<n;i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it,x) for(auto it:x)
#define PI 3.14159265
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef long double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn=1000001;

int n,x,y;
string s;
int prex[maxn],prey[maxn];

void init(){
	memset(prex,0,sizeof(prex));
	memset(prey,0,sizeof(prey));
}

bool check(int mid){
	fore(i,1,n){
		int remx=prex[n]-prex[i+mid-1]+prex[i-1];
		int remy=prey[n]-prey[i+mid-1]+prey[i-1];
		int reqx=abs(x-remx);
		int reqy=abs(y-remy);
		if(mid>=(reqx+reqy)&&(mid-reqx+reqy)%2==0)
			return true;
	}
	return false;
}

int bs(){
	int l=0,r=n;
	int ans=-1;
	while(l<=r){
		int mid=(l+r)/2;
		if(check(mid)){
			ans=mid;
			r=mid-1;
		}
		else{
			l=mid+1;
		}
	}
	return ans;
}

int main(){

sync

#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("out.txt", "w", stdout);
#endif

cin>>n>>s>>x>>y;

init();

forn(i,s.size()){
	prex[i+1]=prex[i];
	prey[i+1]=prey[i];
	if(s[i]=='R') prex[i+1]++;
	if(s[i]=='L') prex[i+1]--;
	if(s[i]=='U') prey[i+1]++;
	if(s[i]=='D') prey[i+1]--;
}
cout<<bs();

return 0;
}


// Devarshi

#include<bits/stdc++.h>
using namespace std;

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i,n) for(i=0;i<n;i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it,x) for(auto it:x)
#define PI 3.14159265
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef long double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn=1000001;

int n,x,y;
string s;
int prex[maxn],prey[maxn];

void init(){
	memset(prex,0,sizeof(prex));
	memset(prey,0,sizeof(prey));
}

bool check(int mid){
	fore(i,1,i+mid-1){
		int remx=prex[n]-prex[i+mid-1]+prex[i-1];
		int remy=prey[n]-prey[i+mid-1]+prey[i-1];
		int reqx=abs(x-remx);
		int reqy=abs(y-remy);
		if(mid>=(reqx+reqy)&&(mid-reqx-reqy)%2==0)
			return true;
	}
	return false;
}

int bs(){
	int l=0,r=n;
	int ans=-1;
	while(l<=r){
		int mid=(l+r)/2;
		if(check(mid)){
			ans=mid;
			r=mid-1;
		}
		else{
			l=mid+1;
		}
	}
	return ans;
}

int main(){

sync

#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("out.txt", "w", stdout);
#endif

cin>>n>>s>>x>>y;

init();

forn(i,s.size()){
	prex[i+1]=prex[i];
	prey[i+1]=prey[i];
	if(s[i]=='R') prex[i+1]++;
	if(s[i]=='L') prex[i+1]--;
	if(s[i]=='U') prey[i+1]++;
	if(s[i]=='D') prey[i+1]--;
}
forn(i,n+1){
	cout<<prex[i]<<" "<<prey[i]<<endl;
}
cout<<bs();

return 0;
}


#include <iostream>
#include <cstdio>
using namespace std;
char c[200001];
int sumx[200001],sumy[200001];
int abs(int x){return (x>0)?x:(-x);}
int n,x,y;
bool judge(int len){
	int sx,sy;
	for(int i=n;i-len>=0;--i){
		sx=sumx[n]-sumx[i]+sumx[i-len];
		sy=sumy[n]-sumy[i]+sumy[i-len];
		if(abs(x-sx)+abs(y-sy)<=len){
			return true;
		}
	}
	return false;
}
int main(){
	scanf("%d",&n);
	scanf("%s",c+1);
	scanf("%d%d",&x,&y);
	for(int i=1;i<=n;++i){
		sumx[i]=sumx[i-1];
		sumy[i]=sumy[i-1];
		switch(c[i]){
			case 'U':++sumy[i];break;
			case 'D':--sumy[i];break;
			case 'L':--sumx[i];break;
			case 'R':++sumx[i];break;
		}
	}
	if(sumx[n]==x && sumy[n]==y){
		puts("0");
		return 0;
	}if(abs(x)+abs(y)>n || ((abs(x)+abs(y))&1) != (n&1)){
		puts("-1");
		return 0;
	}
	int l=1,r=n+1,mid;
	while(l+1<r){
		mid=(l+r)/2;
		if(judge(mid))r=mid+1;
		else l=mid+1;
	}
	printf("%d\n",l);
	return 0;
}


#include <iostream>
#include <cstdio>
using namespace std;
int sumx[200001],sumy[200001];
int abs(int x){return (x>0)?x:(-x);}
int n,x,y;
bool judge(int len){
	int sx,sy;
	for(int i=n;i-len>=0;--i){
		sx=sumx[n]-sumx[i]+sumx[i-len];
		sy=sumy[n]-sumy[i]+sumy[i-len];
		if(abs(x-sx)+abs(y-sy)<=len){
			return true;
		}
	}
	return false;
}
int main(){
	scanf("%d",&n);getchar();
	for(int i=1;i<=n;++i){
		sumx[i]=sumx[i-1];
		sumy[i]=sumy[i-1];
		switch(getchar()){
			case 'U':++sumy[i];break;
			case 'D':--sumy[i];break;
			case 'L':--sumx[i];break;
			case 'R':++sumx[i];break;
		}
	}
	scanf("%d%d",&x,&y);
	if(sumx[n]==x && sumy[n]==y){
		puts("0");
		return 0;
	}if(abs(x)+abs(y)>n || ((abs(x)+abs(y))&1) != (n&1)){
		puts("-1");
		return 0;
	}
	int l=1,r=n+1,mid;
	while(l+1<r){
		mid=(l+r)/2;
		if(judge(mid))r=mid+1;
		else l=mid+1;
	}
	printf("%d\n",l);
	return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
typedef pair<int,int> p;

struct node {
    int k, r, c;
    node() { k=0, r=1, c=3; }
};

node **arr;
int n, m;

p sch(int x, int y) {
    int r=arr[x][y].r, c=arr[x][y].c;
    for(int k=r; ; k++, c+=2) {
        int t=0;
        for(int i=x-c/2; i<=x+c/2; i++) {
            if(abs(i-x)+t==k && i>=0 && i<n && y-t>=0 && y+t<m) {
                if(arr[i][y-t].k!=1) return p(i, y-t);
                if(arr[i][y+t].k!=1) return p(i, y+t);
            }
            if(i<x) t++;
            else t--;
        }
        arr[x][y].r++; arr[x][y].c+=2;
    }
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new node*[n];
    FOR(i, n) arr[i]=new node[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        if(arr[x-1][y-1].k!=1) arr[x-1][y-1].k=1, cout << x << " " << y << endl;
        else {
            p tmp=sch(x-1, y-1);
            arr[tmp.first][tmp.second].k=1;
            cout << (tmp.first+1) << " " << (tmp.second+1) << endl;
        }
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
#define pb push_back
#define f first
#define s second
typedef pair<int,int> p;
struct node {
    int k;
    node() { k=0;}
};
node **arr;
int n, m;

void alg(int i, int y, vector<p>& coll) {
    for(int j=y; j<m; j++)
        if(arr[i][j].k==0) {coll.pb(p(i, j)); break;}
    for(int j=y; j>=0; j--)
        if(arr[i][j].k==0) {coll.pb(p(i, j)); break;}
}
void sch(int x, int y, vector<p>& coll) {
    for(int i=x; i<n; i++) {
        alg(i, y, coll);
    }
    for(int i=x; i>=0; i--) {
        alg(i, y, coll);
    }
}
p res(int x, int y) {
    vector<p> coll;
    sch(x, y, coll); p c;

    vector<p> pos; int min=abs(coll[0].f-x)+abs(coll[0].s-y);
    FOR(i, coll.size()) {
        int tmp=abs(coll[i].f-x)+abs(coll[i].s-y);
        if(tmp<min) min=tmp;
    }
    FOR(i, coll.size()) if(abs(coll[i].f-x)+abs(coll[i].s-y)==min) pos.pb(coll[i]);

    vector<p> px; min=pos[0].f;
    FOR(i, pos.size()) if(pos[i].f<min) min=pos[i].f;
    FOR(i, pos.size()) if(pos[i].f==min) px.pb(pos[i]);

    min=px[0].s; c=px[0];
    FOR(i, px.size()) if(px[i].s<min) min=px[i].s, c=px[i];

    return c;
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new node*[n]; FOR(i, n) arr[i]=new node[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        if(arr[x-1][y-1].k!=1) arr[x-1][y-1].k=1, cout << x << " " << y << endl;
        else {
            p tmp=res(x-1, y-1);
            arr[tmp.f][tmp.s].k=1;
            cout << (tmp.f+1) << " " << (tmp.s+1) << endl;
        }
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
#define f first
#define s second
typedef pair<int,int> p;

int **arr;
int n, m;

p sch(int x, int y) {
    int c=3;
    for(int k=1; ; k++, c+=2) {
        for(int i=x-c; i<=x+c; i++)
            for(int j=y-c; j<=y+c; j++)
                if(abs(i-x)+abs(j-y)==k
                    && i>=0 && i<n && j>=0 && j<m) {
                    if(arr[i][j]!=1) return p(i, j);
                }
    }
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new int*[n];
    FOR(i, n) arr[i]=new int[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        if(arr[x-1][y-1]!=1) arr[x-1][y-1]=1, cout << x << " " << y << endl;
        else {
            p tmp=sch(x-1, y-1);
            arr[tmp.f][tmp.s]=1;
            cout << (tmp.f+1) << " " << (tmp.s+1) << endl;
        }
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
typedef pair<int,int> p;

struct node {
    int k, r, c, t;
    node() { k=0, r=1, c=3, t=0;}
};

node **arr;
int n, m;

p sch(int x, int y) {
    int r=arr[x][y].r, c=arr[x][y].c;
    for(int k=r; ; k++, c+=2) {
        int t=0;
        for(int i=x-c/2; i<=x+c/2; i++) {
            if(abs(i-x)+t==k && i>=0 && i<n) {
                if(y-t>=0) if(arr[i][y-t].k!=1) return p(i, y-t);
                if(y+t<m) if(arr[i][y+t].k!=1) return p(i, y+t);
            }
            if(i<x) t++;
            else t--;
        }
        //arr[x][y].r++; arr[x][y].c+=2;
    }
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new node*[n];
    FOR(i, n) arr[i]=new node[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        x--; y--;
        if(arr[x][y].k!=1) arr[x][y].k=1, cout << (x+1) << " " << (y+1) << endl;
        else {
            p tmp=sch(x, y);
            arr[tmp.first][tmp.second].k=1; arr[tmp.first][tmp.second].t++;
            if(arr[tmp.first][tmp.second].t==(arr[tmp.first][tmp.second].c-1)*2) {
                arr[tmp.first][tmp.second].t=0;
                arr[tmp.first][tmp.second].r++;
                arr[tmp.first][tmp.second].c+=2;
            }
            cout << (tmp.first+1) << " " << (tmp.second+1) << endl;
        }
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
#define pb push_back
#define f first
#define s second
typedef pair<int,int> p;

int **arr;
int n, m, Min=-1;

void alg(int i, int y, vector<p>& coll, int x) {
    for(int j=y; j<m; j++)
        if(arr[i][j]!=1) {
            coll.pb(p(i, j));
            if(abs(i-x)+abs(j-y)<Min || Min==-1)
                Min=abs(i-x)+abs(j-y);
            break;
        }
    for(int j=y; j>=0; j--)
        if(arr[i][j]!=1) {
            coll.pb(p(i, j));
            if(abs(i-x)+abs(j-y)<Min || Min==-1)
                Min=abs(i-x)+abs(j-y);
            break;
        }
}
void sch(int x, int y, vector<p>& coll) {
    for(int i=x; i<n; i++) {
        alg(i, y, coll, x);
        if(arr[i][y]!=1) break;
    }
    for(int i=x; i>=0; i--) {
        alg(i, y, coll, x);
        if(arr[i][y]!=1) break;
    }
}
p res(int x, int y) {
    vector<p> coll;
    sch(x, y, coll); p c;

    vector<p> pos; int min=Min;
    FOR(i, coll.size()) if(abs(coll[i].f-x)+abs(coll[i].s-y)==min) pos.pb(coll[i]);
    if(pos.size()==1) return pos[0];
    vector<p> px; min=pos[0].f;
    FOR(i, pos.size()) if(pos[i].f<min) min=pos[i].f;    // ??
    FOR(i, pos.size()) if(pos[i].f==min) px.pb(pos[i]);
    if(px.size()==1) return px[0];
    min=px[0].s; c=px[0];
    FOR(i, px.size()) if(px[i].s<min) min=px[i].s, c=px[i];
    Min=-1;
    return c;
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new int*[n];
    FOR(i, n) arr[i]=new int[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        if(arr[x-1][y-1]!=1) arr[x-1][y-1]=1, cout << x << " " << y << endl;
        else {
            p tmp=res(x-1, y-1);
            arr[tmp.f][tmp.s]=1;
            cout << (tmp.f+1) << " " << (tmp.s+1) << endl;
        }
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
#define pb push_back
#define f first
#define s second
typedef pair<int,int> p;
typedef enum {white, black} color;
struct node {
    int k;
    color c;
    node() { k=0; c=white;}
};
node arr[2000][2000];
int n, m;
vector<p> coll;

void sch(int x, int y) {
    if(x>=0 && x<n && y>=0 && y<m) {
        if(arr[x][y].c==white)
        {
            if(arr[x][y].k==0 && arr[x][y].c==white) {
                coll.pb(p(x,y));
                arr[x][y].c=black;
            }
            else arr[x][y].c=black, sch(x+1, y), sch(x, y+1), sch(x, y-1), sch(x-1, y);
        }
    }
}
p res(int x, int y) {
    sch(x, y); p c;

    vector<p> pos; int min=abs(coll[0].f-x)+abs(coll[0].s-y);
    FOR(i, coll.size()) {
        int tmp=abs(coll[i].f-x)+abs(coll[i].s-y);
        if(tmp<min) min=tmp;
    }
    FOR(i, coll.size()) if(abs(coll[i].f-x)+abs(coll[i].s-y)==min) pos.pb(coll[i]);

    vector<p> px; min=pos[0].f;
    FOR(i, pos.size()) if(pos[i].f<min) min=pos[i].f;
    FOR(i, pos.size()) if(pos[i].f==min) px.pb(pos[i]);

    min=px[0].s; c=px[0];
    FOR(i, px.size()) if(px[i].s<min) min=px[i].s, c=px[i];

    coll.erase(coll.begin(), coll.end());

    FOR(i, n) FOR(j, m) arr[i][j].c=white;

    return c;
}
int main()
{
    int k; cin >> n >> m >> k;
    FOR(i, k) {
        int x, y; cin >> x >> y;
        if(arr[x-1][y-1].k!=1) arr[x-1][y-1].k=1, cout << x << " " << y << endl;
        else {
            p tmp=res(x-1, y-1);
            arr[tmp.f][tmp.s].k=1;
            cout << (tmp.f+1) << " " << (tmp.s+1) << endl;
        }
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
typedef pair<int,int> p;

struct node {
    int k, r, c;
    node() { k=0, r=1, c=3;}
};

node **arr;
int n, m;

p sch(int x, int y) {
    int r=arr[x][y].r, c=arr[x][y].c;
    for(int k=r; ; k++, c+=2) {
        for(int i=x-c/2, t=0; i<=x+c/2; i++) {
            if(i>=0 && i<n) {
                if(y-t>=0) if(arr[i][y-t].k!=1) return p(i, y-t);
                if(y+t<m) if(arr[i][y+t].k!=1) return p(i, y+t);
            }
            if(i<x) t++;
            else t--;
        }
        arr[x][y].r++; arr[x][y].c+=2;
    }
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new node*[n];
    FOR(i, n) arr[i]=new node[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        x--; y--;
        if(arr[x][y].k!=1) arr[x][y].k=1, cout << (x+1) << " " << (y+1) << endl;
        else {
            p tmp=sch(x, y);
            arr[tmp.first][tmp.second].k=1;
            cout << (tmp.first+1) << " " << (tmp.second+1) << endl;
        }
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
#define pb push_back
#define f first
#define s second
typedef pair<int,int> p;

int **arr;
int n, m, Min=-1;

void alg(int i, int y, vector<p>& coll, int x) {
    for(int j=y+1; j<m; j++)
        if(arr[i][j]!=1) {
            coll.pb(p(i, j));
            if(abs(i-x)+abs(j-y)<Min || Min==-1)
                Min=abs(i-x)+abs(j-y);
            break;
        }
    for(int j=y; j>=0; j--)
        if(arr[i][j]!=1) {
            coll.pb(p(i, j));
            if(abs(i-x)+abs(j-y)<Min || Min==-1)
                Min=abs(i-x)+abs(j-y);
            break;
        }
}
void sch(int x, int y, vector<p>& coll) {
    for(int i=x; i<n; i++) {
        alg(i, y, coll, x);
        if(arr[i][y]!=1) break;
    }
    for(int i=x; i>=0; i--) {
        alg(i, y, coll, x);
        if(arr[i][y]!=1) break;
    }
}
p res(int x, int y) {
    vector<p> coll;
    sch(x, y, coll); p c;

    vector<p> pos; int min=Min;

    FOR(i, coll.size()) if(abs(coll[i].f-x)+abs(coll[i].s-y)==min) pos.pb(coll[i]);
    if(pos.size()==1) return pos[0];
    vector<p> px; min=pos[0].f;
    FOR(i, pos.size()) if(pos[i].f<min) min=pos[i].f;    // ??
    FOR(i, pos.size()) if(pos[i].f==min) px.pb(pos[i]);
    if(px.size()==1) return px[0];
    min=px[0].s; c=px[0];
    FOR(i, px.size()) if(px[i].s<min) min=px[i].s, c=px[i];
    Min=-1;
    return c;
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new int*[n];
    FOR(i, n) arr[i]=new int[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        if(arr[x-1][y-1]!=1) arr[x-1][y-1]=1, cout << x << " " << y << endl;
        else {
            p tmp=res(x-1, y-1);
            arr[tmp.f][tmp.s]=1;
            cout << (tmp.f+1) << " " << (tmp.s+1) << endl;
        }
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
typedef pair<int,int> p;

struct node {
    int k, r, c;
    node() { k=0, r=1, c=3;}
};

node **arr;
int n, m;

p sch(int x, int y) {
    int r=arr[x][y].r, c=arr[x][y].c;
    for(int k=r; ; k++, c+=2) {
        int t=0;
        for(int i=x-c/2; i<=x+c/2; i++) {
            if(abs(i-x)+t==k && i>=0 && i<n) {
                if(y-t>=0) if(arr[i][y-t].k!=1) return p(i, y-t);
                if(y+t<m) if(arr[i][y+t].k!=1) return p(i, y+t);
            }
            if(i<x) t++;
            else t--;
        }
        arr[x][y].r++; arr[x][y].c+=2;
    }
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new node*[n];
    FOR(i, n) arr[i]=new node[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        x--; y--;
        if(arr[x][y].k!=1) arr[x][y].k=1, cout << (x+1) << " " << (y+1) << endl;
        else {
            p tmp=sch(x, y);
            arr[tmp.first][tmp.second].k=1;
            cout << (tmp.first+1) << " " << (tmp.second+1) << endl;
        }
    }

    return 0;
}


#include <iostream>
using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
typedef pair<int,int> p;

struct node {
    int k, r;
    node() { k=0, r=1;}
};
node **arr;
int n, m;
#define at(arr, i, j) (i>=0 && i<n && j>=0 && j<m)?arr[i][j].r:-1
p sch(int x, int y) {
    int r=arr[x][y].r, c=r*2+1;
    r=max(r, at(arr, x-1, y));
    r=max(r, at(arr, x+1, y));
    r=max(r, at(arr, x, y-1));
    r=max(r, at(arr, x, y+1));
    r=max(r, at(arr, x-1, y-1));
    r=max(r, at(arr, x-1, y+1));
    r=max(r, at(arr, x+1, y-1));
    r=max(r, at(arr, x+1, y+1)); c=2*r+1;
    for(int k=r; ; k++, c+=2) {
        for(int i=x-c/2, t=0; i<=x+c/2; i++) {
            if(i>=n) break;
            if(i>=0 && i<n) {
                if(y-t>=0) if(arr[i][y-t].k!=1)return p(i, y-t);
                if(y+t<m) if(arr[i][y+t].k!=1) return p(i, y+t);
            }
            if(i<x) t++;
            else t--;

            if(i<0) continue;
        }
        arr[x][y].r=k;
    }
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new node*[n];
    FOR(i, n) arr[i]=new node[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        x--; y--;
        if(arr[x][y].k!=1) arr[x][y].k=1,
            cout << (x+1) << " " << (y+1) << endl;
        else {
            p tmp=sch(x, y);
            arr[tmp.first][tmp.second].k=1;
            cout << (tmp.first+1) << " " << (tmp.second+1) << endl;
        }
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
#define pb push_back
#define f first
#define s second
typedef pair<int,int> p;

int **arr;
int n, m, Min=-1;

void alg(int i, int y, vector<p>& coll, int x) {
    for(int j=y; j<m; j++)
        if(arr[i][j]!=1) {
            coll.pb(p(i, j));
            if(abs(i-x)+abs(j-y)<Min || Min==-1)
                Min=abs(i-x)+abs(j-y);
            break;
        }
    for(int j=y; j>=0; j--)
        if(arr[i][j]!=1) {
            coll.pb(p(i, j));
            if(abs(i-x)+abs(j-y)<Min || Min==-1)
                Min=abs(i-x)+abs(j-y);
            break;
        }
}
void sch(int x, int y, vector<p>& coll) {
    for(int i=x; i<n; i++) {
        alg(i, y, coll, x);
        if(arr[i][y]!=1) break;
    }
    for(int i=x; i>=0; i--) {
        alg(i, y, coll, x);
        if(arr[i][y]!=1) break;
    }
}
p res(int x, int y) {
    vector<p> coll;
    sch(x, y, coll); p c;

    vector<p> pos; int min=abs(coll[0].f-x)+abs(coll[0].s-y);
    FOR(i, coll.size()) if(abs(coll[i].f-x)+abs(coll[i].s-y)<min) min=abs(coll[i].f-x)+abs(coll[i].s-y);
    FOR(i, coll.size()) if(abs(coll[i].f-x)+abs(coll[i].s-y)==min) pos.pb(coll[i]);
    if(pos.size()==1) return pos[0];
    vector<p> px; min=pos[0].f;
    FOR(i, pos.size()) if(pos[i].f<min) min=pos[i].f;    // ??
    FOR(i, pos.size()) if(pos[i].f==min) px.pb(pos[i]);
    if(px.size()==1) return px[0];
    min=px[0].s; c=px[0];
    FOR(i, px.size()) if(px[i].s<min) min=px[i].s, c=px[i];
    Min=-1;
    return c;
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new int*[n];
    FOR(i, n) arr[i]=new int[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        if(arr[x-1][y-1]!=1) arr[x-1][y-1]=1, cout << x << " " << y << endl;
        else {
            p tmp=res(x-1, y-1);
            arr[tmp.f][tmp.s]=1;
            cout << (tmp.f+1) << " " << (tmp.s+1) << endl;
        }
    }
    return 0;
}


#include <iostream>
using namespace std;

#define FOR(i, n) for(int i=0; i<n; i++)
typedef pair<int,int> p;

struct node {
    int k, r, c;
    node() { k=0, r=1, c=3;}
};

node **arr;
int n, m;

p sch(int x, int y) {
    int r=arr[x][y].r, c=arr[x][y].c;
    for(int k=r; k<=max(x, n-x); k++, c+=2) {
        for(int i=x-c/2, t=0; i<=x+c/2; i++) {
            if(i>=0 && i<n) {
                if(y-t>=0) if(arr[i][y-t].k!=1) return p(i, y-t);
                if(y+t<m) if(arr[i][y+t].k!=1) return p(i, y+t);
            }
            if(i<x) t++;
            else t--;
        }
        arr[x][y].r++; arr[x][y].c+=2;
    }
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new node*[n];
    FOR(i, n) arr[i]=new node[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        x--; y--;
        if(arr[x][y].k!=1) arr[x][y].k=1, cout << (x+1) << " " << (y+1) << endl;
        else {
            p tmp=sch(x, y);
            arr[tmp.first][tmp.second].k=1;
            cout << (tmp.first+1) << " " << (tmp.second+1) << endl;
        }
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
typedef pair<int,int> p;

struct node {
    int k, r, c, t;
    node() { k=0, r=1, c=3, t=0;}
};

node **arr;
int n, m;

p sch(int x, int y) {
    int r=arr[x][y].r, c=arr[x][y].c;
    for(int k=r; ; k++, c+=2) {
        int t=0;
        for(int i=x-c/2; i<=x+c/2; i++) {
            if(abs(i-x)+t==k && i>=0 && i<n) {
                if(y-t>=0) if(arr[i][y-t].k!=1) return p(i, y-t);
                if(y+t<m) if(arr[i][y+t].k!=1) return p(i, y+t);
            }
            if(i<x) t++;
            else t--;
        }
        arr[x][y].r++; arr[x][y].c+=2;
    }
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new node*[n];
    FOR(i, n) arr[i]=new node[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        x--; y--;
        if(arr[x][y].k!=1) arr[x][y].k=1, cout << (x+1) << " " << (y+1) << endl;
        else {
            p tmp=sch(x, y);
            arr[tmp.first][tmp.second].k=1; arr[tmp.first][tmp.second].t++;
            if(arr[tmp.first][tmp.second].t==(arr[tmp.first][tmp.second].c-1)*2) {
                arr[tmp.first][tmp.second].t=0;
                arr[tmp.first][tmp.second].r++;
                arr[tmp.first][tmp.second].c+=2;
            }
            cout << (tmp.first+1) << " " << (tmp.second+1) << endl;
        }
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
typedef pair<int,int> p;

struct node {
    int k;
    node() { k=0;}
};

node **arr;
int n, m;
p sch(int x, int y) {
    for(int k=1, c=3; ; k++, c+=2) {
        if(x+c/2<m) if(arr[x+c/2][y].k==1) continue;
        int t=0;
        for(int i=x-c/2; i<=x+c/2; i++) {
            if(abs(i-x)+t==k && i>=0 && i<n) {
                if(y-t>=0) if(arr[i][y-t].k!=1) return p(i, y-t);
                if(y+t<m) if(arr[i][y+t].k!=1) return p(i, y+t);
            }
            if(i<x) t++;
            else t--;
        }
    }
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new node*[n];
    FOR(i, n) arr[i]=new node[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        if(arr[x-1][y-1].k!=1) arr[x-1][y-1].k=1, cout << x << " " << y << endl;
        else {
            p tmp=sch(x-1, y-1);
            arr[tmp.first][tmp.second].k=1;
            cout << (tmp.first+1) << " " << (tmp.second+1) << endl;
        }
    }
    return 0;
}


#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
typedef pair<int, int> p;

int d[8][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0},
                {-1, -1}, {1, -1}, {1, 1}, {-1, 1}};
int n, m, k;
int arr[2002][2002];
bool use[2002][2002];

p check(int x, int y, int z) {
    for(; ; z++)
        for(int i=-z; i<=z; ++i) {
            if(x+i<1) continue;
            if(x+i>n) break;
            int tx=x+i, j=z-abs(i);
            if(y-j>0 && !use[tx][y-j]) {
                use[tx][y-j]=true;
                arr[x][y]=z;
                return p(tx, y-j);
            }
            if(y+j<=m && !use[tx][y+j]) {
                use[tx][y+j]=true;
                arr[x][y]=z;
                return p(tx, y+j);
            }
        }
}
int main()
{
    cin >> n >> m >> k;
    for(int i=1; i<=k; i++) {
        int x, y; cin >> x >> y;
        int j=arr[x][y];
        FOR(h, 8) j=max(j, arr[x+d[h][0]][y+d[h][1]]-((h<4)?1:2));
        p tmp=check(x, y, j);
        cout << tmp.first << " " << tmp.second << endl;
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
#define pb push_back
#define f first
#define s second
typedef pair<int,int> p;

int **arr;
int n, m;

void alg(int i, int y, vector<p>& coll) {
    for(int j=y; j<m; j++)
        if(arr[i][j]!=1) {coll.pb(p(i, j)); break;}
    for(int j=y; j>=0; j--)
        if(arr[i][j]!=1) {coll.pb(p(i, j)); break;}
}
void sch(int x, int y, vector<p>& coll) {
    for(int i=x; i<n; i++) {
        alg(i, y, coll);
        if(arr[i][y]!=1) break;
    }
    for(int i=x; i>=0; i--) {
        alg(i, y, coll);
        if(arr[i][y]!=1) break;
    }
}
p res(int x, int y) {
    vector<p> coll;
    sch(x, y, coll); p c;

    vector<p> pos; int min=abs(coll[0].f-x)+abs(coll[0].s-y);
    FOR(i, coll.size()) {
        int tmp=abs(coll[i].f-x)+abs(coll[i].s-y);
        if(tmp<min) min=tmp;
    }
    FOR(i, coll.size()) if(abs(coll[i].f-x)+abs(coll[i].s-y)==min) pos.pb(coll[i]);

    vector<p> px; min=pos[0].f;
    FOR(i, pos.size()) if(pos[i].f<min) min=pos[i].f;
    FOR(i, pos.size()) if(pos[i].f==min) px.pb(pos[i]);

    min=px[0].s; c=px[0];
    FOR(i, px.size()) if(px[i].s<min) min=px[i].s, c=px[i];

    return c;
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new int*[n];
    FOR(i, n) arr[i]=new int[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        if(arr[x-1][y-1]!=1) arr[x-1][y-1]=1, cout << x << " " << y << endl;
        else {
            p tmp=res(x-1, y-1);
            arr[tmp.f][tmp.s]=1;
            cout << (tmp.f+1) << " " << (tmp.s+1) << endl;
        }
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
#define pb push_back
#define f first
#define s second
typedef pair<int,int> p;
typedef enum {white, black} color;
struct node {
    int k;
    color c;
    node() { k=0; c=white;}
};
node **arr;
int n, m;

void sch(int x, int y, vector<p>& coll, vector<p>& temp) {
    if(x>=0 && x<n && y>=0 && y<m) {
        if(arr[x][y].c==white)
        {
            temp.pb(p(x,y));
            if(arr[x][y].k==0 && arr[x][y].c==white) {
                coll.pb(p(x,y));
                arr[x][y].c=black;
            }
            else arr[x][y].c=black,
                sch(x+1, y, coll, temp), sch(x, y+1, coll, temp), sch(x, y-1, coll, temp), sch(x-1, y, coll, temp);
        }
    }
}
p res(int x, int y) {
    vector<p> coll, temp;
    sch(x, y, coll, temp); p c;

    vector<p> pos; int min=abs(coll[0].f-x)+abs(coll[0].s-y);
    FOR(i, coll.size()) {
        int tmp=abs(coll[i].f-x)+abs(coll[i].s-y);
        if(tmp<min) min=tmp;
    }
    FOR(i, coll.size()) if(abs(coll[i].f-x)+abs(coll[i].s-y)==min) pos.pb(coll[i]);

    vector<p> px; min=pos[0].f;
    FOR(i, pos.size()) if(pos[i].f<min) min=pos[i].f;
    FOR(i, pos.size()) if(pos[i].f==min) px.pb(pos[i]);

    min=px[0].s; c=px[0];
    FOR(i, px.size()) if(px[i].s<min) min=px[i].s, c=px[i];

    FOR(i, temp.size()) arr[temp[i].f][temp[i].s].c=white;

    return c;
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new node*[n]; FOR(i, n) arr[i]=new node[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        if(arr[x-1][y-1].k!=1) arr[x-1][y-1].k=1, cout << x << " " << y << endl;
        else {
            p tmp=res(x-1, y-1);
            arr[tmp.f][tmp.s].k=1;
            cout << (tmp.f+1) << " " << (tmp.s+1) << endl;
        }
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
#define pb push_back
#define f first
#define s second
typedef pair<int,int> p;
typedef enum {white, black} color;
struct node {
    int k;
    color c;
    node() { k=0; c=white;}
};
node **arr;
int n, m;

void sch(int x, int y, vector<p>& coll) {
    if(x>=0 && x<n && y>=0 && y<m) {
        if(arr[x][y].c==white)
        {
            if(arr[x][y].k==0 && arr[x][y].c==white) {
                coll.pb(p(x,y));
                arr[x][y].c=black;
            }
            else arr[x][y].c=black,
                sch(x+1, y, coll), sch(x, y+1, coll), sch(x, y-1, coll), sch(x-1, y, coll);
        }
    }
}
p res(int x, int y) {
    vector<p> coll;
    sch(x, y, coll); p c;

    vector<p> pos; int min=abs(coll[0].f-x)+abs(coll[0].s-y);
    FOR(i, coll.size()) {
        int tmp=abs(coll[i].f-x)+abs(coll[i].s-y);
        if(tmp<min) min=tmp;
    }
    FOR(i, coll.size()) if(abs(coll[i].f-x)+abs(coll[i].s-y)==min) pos.pb(coll[i]);

    vector<p> px; min=pos[0].f;
    FOR(i, pos.size()) if(pos[i].f<min) min=pos[i].f;
    FOR(i, pos.size()) if(pos[i].f==min) px.pb(pos[i]);

    min=px[0].s; c=px[0];
    FOR(i, px.size()) if(px[i].s<min) min=px[i].s, c=px[i];

    FOR(i, n) FOR(j, m) arr[i][j].c=white;

    return c;
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new node*[n]; FOR(i, n) arr[i]=new node[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        if(arr[x-1][y-1].k!=1) arr[x-1][y-1].k=1, cout << x << " " << y << endl;
        else {
            p tmp=res(x-1, y-1);
            arr[tmp.f][tmp.s].k=1;
            cout << (tmp.f+1) << " " << (tmp.s+1) << endl;
        }
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
#define pb push_back
#define f first
#define s second
typedef pair<int,int> p;
typedef enum {white, black} color;
struct node {
    int k;
    color c;
    node() { k=0; c=white;}
};
node **arr;
int n, m;

/*void sch(int x, int y, vector<p>& coll) {
    if(x>=0 && x<n && y>=0 && y<m) {
        if(arr[x][y].c==white)
        {
            if(arr[x][y].k==0 && arr[x][y].c==white) {
                coll.pb(p(x,y));
                arr[x][y].c=black;
            }
            else arr[x][y].c=black,
                sch(x+1, y, coll), sch(x, y+1, coll), sch(x, y-1, coll), sch(x-1, y, coll);
        }
    }
}*/
void alg(int i, int y, vector<p>& coll) {
    for(int j=y; j<m; j++)
        if(arr[i][j].k==0) {coll.pb(p(i, j)); break;}
    for(int j=y; j>=0; j--)
        if(arr[i][j].k==0) {coll.pb(p(i, j)); break;}
}
void sch(int x, int y, vector<p>& coll) {
    for(int i=x; i<n; i++) {
        alg(i, y, coll);
    }
    for(int i=x; i>=0; i--) {
        alg(i, y, coll);
    }
}
p res(int x, int y) {
    vector<p> coll;
    sch(x, y, coll); p c;

    vector<p> pos; int min=abs(coll[0].f-x)+abs(coll[0].s-y);
    FOR(i, coll.size()) {
        int tmp=abs(coll[i].f-x)+abs(coll[i].s-y);
        if(tmp<min) min=tmp;
    }
    FOR(i, coll.size()) if(abs(coll[i].f-x)+abs(coll[i].s-y)==min) pos.pb(coll[i]);

    vector<p> px; min=pos[0].f;
    FOR(i, pos.size()) if(pos[i].f<min) min=pos[i].f;
    FOR(i, pos.size()) if(pos[i].f==min) px.pb(pos[i]);

    min=px[0].s; c=px[0];
    FOR(i, px.size()) if(px[i].s<min) min=px[i].s, c=px[i];

    //FOR(i, n) FOR(j, m) arr[i][j].c=white;

    return c;
}
int main()
{
    freopen("test.txt", "r", stdin);
    int k; cin >> n >> m >> k;
    arr=new node*[n]; FOR(i, n) arr[i]=new node[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        if(arr[x-1][y-1].k!=1) arr[x-1][y-1].k=1, cout << x << " " << y << endl;
        else {
            p tmp=res(x-1, y-1);
            arr[tmp.f][tmp.s].k=1;
            cout << (tmp.f+1) << " " << (tmp.s+1) << endl;
        }
    }

    return 0;
}


#include <iostream>
using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
typedef pair<int,int> p;

struct node {
    int k, r;
    node() { k=0, r=1;}
};
node **arr;
int n, m;
p sch(int x, int y) {
    int r=arr[x][y].r, c=arr[x][y].r*2+1;
    for(int k=r; ; k++, c+=2) {
        for(int i=x-c/2, t=0; i<=x+c/2; i++) {
            if(i>=n) break;
            if(i>=0 && i<n) {
                if(y-t>=0) if(arr[i][y-t].k!=1)return p(i, y-t);
                if(y+t<m) if(arr[i][y+t].k!=1) return p(i, y+t);
            }
            if(i<x) t++;
            else t--;

            if(i<0) continue;
        }
        arr[x][y].r=k;
    }
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new node*[n];
    FOR(i, n) arr[i]=new node[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        x--; y--;
        if(arr[x][y].k!=1) arr[x][y].k=1,
            cout << (x+1) << " " << (y+1) << endl;
        else {
            p tmp=sch(x, y);
            arr[tmp.first][tmp.second].k=1;
            cout << (tmp.first+1) << " " << (tmp.second+1) << endl;
        }
    }
    return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
#define FOR(i, n) for(int i=0; i<n; i++)
typedef pair<int,int> p;

struct node {
    int k, r, c;
    node() { k=0, r=1, c=3; }
};

node **arr;
int n, m;

p sch(int x, int y) {
    int r=arr[x][y].r, c=arr[x][y].c;
    for(int k=r; ; k++, c+=2) {
        int t=0;
        for(int i=x-c/2; i<=x+c/2; i++) {
            if(abs(i-x)+t==k && i>=0 && i<n) {
                if(y-t>=0) if(arr[i][y-t].k!=1) return p(i, y-t);
                if(y+t<m) if(arr[i][y+t].k!=1) return p(i, y+t);
            }
            if(i<x) t++;
            else t--;
        }
        arr[x][y].r++; arr[x][y].c+=2;
    }
}
int main()
{
    int k; cin >> n >> m >> k;
    arr=new node*[n];
    FOR(i, n) arr[i]=new node[m];
    FOR(i, k) {
        int x, y; cin >> x >> y;
        if(arr[x-1][y-1].k!=1) arr[x-1][y-1].k=1, cout << x << " " << y << endl;
        else {
            p tmp=sch(x-1, y-1);
            arr[tmp.first][tmp.second].k=1;
            cout << (tmp.first+1) << " " << (tmp.second+1) << endl;
        }
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <math.h>
int dis(int x1,int y1,int x2,int y2)
{
    return abs(x1-x2)+abs(y1-y2);
}
int set[2005][2005];
struct near
{
    int dis;
    int last;
}sit[2005][2005];
struct guanzhong
{
    int x;
    int y;
}gz[100005];
int main()
{
  int n,i,j,k,m,k1;
      /*freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);*/
  while (scanf("%d%d%d",&n,&m,&k)!=EOF)
  {

     memset(set,0,sizeof(set));
     memset(sit,0,sizeof(sit));
     for(i=1;i<=k;i++)
     {
         scanf("%d %d",&gz[i].x,&gz[i].y);
     }
     for(k1=1;k1<=k;k1++)
     {
         if(!set[gz[k1].x][gz[k1].y])
         {
             printf("%d %d\n",gz[k1].x,gz[k1].y);
             sit[gz[k1].x][gz[k1].y].dis=1;
             set[gz[k1].x][gz[k1].y]=1;
         }
         else
         {
              int xx,yy,tag,tag1;
              tag1=0;
              int dist1;
loop:    dist1=sit[gz[k1].x][gz[k1].y].dis;
              if(sit[gz[k1].x][gz[k1].y].dis==1)
                  i=dist1;
              else
                  i=sit[gz[k1].x][gz[k1].y].last;
            tag=0;
             for(i=dist1;i>=-dist1;i--)
             {
                 xx=gz[k1].x-i;
                 yy=gz[k1].y-(dist1-abs(i));
                 if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                 {
                     if(!set[xx][yy])
                     {
                         tag=1;
                         break;
                     }
                 }
                  xx=gz[k1].x-i;
                 yy=gz[k1].y+(dist1-abs(i));
                 if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                 {
                     if(!set[xx][yy])
                     {
                         tag=1;
                         break;
                     }
                 }
             }
             if(!tag)
             {
                 sit[gz[k1].x][gz[k1].y].dis++;
                     tag1=1;
                 goto loop;
             }
             set[xx][yy]=1;
             sit[gz[k1].x][gz[k1].y].last=i;
             printf("%d %d\n",xx,yy);
             if(tag1&&(sit[gz[k1].x][gz[k1].y].dis%30==0))
             {
             for(i=k1+1;i<=k;i++)
             {
                 int dist2=dis(gz[i].x,gz[i].y,gz[k1].x,gz[k1].y);
                 if(dist2< sit[gz[k1].x][gz[k1].y].dis)
                 {
                     int tmp=sit[gz[k1].x][gz[k1].y].dis-dist2;
                     if(tmp>sit[gz[i].x][gz[i].y].dis)
                         sit[gz[i].x][gz[i].y].dis=tmp;
                 }
             }
             }
         }
     }
  }
  return 0;
}


#include <stdio.h>
#include <string.h>
#include <math.h>
int dis(int x1,int y1,int x2,int y2)
{
    return abs(x1-x2)+abs(y1-y2);
}
int set[2005][2005];
struct near
{
    int dis;
    int xx;
}sit[2005][2005];
struct guanzhong
{
    int x;
    int y;
}gz[100005];
int main()
{
  int n,i,j,k,m,k1;
     /*freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);*/
  while (scanf("%d%d%d",&n,&m,&k)!=EOF)
  {

     memset(set,0,sizeof(set));
     memset(sit,0,sizeof(sit));
     for(i=1;i<=k;i++)
     {
         scanf("%d %d",&gz[i].x,&gz[i].y);
     }
     for(k1=1;k1<=k;k1++)
     {
         if(!set[gz[k1].x][gz[k1].y])
         {
             printf("%d %d\n",gz[k1].x,gz[k1].y);
             sit[gz[k1].x][gz[k1].y].dis=1;
             set[gz[k1].x][gz[k1].y]=1;
         }
         else
         {
              int xx,yy,tag,tag1;
              tag1=0;
              int dist1;
loop:    dist1=sit[gz[k1].x][gz[k1].y].dis;
            tag=0;
            if(sit[gz[k1].x][gz[k1].y].xx<=0)
                i=dist1;
            else
                i=sit[gz[k1].x][gz[k1].y].xx;
             for(;i>=-dist1;i--)
             {
                 xx=gz[k1].x-i;
                 yy=gz[k1].y-(dist1-abs(i));
                 if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                 {
                     if(!set[xx][yy])
                     {
                         tag=1;
                         break;
                     }
                 }
                  xx=gz[k1].x-i;
                 yy=gz[k1].y+(dist1-abs(i));
                 if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                 {
                     if(!set[xx][yy])
                     {
                         tag=1;
                         break;
                     }
                 }
             }
             if(!tag)
             {
                 sit[gz[k1].x][gz[k1].y].dis++;
                     tag1=1;
                 goto loop;
             }
             set[xx][yy]=1;
            sit[gz[k1].x][gz[k1].y].xx=i;
             printf("%d %d\n",xx,yy);
             if(tag1)
             {
             for(i=k1+1;i<=k;i++)
             {
                 int dist2=dis(gz[i].x,gz[i].y,gz[k1].x,gz[k1].y);
                 if(dist2< sit[gz[k1].x][gz[k1].y].dis)
                 {
                     int tmp=sit[gz[k1].x][gz[k1].y].dis-dist2;
                     if(tmp>sit[gz[i].x][gz[i].y].dis)
                         sit[gz[i].x][gz[i].y].dis=tmp;
                 }
             }
             }
         }
     }
  }
  return 0;
}


#include <stdio.h>
#include <string.h>
#include <math.h>
int dis(int x1,int y1,int x2,int y2)
{
    return abs(x1-x2)+abs(y1-y2);
}
int set[2005][2005];
struct near
{
    int dis;
    int last;
}sit[2005][2005];
struct guanzhong
{
    int x;
    int y;
}gz[100005];
int main()
{
  int n,i,j,k,m,k1;
      /*freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);*/
  while (scanf("%d%d%d",&n,&m,&k)!=EOF)
  {

     memset(set,0,sizeof(set));
     memset(sit,0,sizeof(sit));
     for(i=1;i<=k;i++)
     {
         scanf("%d %d",&gz[i].x,&gz[i].y);
     }
     for(k1=1;k1<=k;k1++)
     {
         if(!set[gz[k1].x][gz[k1].y])
         {
             printf("%d %d\n",gz[k1].x,gz[k1].y);
             sit[gz[k1].x][gz[k1].y].dis=1;
             set[gz[k1].x][gz[k1].y]=1;
         }
         else
         {
              int xx,yy,tag,tag1;
              tag1=0;
              int dist1;
loop:    dist1=sit[gz[k1].x][gz[k1].y].dis;
              if(sit[gz[k1].x][gz[k1].y].dis==1)
                  i=dist1;
              else
                  i=sit[gz[k1].x][gz[k1].y].last;
            tag=0;
             for(i=dist1;i>=-dist1;i--)
             {
                 xx=gz[k1].x-i;
                 yy=gz[k1].y-(dist1-abs(i));
                 if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                 {
                     if(!set[xx][yy])
                     {
                         tag=1;
                         break;
                     }
                 }
                  xx=gz[k1].x-i;
                 yy=gz[k1].y+(dist1-abs(i));
                 if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                 {
                     if(!set[xx][yy])
                     {
                         tag=1;
                         break;
                     }
                 }
             }
             if(!tag)
             {
                 sit[gz[k1].x][gz[k1].y].dis++;
                     tag1=1;
                 goto loop;
             }
             set[xx][yy]=1;
             sit[gz[k1].x][gz[k1].y].last=i;
             printf("%d %d\n",xx,yy);
             if(tag1)
             {
             for(i=k1+1;i<=k;i++)
             {
                 int dist2=dis(gz[i].x,gz[i].y,gz[k1].x,gz[k1].y);
                 if(dist2< sit[gz[k1].x][gz[k1].y].dis)
                 {
                     int tmp=sit[gz[k1].x][gz[k1].y].dis-dist2;
                     if(tmp>sit[gz[i].x][gz[i].y].dis)
                         sit[gz[i].x][gz[i].y].dis=tmp;
                 }
             }
             }
         }
     }
  }
  return 0;
}


#include <stdio.h>
#include <string.h>
#include <math.h>
int dis(int x1,int y1,int x2,int y2)
{
    return abs(x1-x2)+abs(y1-y2);
}
int set[2005][2005];
struct near
{
    int dis;
    int x;
    int y;
}sit[2005][2005];
struct guanzhong
{
    int x;
    int y;
}gz[100005];
int main()
{
  int n,i,j,k,m,k1;
     /* freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);*/
  while (scanf("%d%d%d",&n,&m,&k)!=EOF)
  {

     memset(set,0,sizeof(set));
     memset(sit,0,sizeof(sit));
     for(i=1;i<=k;i++)
     {
         scanf("%d %d",&gz[i].x,&gz[i].y);
     }
     for(k1=1;k1<=k;k1++)
     {
         if(!set[gz[k1].x][gz[k1].y])
         {
             printf("%d %d\n",gz[k1].x,gz[k1].y);
             sit[gz[k1].x][gz[k1].y].dis=1;
             set[gz[k1].x][gz[k1].y]=1;
         }
         else
         {
              int xx,yy,tag,tag1;
              tag1=0;
              int dist1;
loop:    dist1=sit[gz[k1].x][gz[k1].y].dis;
            tag=0;
             for(i=dist1;i>=-dist1;i--)
             {
                 xx=gz[k1].x-i;
                 yy=gz[k1].y-(dist1-abs(i));
                 if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                 {
                     if(!set[xx][yy])
                     {
                         tag=1;
                         break;
                     }
                 }
                  xx=gz[k1].x-i;
                 yy=gz[k1].y+(dist1-abs(i));
                 if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                 {
                     if(!set[xx][yy])
                     {
                         tag=1;
                         break;
                     }
                 }
             }
             if(!tag)
             {
                 sit[gz[k1].x][gz[k1].y].dis++;
                     tag1=1;
                 goto loop;
             }
             set[xx][yy]=1;
             printf("%d %d\n",xx,yy);
             if(tag1)
             {
             for(i=k1+1;i<=k;i++)
             {
                 int dist2=dis(gz[i].x,gz[i].y,gz[k1].x,gz[k1].y);
                 if(dist2< sit[gz[k1].x][gz[k1].y].dis)
                 {
                     int tmp=sit[gz[k1].x][gz[k1].y].dis-dist2;
                     if(tmp>sit[gz[i].x][gz[i].y].dis)
                         sit[gz[i].x][gz[i].y].dis=tmp;
                 }
             }
             }
         }
     }
  }
  return 0;
}