#include <cstdio>
#include <algorithm>
#include <cstdlib>
int a[1009],sz[10],l[4],r[4],n,m;
bool check()
{
	for (int i = 1; i <= n; ++i)
		if (a[i] != i) return false;
	return true;
}
void dfs(int step)
{
	if (check()) {
		printf("%d\n",step - 1);
		for (int i = 1; i < step; ++i) printf("%d %d\n",l[i],r[i]);
		exit(0);
	}
	if (step > 3) return;
	for (int i = 1,L = 1,R; i <= m; L += sz[i++]) {
		R = L + sz[i] - 1;
		for (int j = i; j <= m; R += sz[++j]) {
			if (L == R) continue;
			std::reverse(a + L,a + R + 1);
			l[step] = L; r[step] = R;
			dfs(step + 1);
			std::reverse(a + L,a + R + 1);
		}
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("339E.in","r",stdin);
	freopen("339E.out","w",stdout);
	#endif
	scanf("%d",&n);
	for (int i = 1; i <= n; ++i) scanf("%d",&a[i]);
	for (int i = 1,t = 0; i <= n; ++i) {
		if (abs(t) != 1 || a[i] != a[i - 1] + t) ++m,t = a[i + 1] - a[i];
		++sz[m];
	}
	dfs(1);
}


#include <cstdio>
#include <algorithm>
#include <cstdlib>
int a[1009],sz[10],l[4],r[4],n,m;
bool check()
{
	for (int i = 1; i <= n; ++i)
		if (a[i] != i) return false;
	return true;
}
void dfs(int step)
{
	if (check()) {
		printf("%d\n",step - 1);
		for (int i = 1; i < step; ++i) printf("%d %d\n",l[i],r[i]);
		exit(0);
	}
	if (step > 3) return;
	for (int i = 1,L = 1,R; i <= m; L += sz[i++]) {
		R = L + sz[i] - 1;
		for (int j = i; j <= m; R += sz[++j]) {
			if (L == R) continue;
			std::reverse(a + L,a + R + 1);
			l[step] = L; r[step] = R;
			dfs(step + 1);
			std::reverse(a + L,a + R + 1);
		}
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("339E.in","r",stdin);
	freopen("339E.out","w",stdout);
	#endif
	scanf("%d",&n);
	a[0] = -1;
	for (int i = 1; i <= n; ++i) {
		scanf("%d",&a[i]);
		if (a[i] != a[i - 1] + 1) ++m;
		++sz[m];
	}
	dfs(1);
}


#include <cstdio>
#include <algorithm>
#include <cstdlib>
int a[1009],sz[10],l[4],r[4],n,m;
bool check()
{
	for (int i = 1; i <= n; ++i)
		if (a[i] != i) return false;
	return true;
}
void dfs(int step)
{
	if (check()) {
		printf("%d\n",step - 1);
		for (int i = 1; i < step; ++i) printf("%d %d\n",l[step - i],r[step - i]);
		exit(0);
	}
	if (step > 3) return;
	for (int i = 1,L = 1,R; i <= m; L += sz[i++]) {
		R = L + sz[i] - 1;
		for (int j = i; j <= m; R += sz[++j]) {
			if (L == R) continue;
			std::reverse(a + L,a + R + 1);
			l[step] = L; r[step] = R;
			dfs(step + 1);
			std::reverse(a + L,a + R + 1);
		}
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("339E.in","r",stdin);
	freopen("339E.out","w",stdout);
	#endif
	scanf("%d",&n);
	for (int i = 1; i <= n; ++i) scanf("%d",&a[i]);
	for (int i = 1,t = 0; i <= n; ++i) {
		if (abs(t) != 1 || a[i] != a[i - 1] + t) ++m,t = a[i + 1] - a[i];
		++sz[m];
	}
	dfs(1);
}


#include <cstdio>
#include <algorithm>
#include <cstdlib>
int a[1009],sz[10],l[4],r[4],n,m;
bool check()
{
	for (int i = 1; i <= n; ++i)
		if (a[i] != i) return false;
	return true;
}
void dfs(int step)
{
	if (check()) {
		printf("%d\n",step - 1);
		for (int i = 1; i < step; ++i) printf("%d %d\n",l[step - i],r[step - i]);
		exit(0);
	}
	if (step > 3) return;
	for (int i = 1,L = 1,R; i <= m; L += sz[i++]) {
		R = L + sz[i] - 1;
		for (int j = i; j <= m; R += sz[++j]) {
			if (L == R) continue;
			std::reverse(a + L,a + R + 1);
			std::reverse(sz + i,sz + j + 1);
			l[step] = L; r[step] = R;
			dfs(step + 1);
			std::reverse(sz + i,sz + j + 1);
			std::reverse(a + L,a + R + 1);
		}
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("339E.in","r",stdin);
	freopen("339E.out","w",stdout);
	#endif
	scanf("%d",&n);
	for (int i = 1; i <= n; ++i) scanf("%d",&a[i]);
	for (int i = 1,t = 0; i <= n; ++i) {
		if (abs(t) != 1 || a[i] != a[i - 1] + t) ++m,t = a[i + 1] - a[i];
		++sz[m];
	}
	dfs(1);
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<string>
#define LL long long
#define MP make_pair
#define PII pair<int,int>
using namespace std;
bool flag;
int n,a[1010],vis[2010],T;
PII ans[5];
void read(int &x)
{
	char ch=getchar();int mark=1;for(;ch!='-'&&(ch<'0'||ch>'9');ch=getchar());if (ch=='-') mark=-1,ch=getchar();
	for(x=0;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-48;x*=mark;
}
bool check(int step)
{
	for(int i=1;i<=n;i++) if (a[i]!=i) return 0;
	return 1;
}
void rev(int *a,int l,int r)
{
	for(int i=0;i<=(r-l-1)/2;i++) swap(a[i+l],a[r-i]);
}
int expect()
{
	int ans=1;
	for(int i=2;i<=n;i++)
		if (abs(a[i]-a[i-1])!=1) ans++;
	return ans;
}
void dfs(int step)
{
	if (check(step-1))
	{
		printf("%d\n",step-1);
		for(int i=step-1;i;i--) printf("%d %d\n",ans[i].first,ans[i].second);
		flag=1;
		return;
	}
	if (step>3) return;
	int c[1010];
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		if (abs(a[i-1]-a[j])==1||abs(a[i]-a[j+1])==1)
		{
			for(int k=i;k<=j;k++) c[k]=a[k];
			rev(a,i,j);
			if (expect()<=(3-step)*2+1)
			{
				ans[step]=MP(i,j);
				dfs(step+1);
				if (flag) return;
			}
			for(int k=i;k<=j;k++) a[k]=c[k];
		}
	}
}
int main()
{
	//freopen("a.txt","r",stdin);
	read(n);
	for(int i=1;i<=n;i++) read(a[i]);
	a[n+1]=n+1;
	dfs(1);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<string>
#define LL long long
#define MP make_pair
#define PII pair<int,int>
using namespace std;
bool flag;
int n,a[1010],vis[2010],T;
PII ans[5];
void read(int &x)
{
	char ch=getchar();int mark=1;for(;ch!='-'&&(ch<'0'||ch>'9');ch=getchar());if (ch=='-') mark=-1,ch=getchar();
	for(x=0;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-48;x*=mark;
}
bool check(int step)
{
	for(int i=1;i<=n;i++) if (a[i]!=i) return 0;
	return 1;
}
void rev(int *a,int l,int r)
{
	for(int i=0;i<=(r-l-1)/2;i++) swap(a[i+l],a[r-i]);
}
int expect()
{
	int ans=0;
	for(int i=2;i<=n;i++)
		if (abs(a[i]-a[i-1])!=1) ans++;
	return ans;
}
void dfs(int step)
{
	if (check(step-1))
	{
		printf("%d\n",step-1);
		for(int i=step-1;i;i--) printf("%d %d\n",ans[i].first,ans[i].second);
		flag=1;
		return;
	}
	if (step>3) return;
	int c[1010];
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			for(int k=i;k<=j;k++) c[k]=a[k];
			rev(a,i,j);
			if (expect()<=(3-step)*2+1)
			{
				ans[step]=MP(i,j);
				dfs(step+1);
				if (flag) return;
			}
			for(int k=i;k<=j;k++) a[k]=c[k];
		}
	}
}
int main()
{
	//freopen("a.txt","r",stdin);
	read(n);
	for(int i=1;i<=n;i++) read(a[i]);
	dfs(1);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<string>
#define LL long long
#define MP make_pair
#define PII pair<int,int>
using namespace std;
bool flag;
int n,a[1010],vis[2010],T;
PII ans[5];
void read(int &x)
{
	char ch=getchar();int mark=1;for(;ch!='-'&&(ch<'0'||ch>'9');ch=getchar());if (ch=='-') mark=-1,ch=getchar();
	for(x=0;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-48;x*=mark;
}
bool check(int step)
{
	for(int i=1;i<=n;i++) if (a[i]!=i) return 0;
	return 1;
}
void rev(int *a,int l,int r)
{
	for(int i=0;i<=(r-l-1)/2;i++) swap(a[i+l],a[r-i]);
}
int expect()
{
	int ans=1;
	for(int i=2;i<=n;i++)
		if (abs(a[i]-a[i-1])!=1) ans++;
	return ans;
}
void dfs(int step)
{
	if (check(step-1))
	{
		printf("%d\n",step-1);
		for(int i=step-1;i;i--) printf("%d %d\n",ans[i].first,ans[i].second);
		flag=1;
		return;
	}
	if (step>3) return;
	int c[1010];
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		if (abs(a[i-1]-a[j])==1&&abs(a[i]-a[j+1])==1)
		{
			for(int k=i;k<=j;k++) c[k]=a[k];
			rev(a,i,j);
			if (expect()<=(3-step)*2+1)
			{
				ans[step]=MP(i,j);
				dfs(step+1);
				if (flag) return;
			}
			for(int k=i;k<=j;k++) a[k]=c[k];
		}
	}
}
int main()
{
	//freopen("a.txt","r",stdin);
	read(n);
	for(int i=1;i<=n;i++) read(a[i]);
	a[n+1]=n+1;
	dfs(1);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<string>
#define LL long long
#define MP make_pair
#define PII pair<int,int>
using namespace std;
bool flag;
int n,a[1010],vis[2010],T;
PII ans[5];
void read(int &x)
{
	char ch=getchar();int mark=1;for(;ch!='-'&&(ch<'0'||ch>'9');ch=getchar());if (ch=='-') mark=-1,ch=getchar();
	for(x=0;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-48;x*=mark;
}
bool check(int step)
{
	for(int i=1;i<=n;i++) if (a[i]!=i) return 0;
	return 1;
}
void rev(int *a,int l,int r)
{
	for(int i=0;i<=(r-l-1)/2;i++) swap(a[i+l],a[r-i]);
}
int expect()
{
	T++;
	int ans=0;
	for(int i=1;i<=n;i++)
		if (a[i]!=i)
		{
			int t=i+a[i];
			if (vis[t]!=T)
			{
				vis[t]=T;ans++;
			}
		}
	return ans;
}
void dfs(int step)
{
	if (check(step-1))
	{
		printf("%d\n",step-1);
		for(int i=step-1;i;i--) printf("%d %d\n",ans[i].first,ans[i].second);
		flag=1;
		return;
	}
	if (step>3) return;
	int c[1010];
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			for(int k=i;k<=j;k++) c[k]=a[k];
			rev(a,i,j);
			if (expect()<=(3-step)*2)
			{
				ans[step]=MP(i,j);
				dfs(step+1);
				if (flag) return;
			}
			for(int k=i;k<=j;k++) a[k]=c[k];
		}
	}
}
int main()
{
	//freopen("a.txt","r",stdin);
	read(n);
	for(int i=1;i<=n;i++) read(a[i]);
	dfs(1);
	return 0;
}


#include <bits/stdc++.h>

#define R "%d"
#define RL "%I64d"
#define RD "%lf"
#define W "%.8lf"

#define Max(x, y) ((x) > (y) ? (x) : (y))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Set(x, y) memset(x, y, sizeof(x))
#define Cpy(x, y) memcpy(x, y, sizeof(y))
#define All(x) x.begin(), x.end()
#define Sqr(x) ((x) * (x))
#define Sz(x) ((LL)x.size())

#define Mk make_pair
#define Pb push_back
#define Pk pop_back
#define Fr first
#define Se second

#define FOR(x, l, r) for (LL x = (l), end = (r); x <= end; ++x)
#define ROF(x, l, r) for (LL x = (l), end = (r); x >= end; --x)
#define Edge(x, y) for (LL x = g[y]; x; x = nt[x])

#define Read(x) ((x) = Get())
#define Lowbit(x) ((x) & (-x))
using namespace std;

typedef int LL;
typedef long long ll;
typedef double LD;
typedef long double real;

typedef map <LL, LL> MI;
typedef pair <LL, LL> PI;
typedef set <LL> SI;
typedef vector <LL> VI;

inline LL Get() {
	LL res = 0, q = 1;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
	if (ch == '-') q = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar();
	return res * q;
}

const LD eps = 1e-12, pi = M_PI;
const LL oo = (LL)2e9, mod = (LL)1e9 + 7;
const ll INF = (ll)1e17;

const LL N = 1000;

LL a[N + 10], b[N + 10], l[4], r[4];
LL n, m;
bool v[N + 10][N + 10];

void Dfs(LL x) {
	bool ret = 1;
	FOR (i, 1, n) if (a[i] != i) { ret = 0; break; }
	if (ret) {
		printf(R "\n", x - 1);
		FOR (i, 1, x - 1) printf(R " " R "\n", l[i], r[i]);
		exit(0);
	}
	if (x > 3) return;
	FOR (i, 1, m) {
		FOR (j, i + 1, m)
			if (!v[i][j]) {
				v[i][j] = 1;
				reverse(a + b[i], a + b[j] + 1);
				l[x] = b[i], r[x] = b[j];
				Dfs(x + 1);
				v[i][j] = 0;
				reverse(a + b[i], a + b[j] + 1);
			}
	}
}

LL main() {
#ifndef ONLINE_JUDGE
	freopen("swap.in", "r", stdin);
	freopen("swap.out", "w", stdout);
#endif

	scanf(R "\n", &n);
	FOR (i, 1, n) scanf(R, a + i);
	a[n + 1] = n + 1;

	FOR (i, 1, n)
		if (abs(a[i] - a[i - 1]) != 1 || abs(a[i] - a[i + 1]) != 1) b[++m] = i;

	Dfs(1);

	return 0;
}


#include <bits/stdc++.h>

#define R "%d"
#define RL "%I64d"
#define RD "%lf"
#define W "%.8lf"

#define Max(x, y) ((x) > (y) ? (x) : (y))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Set(x, y) memset(x, y, sizeof(x))
#define Cpy(x, y) memcpy(x, y, sizeof(y))
#define All(x) x.begin(), x.end()
#define Sqr(x) ((x) * (x))
#define Sz(x) ((LL)x.size())

#define Mk make_pair
#define Pb push_back
#define Pk pop_back
#define Fr first
#define Se second

#define FOR(x, l, r) for (LL x = (l), end = (r); x <= end; ++x)
#define ROF(x, l, r) for (LL x = (l), end = (r); x >= end; --x)
#define Edge(x, y) for (LL x = g[y]; x; x = nt[x])

#define Read(x) ((x) = Get())
#define Lowbit(x) ((x) & (-x))
using namespace std;

typedef int LL;
typedef long long ll;
typedef double LD;
typedef long double real;

typedef map <LL, LL> MI;
typedef pair <LL, LL> PI;
typedef set <LL> SI;
typedef vector <LL> VI;

inline LL Get() {
	LL res = 0, q = 1;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
	if (ch == '-') q = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar();
	return res * q;
}

const LD eps = 1e-12, pi = M_PI;
const LL oo = (LL)2e9, mod = (LL)1e9 + 7;
const ll INF = (ll)1e17;

const LL N = 1000;

LL a[N + 10], b[N + 10], l[4], r[4];
LL n, m;
bool v[N + 10][N + 10];

void Dfs(LL x) {
	bool ret = 1;
	FOR (i, 1, n) if (a[i] != i) { ret = 0; break; }
	if (ret) {
		printf(R "\n", x - 1);
		ROF (i, x - 1, 1) printf(R " " R "\n", l[i], r[i]);
		exit(0);
	}
	if (x > 3) return;
	FOR (i, 1, m) {
		FOR (j, i + 1, m)
			if (!v[i][j]) {
				v[i][j] = 1;
				reverse(a + b[i], a + b[j] + 1);
				l[x] = b[i], r[x] = b[j];
				Dfs(x + 1);
				v[i][j] = 0;
				reverse(a + b[i], a + b[j] + 1);
			}
	}
}

LL main() {
#ifndef ONLINE_JUDGE
	freopen("swap.in", "r", stdin);
	freopen("swap.out", "w", stdout);
#endif

	scanf(R "\n", &n);
	FOR (i, 1, n) scanf(R, a + i);
	a[n + 1] = n + 1;

	FOR (i, 1, n)
		if (abs(a[i] - a[i - 1]) != 1 || abs(a[i] - a[i + 1]) != 1) b[++m] = i;

	Dfs(1);

	return 0;
}


#include <bits/stdc++.h>

#define R "%d"
#define RL "%I64d"
#define RD "%lf"
#define W "%.8lf"

#define Max(x, y) ((x) > (y) ? (x) : (y))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Set(x, y) memset(x, y, sizeof(x))
#define Cpy(x, y) memcpy(x, y, sizeof(y))
#define All(x) x.begin(), x.end()
#define Sqr(x) ((x) * (x))
#define Sz(x) ((LL)x.size())

#define Mk make_pair
#define Pb push_back
#define Pk pop_back
#define Fr first
#define Se second

#define FOR(x, l, r) for (LL x = (l), end = (r); x <= end; ++x)
#define ROF(x, l, r) for (LL x = (l), end = (r); x >= end; --x)
#define Edge(x, y) for (LL x = g[y]; x; x = nt[x])

#define Read(x) ((x) = Get())
#define Lowbit(x) ((x) & (-x))
using namespace std;

typedef int LL;
typedef long long ll;
typedef double LD;
typedef long double real;

typedef map <LL, LL> MI;
typedef pair <LL, LL> PI;
typedef set <LL> SI;
typedef vector <LL> VI;

inline LL Get() {
	LL res = 0, q = 1;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
	if (ch == '-') q = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar();
	return res * q;
}

const LD eps = 1e-12, pi = M_PI;
const LL oo = (LL)2e9, mod = (LL)1e9 + 7;
const ll INF = (ll)1e17;

const LL N = (LL)1e6;

LL a[N + 10], b[N + 10], vl[4], vr[4];
LL n, m;

void Dfs(LL *a, LL x, LL tp) {
	LL l = 1, r = n;
	for ( ; a[l] == l; ++l);
	if (l > n) {
		printf(R "\n", x - 1);
		if (!tp)
			ROF (i, x - 1, 1) printf(R " " R "\n", vl[i], vr[i]);
		else FOR (i, 1, x - 1) printf(R " " R "\n", vl[i], vr[i]);
		exit(0);
	}
	if (x > 3) return;
	for (r = l + 1; r <= n; ++r) if (a[r] == l) break;
	reverse(a + l, a + r + 1);
	vl[x] = l, vr[x] = r;
	Dfs(a, x + 1, tp);
	reverse(a + l, a + r + 1);
	for (r = n; a[r] == r; --r);
	for (l = r - 1; l >= 1; --l) if (a[l] == r) break;
	reverse(a + l, a + r + 1);
	vl[x] = l, vr[x] = r;
	Dfs(a, x + 1, tp);
	reverse(a + l, a + r + 1);
}

LL main() {
#ifndef ONLINE_JUDGE
	freopen("swap.in", "r", stdin);
	freopen("swap.out", "w", stdout);
#endif

	scanf(R "\n", &n);
	FOR (i, 1, n) scanf(R, a + i), b[a[i]] = i;

	Dfs(a, 1, 0);
	Dfs(b, 1, 1);

	return 0;
}


#include <bits/stdc++.h>

#define R "%d"
#define RL "%I64d"
#define RD "%lf"
#define W "%.8lf"

#define Max(x, y) ((x) > (y) ? (x) : (y))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Set(x, y) memset(x, y, sizeof(x))
#define Cpy(x, y) memcpy(x, y, sizeof(y))
#define All(x) x.begin(), x.end()
#define Sqr(x) ((x) * (x))
#define Sz(x) ((LL)x.size())

#define Mk make_pair
#define Pb push_back
#define Pk pop_back
#define Fr first
#define Se second

#define FOR(x, l, r) for (LL x = (l), end = (r); x <= end; ++x)
#define ROF(x, l, r) for (LL x = (l), end = (r); x >= end; --x)
#define Edge(x, y) for (LL x = g[y]; x; x = nt[x])

#define Read(x) ((x) = Get())
#define Lowbit(x) ((x) & (-x))
using namespace std;

typedef int LL;
typedef long long ll;
typedef double LD;
typedef long double real;

typedef map <LL, LL> MI;
typedef pair <LL, LL> PI;
typedef set <LL> SI;
typedef vector <LL> VI;

inline LL Get() {
	LL res = 0, q = 1;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
	if (ch == '-') q = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar();
	return res * q;
}

const LD eps = 1e-12, pi = M_PI;
const LL oo = (LL)2e9, mod = (LL)1e9 + 7;
const ll INF = (ll)1e17;

const LL N = (LL)1e6;

LL a[N + 10], vl[4], vr[4];
LL n, m;

void Dfs(LL x) {
	LL l = 1, r = n;
	for ( ; a[l] == l; ++l);
	if (l > n) {
		printf(R "\n", x - 1);
		ROF (i, x - 1, 1) printf(R " " R "\n", vl[i], vr[i]);
		exit(0);
	}
	if (x > 3) return;
	for (r = l + 1; r <= n; ++r) if (a[r] == l) break;
	reverse(a + l, a + r + 1);
	vl[x] = l, vr[x] = r;
	Dfs(x + 1);
	reverse(a + l, a + r + 1);
	for ( ; a[r] == r; --r);
	for (l = r - 1; l >= 1; --l) if (a[l] == r) break;
	reverse(a + l, a + r + 1);
	vl[x] = l, vr[x] = r;
	Dfs(x + 1);
	reverse(a + l, a + r + 1);
}

LL main() {
#ifndef ONLINE_JUDGE
	freopen("swap.in", "r", stdin);
	freopen("swap.out", "w", stdout);
#endif

	scanf(R "\n", &n);
	FOR (i, 1, n) scanf(R, a + i);

	Dfs(1);

	return 0;
}


#include <bits/stdc++.h>

#define R "%d"
#define RL "%I64d"
#define RD "%lf"
#define W "%.8lf"

#define Max(x, y) ((x) > (y) ? (x) : (y))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Set(x, y) memset(x, y, sizeof(x))
#define Cpy(x, y) memcpy(x, y, sizeof(y))
#define All(x) x.begin(), x.end()
#define Sqr(x) ((x) * (x))
#define Sz(x) ((LL)x.size())

#define Mk make_pair
#define Pb push_back
#define Pk pop_back
#define Fr first
#define Se second

#define FOR(x, l, r) for (LL x = (l), end = (r); x <= end; ++x)
#define ROF(x, l, r) for (LL x = (l), end = (r); x >= end; --x)
#define Edge(x, y) for (LL x = g[y]; x; x = nt[x])

#define Read(x) ((x) = Get())
#define Lowbit(x) ((x) & (-x))
using namespace std;

typedef int LL;
typedef long long ll;
typedef double LD;
typedef long double real;

typedef map <LL, LL> MI;
typedef pair <LL, LL> PI;
typedef set <LL> SI;
typedef vector <LL> VI;

inline LL Get() {
	LL res = 0, q = 1;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
	if (ch == '-') q = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar();
	return res * q;
}

const LD eps = 1e-12, pi = M_PI;
const LL oo = (LL)2e9, mod = (LL)1e9 + 7;
const ll INF = (ll)1e17;

const LL N = (LL)1e6;

LL a[N + 10], vl[4], vr[4];
LL n, m;

void Dfs(LL x) {
	LL l = 1, r = n;
	for ( ; a[l] == l; ++l);
	if (l > n) {
		printf(R "\n", x - 1);
		ROF (i, x - 1, 1) printf(R " " R "\n", vl[i], vr[i]);
		exit(0);
	}
	if (x > 3) return;
	for (r = l + 1; r <= n; ++r) if (a[r] == l) break;
	reverse(a + l, a + r + 1);
	vl[x] = l, vr[x] = r;
	Dfs(x + 1);
	reverse(a + l, a + r + 1);
	for (r = n; a[r] == r; --r);
	for (l = r - 1; l >= 1; --l) if (a[l] == r) break;
	reverse(a + l, a + r + 1);
	vl[x] = l, vr[x] = r;
	Dfs(x + 1);
	reverse(a + l, a + r + 1);
}

LL main() {
#ifndef ONLINE_JUDGE
	freopen("swap.in", "r", stdin);
	freopen("swap.out", "w", stdout);
#endif

	scanf(R "\n", &n);
	FOR (i, 1, n) scanf(R, a + i);

	Dfs(1);

	return 0;
}


#include <bits/stdc++.h>

#define R "%d"
#define RL "%I64d"
#define RD "%lf"
#define W "%.8lf"

#define Max(x, y) ((x) > (y) ? (x) : (y))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Set(x, y) memset(x, y, sizeof(x))
#define Cpy(x, y) memcpy(x, y, sizeof(y))
#define All(x) x.begin(), x.end()
#define Sqr(x) ((x) * (x))
#define Sz(x) ((LL)x.size())

#define Mk make_pair
#define Pb push_back
#define Pk pop_back
#define Fr first
#define Se second

#define FOR(x, l, r) for (LL x = (l), end = (r); x <= end; ++x)
#define ROF(x, l, r) for (LL x = (l), end = (r); x >= end; --x)
#define Edge(x, y) for (LL x = g[y]; x; x = nt[x])

#define Read(x) ((x) = Get())
#define Lowbit(x) ((x) & (-x))
using namespace std;

typedef int LL;
typedef long long ll;
typedef double LD;
typedef long double real;

typedef map <LL, LL> MI;
typedef pair <LL, LL> PI;
typedef set <LL> SI;
typedef vector <LL> VI;

inline LL Get() {
	LL res = 0, q = 1;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
	if (ch == '-') q = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar();
	return res * q;
}

const LD eps = 1e-12, pi = M_PI;
const LL oo = (LL)2e9, mod = (LL)1e9 + 7;
const ll INF = (ll)1e17;

const LL N = 1000;

LL a[N + 10], l[4], r[4];
LL n, m;
bool v[N + 10][N + 10];

void Dfs(LL x) {
	bool ret = 1;
	FOR (i, 1, n) if (a[i] != i) { ret = 0; break; }
	if (ret) {
		printf(R "\n", x - 1);
		ROF (i, x - 1, 1) printf(R " " R "\n", l[i], r[i]);
		exit(0);
	}
	if (x > 3) return;
	FOR (i, 1, n)
		if (abs(a[i] - a[i + 1]) != 1 || abs(a[i] - a[i - 1]) != 1) {
			FOR (j, i + 1, n)
				if ((abs(a[j] - a[j + 1]) != 1 || abs(a[j] - a[j - 1]) != 1) && !v[i][j]) {
					v[i][j] = 1;
					l[x] = i, r[x] = j;
					reverse(a + i, a + j + 1);
					Dfs(x + 1);
					v[i][j] = 0;
					reverse(a + i, a + j + 1);
				}
		}
}

LL main() {
#ifndef ONLINE_JUDGE
	freopen("swap.in", "r", stdin);
	freopen("swap.out", "w", stdout);
#endif

	scanf(R "\n", &n);
	FOR (i, 1, n) scanf(R, a + i);
	a[n + 1] = n + 1;

	Dfs(1);

	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int inf=2e9+7;
const ll linf=2e18+7;
int n;
int a[1007];
int hh1[17],c1=0,hh2[17],c2=0,hh3[17],c3=0;
int jh(int l,int r){
	for (int i=l;i<=(l+r)/2;i++){
		swap(a[i],a[l+r-i]);
	}
}
bool check(){
	for (int i=0;i<n;i++){
		if (a[i]!=i+1){
			return false;
		}
	}
	return true;
}
int main(){
	scanf("%d",&n);
	hh1[c1]=0;
	c1++;
	for (int i=0;i<n;i++){
		scanf("%d",a+i);
		if (i>=1 && (abs(a[i]-a[i-1]))!=1 || (i>=2 && (a[i]-a[i-1]!=a[i-1]-a[i-2]))){
			hh1[c1]=i;
			//cout<<i<<endl;
			c1++;
		}
	}
	hh1[c1]=n;
	c1++;
	if (check()){
		printf("0\n");
		return 0;
	}
	for (int i1=0;i1<c1;i1++){
		for (int j1=i1+1;j1<c1;j1++){
			//cout<<i1<<' '<<j1<<endl;
			if (hh1[i1]==hh1[j1]-1){
				continue;
			}
			jh(hh1[i1],hh1[j1]-1);
			if (check()){
				printf("1\n");
				printf("%d %d\n",hh1[i1]+1,hh1[j1]);
				return 0;
			}
			c2=0;
			hh2[c2]=0;
			c2++;
			for (int k=1;k<n;k++){
				if (abs(a[k]-a[k-1])!=1 || (k>=2 && (a[k]-a[k-1]!=a[k-1]-a[k-2]))){
					hh2[c2]=k;
					c2++;
					if (c2>=7){
						continue;
					}
				}
			}
			hh2[c2]=n;
			c2++;
			for (int i2=0;i2<c2;i2++){
				for (int j2=i2+1;j2<c2;j2++){
					if (hh2[i2]==hh2[j2]-1){
						continue;
					}
					jh(hh2[i2],hh2[j2]-1);
					//if (i1==2 && j1==4 && i2==1 && j2==)
					if (check()){
						printf("2\n");
						printf("%d %d\n",hh1[i1]+1,hh1[j1]);
						printf("%d %d\n",hh2[i2]+1,hh2[j2]);
						return 0;
					}
					c3=0;
					hh3[c3]=0;
					c3++;
					for (int k=1;k<n;k++){
						if (abs(a[k]-a[k-1])!=1 || (k>=2 && (a[k]-a[k-1]!=a[k-1]-a[k-2]))){
							hh3[c3]=k;
							c3++;
							if (c3>=7){
								continue;
							}
						}
					}
					hh3[c3]=n;
					c3++;
					for (int i3=0;i3<c3;i3++){
						for (int j3=i3+1;j3<c3;j3++){
							if (hh3[i3]==hh3[j3]-1){
								continue;
							}
							//cout<<1<<endl;
							/*
							if (i1==0 && j1==1 && i2==0 && j2==1){
								cout<<i3<<' '<<j3<<endl;
							}
							*/
							jh(hh3[i3],hh3[j3]-1);
							if (check()){
								printf("3\n");
								printf("%d %d\n",hh1[i1]+1,hh1[j1]);
								printf("%d %d\n",hh2[i2]+1,hh2[j2]);
								printf("%d %d\n",hh3[i3]+1,hh3[j3]);
								return 0;
							}
							jh(hh3[i3],hh3[j3]-1);
						}
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					for (int i=0;i<n;i++){
						cout<<a[i]<<' ';
					}
					cout<<endl;
					*/
				}
			}
			jh(hh1[i1],hh1[j1]-1);
			/*
			for (int i=0;i<n;i++){
				cout<<a[i]<<' ';
			}
			cout<<endl;
			*/
		}
	}
	return 0;
}
/*
input:
100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 70 71 72 73 74 26 25 24 23 22
98 99 100
*/


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int inf=2e9+7;
const ll linf=2e18+7;
int n;
int a[1007];
int hh1[17],c1=0,hh2[17],c2=0,hh3[17],c3=0;
int jh(int l,int r){
	for (int i=l;i<=(l+r)/2;i++){
		swap(a[i],a[l+r-i]);
	}
}
bool check(){
	for (int i=0;i<n;i++){
		if (a[i]!=i+1){
			return false;
		}
	}
	return true;
}
int main(){
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	scanf("%d",&n);
	hh1[c1]=0;
	c1++;
	for (int i=0;i<n;i++){
		scanf("%d",a+i);
		//cout<<1<<endl;
		if (i>=1 && (abs(a[i]-a[i-1]))!=1 || (i>=2 && (a[i]-a[i-1]!=a[i-1]-a[i-2]))){
			hh1[c1]=i;
			//cout<<i<<endl;
			c1++;
		}
	}
	hh1[c1]=n;
	c1++;
	//cout<<1<<endl;
	if (check()){
		printf("0\n");
		return 0;
	}
	for (int i1=0;i1<c1;i1++){
		for (int j1=i1+1;j1<c1;j1++){
			if (hh1[i1]==hh1[j1]-1){
				continue;
			}
			jh(hh1[i1],hh1[j1]-1);
			if (check()){
				printf("1\n");
				printf("%d %d\n",hh1[i1]+1,hh1[j1]);
				return 0;
			}
			bool f=true;
			c2=0;
			hh2[c2]=0;
			c2++;
			for (int k=1;k<n;k++){
				if (abs(a[k]-a[k-1])!=1 || (k>=2 && (a[k]-a[k-1]!=a[k-1]-a[k-2]))){
					hh2[c2]=k;
					c2++;
					if (c2>=7){
						f=false;
						break;
					}
				}
			}
			if (!f){
				continue;
			}
			hh2[c2]=n;
			c2++;
			for (int i2=0;i2<c2;i2++){
				for (int j2=i2+1;j2<c2;j2++){
					if (hh2[i2]==hh2[j2]-1){
						continue;
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					if (i1==0 && j1==2){
						cout<<i2<<' '<<j2<<endl;
					}
					*/
					if (check()){
						printf("2\n");
						printf("%d %d\n",hh1[i1]+1,hh1[j1]);
						printf("%d %d\n",hh2[i2]+1,hh2[j2]);
						return 0;
					}
					bool f=true;
					c3=0;
					hh3[c3]=0;
					c3++;
					for (int k=1;k<n;k++){
						if (abs(a[k]-a[k-1])!=1 || (k>=2 && (a[k]-a[k-1]!=a[k-1]-a[k-2]))){
							hh3[c3]=k;
							c3++;
							if (c3>=7){
								f=false;
								break;
							}
						}
					}
					if (!f){
						continue;
					}
					hh3[c3]=n;
					c3++;
					for (int i3=0;i3<c3;i3++){
						for (int j3=i3+1;j3<c3;j3++){
							if (hh3[i3]==hh3[j3]-1){
								continue;
							}
							//cout<<1<<endl;
							/*
							if (i1==0 && j1==1 && i2==0 && j2==1){
								cout<<i3<<' '<<j3<<endl;
							}
							*/
							jh(hh3[i3],hh3[j3]-1);
							if (check()){
								printf("3\n");
								printf("%d %d\n",hh1[i1]+1,hh1[j1]);
								printf("%d %d\n",hh2[i2]+1,hh2[j2]);
								printf("%d %d\n",hh3[i3]+1,hh3[j3]);
								return 0;
							}
							jh(hh3[i3],hh3[j3]-1);
						}
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					for (int i=0;i<n;i++){
						cout<<a[i]<<' ';
					}
					cout<<endl;
					*/
				}
			}
			jh(hh1[i1],hh1[j1]-1);
			/*
			for (int i=0;i<n;i++){
				cout<<a[i]<<' ';
			}
			cout<<endl;
			*/
		}
	}
	//cout<<1<<endl;
	return 0;
}
/*
input:
100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 70 71 72 73 74 26 25 24 23 22
98 99 100
*/


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int inf=2e9+7;
const ll linf=2e18+7;
int n;
int a[1007];
int hh1[27],c1=0,hh2[27],c2=0,hh3[27],c3=0;
int jh(int l,int r){
	for (int i=l;i<=(l+r)/2;i++){
		swap(a[i],a[l+r-i]);
	}
}
bool check(){
	for (int i=0;i<n;i++){
		if (a[i]!=i+1){
			return false;
		}
	}
	return true;
}
int main(){
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	scanf("%d",&n);
	hh1[c1]=0;
	c1++;
	for (int i=0;i<n;i++){
		scanf("%d",a+i);
		//cout<<1<<endl;
		if (i>=1 && (abs(a[i]-a[i-1]))!=1 || (i>=2 && (a[i]-a[i-1]!=a[i-1]-a[i-2]))){
			hh1[c1]=i;
			//cout<<i<<endl;
			c1++;
		}
	}
	hh1[c1]=n;
	c1++;
	//cout<<1<<endl;
	if (check()){
		printf("0\n");
		return 0;
	}
	for (int i1=0;i1<c1;i1++){
		for (int j1=i1+1;j1<c1;j1++){
			if (hh1[i1]==hh1[j1]-1){
				continue;
			}
			jh(hh1[i1],hh1[j1]-1);
			if (check()){
				printf("1\n");
				printf("%d %d\n",hh1[i1]+1,hh1[j1]);
				return 0;
			}
			bool f=true;
			c2=0;
			hh2[c2]=0;
			c2++;
			for (int k=1;k<n;k++){
				if (abs(a[k]-a[k-1])!=1 || (k>=2 && (a[k]-a[k-1]!=a[k-1]-a[k-2]))){
					hh2[c2]=k;
					c2++;
					if (c2>=27){
						f=false;
						break;
					}
				}
			}
			if (!f){
				continue;
			}
			hh2[c2]=n;
			c2++;
			for (int i2=0;i2<c2;i2++){
				for (int j2=i2+1;j2<c2;j2++){
					if (hh2[i2]==hh2[j2]-1){
						continue;
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					if (i1==0 && j1==2){
						cout<<i2<<' '<<j2<<endl;
					}
					*/
					if (check()){
						printf("2\n");
						printf("%d %d\n",hh1[i1]+1,hh1[j1]);
						printf("%d %d\n",hh2[i2]+1,hh2[j2]);
						return 0;
					}
					bool f=true;
					c3=0;
					hh3[c3]=0;
					c3++;
					for (int k=1;k<n;k++){
						if (abs(a[k]-a[k-1])!=1 || (k>=2 && (a[k]-a[k-1]!=a[k-1]-a[k-2]))){
							hh3[c3]=k;
							c3++;
							if (c3>=27){
								f=false;
								break;
							}
						}
					}
					if (!f){
						continue;
					}
					hh3[c3]=n;
					c3++;
					for (int i3=0;i3<c3;i3++){
						for (int j3=i3+1;j3<c3;j3++){
							if (hh3[i3]==hh3[j3]-1){
								continue;
							}
							//cout<<1<<endl;
							/*
							if (i1==0 && j1==1 && i2==0 && j2==1){
								cout<<i3<<' '<<j3<<endl;
							}
							*/
							jh(hh3[i3],hh3[j3]-1);
							if (check()){
								printf("3\n");
								printf("%d %d\n",hh1[i1]+1,hh1[j1]);
								printf("%d %d\n",hh2[i2]+1,hh2[j2]);
								printf("%d %d\n",hh3[i3]+1,hh3[j3]);
								return 0;
							}
							jh(hh3[i3],hh3[j3]-1);
						}
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					for (int i=0;i<n;i++){
						cout<<a[i]<<' ';
					}
					cout<<endl;
					*/
				}
			}
			jh(hh1[i1],hh1[j1]-1);
			/*
			for (int i=0;i<n;i++){
				cout<<a[i]<<' ';
			}
			cout<<endl;
			*/
		}
	}
	//cout<<1<<endl;
	return 0;
}
/*
input:
100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 70 71 72 73 74 26 25 24 23 22
98 99 100
*/


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int inf=2e9+7;
const ll linf=2e18+7;
int n;
int a[1007];
int hh1[17],c1=0,hh2[17],c2=0,hh3[17],c3=0;
int jh(int l,int r){
	for (int i=l;i<=(l+r)/2;i++){
		swap(a[i],a[l+r-i]);
	}
}
bool check(){
	for (int i=0;i<n;i++){
		if (a[i]!=i+1){
			return false;
		}
	}
	return true;
}
int main(){
	scanf("%d",&n);
	hh1[c1]=0;
	c1++;
	for (int i=0;i<n;i++){
		scanf("%d",a+i);
		if (i>=1 && abs(a[i]-a[i-1])!=1){
			hh1[c1]=i;
			//cout<<i<<endl;
			c1++;
		}
	}
	hh1[c1]=n;
	c1++;
	for (int i1=0;i1<c1;i1++){
		for (int j1=i1+1;j1<c1;j1++){
			//cout<<i1<<' '<<j1<<endl;
			jh(hh1[i1],hh1[j1]-1);
			if (check()){
				printf("1\n");
				printf("%d %d\n",hh1[i1]+1,hh1[j1]);
				return 0;
			}
			c2=0;
			hh2[c2]=0;
			c2++;
			for (int k=1;k<n;k++){
				if (abs(a[k]-a[k-1])!=1){
					hh2[c2]=k;
					c2++;
					if (c2>=7){
						continue;
					}
				}
			}
			hh2[c2]=n;
			c2++;
			for (int i2=0;i2<c2;i2++){
				for (int j2=i2+1;j2<c2;j2++){
					jh(hh2[i2],hh2[j2]-1);
					//if (i1==2 && j1==4 && i2==1 && j2==)
					if (check()){
						printf("2\n");
						printf("%d %d\n",hh1[i1]+1,hh1[j1]);
						printf("%d %d\n",hh2[i2]+1,hh2[j2]);
						return 0;
					}
					c3=0;
					hh3[c3]=0;
					c3++;
					for (int k=1;k<n;k++){
						if (abs(a[k]-a[k-1])!=1){
							hh3[c3]=k;
							c3++;
							if (c3>=7){
								continue;
							}
						}
					}
					hh3[c3]=n;
					c3++;
					for (int i3=0;i3<c3;i3++){
						for (int j3=i3+1;j3<c3;j3++){
							//cout<<1<<endl;
							/*
							if (i1==0 && j1==1 && i2==0 && j2==1){
								cout<<i3<<' '<<j3<<endl;
							}
							*/
							jh(hh3[i3],hh3[j3]-1);
							if (check()){
								printf("3\n");
								printf("%d %d\n",hh1[i1]+1,hh1[j1]);
								printf("%d %d\n",hh2[i2]+1,hh2[j2]);
								printf("%d %d\n",hh3[i3]+1,hh3[j3]);
								return 0;
							}
							jh(hh3[i3],hh3[j3]-1);
						}
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					for (int i=0;i<n;i++){
						cout<<a[i]<<' ';
					}
					cout<<endl;
					*/
				}
			}
			jh(hh1[i1],hh1[j1]-1);
			/*
			for (int i=0;i<n;i++){
				cout<<a[i]<<' ';
			}
			cout<<endl;
			*/
		}
	}
	return 0;
}
/*
input:
11
1 5 4 3 10 9 8 7 6 2 11
*/


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int inf=2e9+7;
const ll linf=2e18+7;
int a[1007];
int n;
int hh1[27],c1=0,hh2[27],c2=0,hh3[27],c3=0;
int jh(int l,int r){
	for (int i=l;i<=(l+r)/2;i++){
		swap(a[i],a[l+r-i]);
	}
}
bool check(){
	for (int i=0;i<n;i++){
		if (a[i]!=i+1){
			return false;
		}
	}
	return true;
}
void output(){
	for (int i=0;i<n;i++){
		cout<<a[i]<<' '<<i+1<<endl;
	}
}
int main(){
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	scanf("%d",&n);
	if (n==100){
		cout<<1<<endl;
	}
	hh1[c1]=0;
	c1++;
	for (int i=0;i<n;i++){
		scanf("%d",a+i);
		//cout<<1<<endl;
		if (i>=1 && (abs(a[i]-a[i-1]))!=1 || (i>=2 && (a[i]-a[i-1]==1 && a[i-1]-a[i-2]==-1) || (a[i]-a[i-1]==-1 && a[i-1]-a[i-2]==1))){
			hh1[c1]=i;
			//cout<<i<<endl;
			c1++;
		}
	}
	hh1[c1]=n;
	c1++;
	//cout<<1<<endl;
	if (check()){
		printf("0\n");
		return 0;
	}
	for (int i1=0;i1<c1;i1++){
		for (int j1=i1+1;j1<c1;j1++){
			if (hh1[i1]==hh1[j1]-1){
				continue;
			}
			jh(hh1[i1],hh1[j1]-1);
			if (check()){
				printf("1\n");
				printf("%d %d\n",hh1[i1]+1,hh1[j1]);
				return 0;
			}
			bool f=true;
			c2=0;
			hh2[c2]=0;
			c2++;
			for (int k=1;k<n;k++){
				if (abs(a[k]-a[k-1])!=1 || (k>=2 && ((a[k]-a[k-1]==1 && a[k-1]-a[k-2]==-1) || (a[k]-a[k-1]==-1 && a[k-1]-a[k-2]==1)))){
					hh2[c2]=k;
					c2++;
					if (c2>=27){
						f=false;
						break;
					}
				}
			}
			if (!f){
				continue;
			}
			hh2[c2]=n;
			c2++;
			for (int i2=0;i2<c2;i2++){
				for (int j2=i2+1;j2<c2;j2++){
					if (hh2[i2]==hh2[j2]-1){
						continue;
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					if (i1==0 && j1==2){
						cout<<i2<<' '<<j2<<endl;
					}
					*/
					if (check()){
						printf("2\n");
						printf("%d %d\n",hh1[i1]+1,hh1[j1]);
						printf("%d %d\n",hh2[i2]+1,hh2[j2]);
						return 0;
					}
					bool f=true;
					c3=0;
					hh3[c3]=0;
					c3++;
					for (int k=1;k<n;k++){
						if (abs(a[k]-a[k-1])!=1 || (k>=2 && ((a[k]-a[k-1]==1 && a[k-1]-a[k-2]==-1) || (a[k]-a[k-1]==-1 && a[k-1]-a[k-2]==1)))){
							hh3[c3]=k;
							c3++;
							if (c3>=27){
								f=false;
								break;
							}
						}
					}
					if (!f){
						continue;
					}
					hh3[c3]=n;
					c3++;
					for (int i3=0;i3<c3;i3++){
						for (int j3=i3+1;j3<c3;j3++){
							if (hh3[i3]==hh3[j3]-1){
								continue;
							}
							//cout<<1<<endl;
							/*
							if (i1==0 && j1==1 && i2==0 && j2==1){
								cout<<i3<<' '<<j3<<endl;
							}
							*/
							jh(hh3[i3],hh3[j3]-1);
							if (check()){
								printf("3\n");
								printf("%d %d\n",hh1[i1]+1,hh1[j1]);
								printf("%d %d\n",hh2[i2]+1,hh2[j2]);
								printf("%d %d\n",hh3[i3]+1,hh3[j3]);
								//output();
								return 0;
							}
							jh(hh3[i3],hh3[j3]-1);
						}
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					for (int i=0;i<n;i++){
						cout<<a[i]<<' ';
					}
					cout<<endl;
					*/
				}
			}
			jh(hh1[i1],hh1[j1]-1);
			/*
			for (int i=0;i<n;i++){
				cout<<a[i]<<' ';
			}
			cout<<endl;
			*/
		}
	}
	//cout<<1<<endl;
	return 0;
}
/*
input:
100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 70 71 72 73 74 26 25 24 23 22
98 99 100
*/


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int inf=2e9+7;
const ll linf=2e18+7;
int a[1007];
int n;
int hh1[27],c1=0,hh2[27],c2=0,hh3[27],c3=0;
int jh(int l,int r){
	for (int i=l;i<=(l+r)/2;i++){
		swap(a[i],a[l+r-i]);
	}
}
bool check(){
	for (int i=0;i<n;i++){
		if (a[i]!=i+1){
			return false;
		}
	}
	return true;
}
void output(){
	for (int i=0;i<n;i++){
		cout<<a[i]<<' '<<i+1<<endl;
	}
}
int main(){
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	scanf("%d",&n);
	hh1[c1]=0;
	c1++;
	for (int i=0;i<n;i++){
		scanf("%d",a+i);
		//cout<<1<<endl;
		if (i>=1 && (abs(a[i]-a[i-1]))!=1 || (i>=2 && ((a[i]-a[i-1]==1 && a[i-1]-a[i-2]==-1) || (a[i]-a[i-1]==-1 && a[i-1]-a[i-2]==1)))){
			hh1[c1]=i;
			//cout<<i<<endl;
			c1++;
			hh1[c1]=i+1;
			//cout<<i+1<<endl;
			c1++;
			i++;
			scanf("%d",a+i);
		}
	}
	hh1[c1]=n;
	c1++;
	//cout<<1<<endl;
	if (check()){
		printf("0\n");
		return 0;
	}
	for (int i1=0;i1<c1;i1++){
		for (int j1=i1+1;j1<c1;j1++){
			if (hh1[i1]==hh1[j1]-1){
				continue;
			}
			jh(hh1[i1],hh1[j1]-1);
			if (check()){
				printf("1\n");
				printf("%d %d\n",hh1[i1]+1,hh1[j1]);
				return 0;
			}
			bool f=true;
			c2=0;
			hh2[c2]=0;
			c2++;
			for (int k=1;k<n;k++){
				if (abs(a[k]-a[k-1])!=1 || (k>=2 && ((a[k]-a[k-1]==1 && a[k-1]-a[k-2]==-1) || (a[k]-a[k-1]==-1 && a[k-1]-a[k-2]==1)))){
					hh2[c2]=k;
					c2++;
					hh2[c2]=k+1;
					c2++;
					if (c2>=23){
						f=false;
						break;
					}
					k++;
				}
			}
			if (!f){
				continue;
			}
			hh2[c2]=n;
			c2++;
			for (int i2=0;i2<c2;i2++){
				for (int j2=i2+1;j2<c2;j2++){
					if (hh2[i2]==hh2[j2]-1){
						continue;
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					if (i1==0 && j1==2){
						cout<<i2<<' '<<j2<<endl;
					}
					*/
					if (check()){
						printf("2\n");
						printf("%d %d\n",hh2[i2]+1,hh2[j2]);
						printf("%d %d\n",hh1[i1]+1,hh1[j1]);
						return 0;
					}
					bool f=true;
					c3=0;
					hh3[c3]=0;
					c3++;
					for (int k=1;k<n;k++){
						if (abs(a[k]-a[k-1])!=1 || (k>=2 && ((a[k]-a[k-1]==1 && a[k-1]-a[k-2]==-1) || (a[k]-a[k-1]==-1 && a[k-1]-a[k-2]==1)))){

							hh3[c3]=k;
							c3++;
							hh3[c3]=k+1;
							c3++;
							if (c3>=17){
								f=false;
								break;
							}
							k++;
						}
					}
					if (!f){
						continue;
					}
					hh3[c3]=n;
					c3++;
					for (int i3=0;i3<c3;i3++){
						for (int j3=i3+1;j3<c3;j3++){
							if (hh3[i3]==hh3[j3]-1){
								continue;
							}
							/*
							if (i1==0 && j1==1 && i2==0 && j2==1){
								cout<<i3<<' '<<j3<<endl;
							}
							*/
							jh(hh3[i3],hh3[j3]-1);
							if (check()){
								printf("3\n");
								printf("%d %d\n",hh3[i3]+1,hh3[j3]);
								printf("%d %d\n",hh2[i2]+1,hh2[j2]);
								printf("%d %d\n",hh1[i1]+1,hh1[j1]);
								//output();
								return 0;
							}
							jh(hh3[i3],hh3[j3]-1);
						}
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					for (int i=0;i<n;i++){
						cout<<a[i]<<' ';
					}
					cout<<endl;
					*/
				}
			}
			jh(hh1[i1],hh1[j1]-1);
			/*
			for (int i=0;i<n;i++){
				cout<<a[i]<<' ';
			}
			cout<<endl;
			*/
		}
	}
	//cout<<1<<endl;
	return 0;
}
/*
input:
100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 70 71 72 73 74 26 25 24 23 22
98 99 100
*/


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int inf=2e9+7;
const ll linf=2e18+7;
int a[1007];
int n;
int hh1[27],c1=0,hh2[27],c2=0,hh3[27],c3=0;
int jh(int l,int r){
	for (int i=l;i<=(l+r)/2;i++){
		swap(a[i],a[l+r-i]);
	}
}
bool check(){
	for (int i=0;i<n;i++){
		if (a[i]!=i+1){
			return false;
		}
	}
	return true;
}
void output(){
	for (int i=0;i<n;i++){
		cout<<a[i]<<' '<<i+1<<endl;
	}
}
int main(){
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	scanf("%d",&n);
	hh1[c1]=0;
	c1++;
	for (int i=0;i<n;i++){
		scanf("%d",a+i);
		//cout<<1<<endl;
		if (i>=1 && (abs(a[i]-a[i-1]))!=1 || (i>=2 && (a[i]-a[i-1]==1 && a[i-1]-a[i-2]==-1) || (a[i]-a[i-1]==-1 && a[i-1]-a[i-2]==1))){
			hh1[c1]=i;
			//cout<<i<<endl;
			c1++;
		}
	}
	hh1[c1]=n;
	c1++;
	//cout<<1<<endl;
	if (check()){
		printf("0\n");
		return 0;
	}
	for (int i1=0;i1<c1;i1++){
		for (int j1=i1+1;j1<c1;j1++){
			if (hh1[i1]==hh1[j1]-1){
				continue;
			}
			jh(hh1[i1],hh1[j1]-1);
			if (check()){
				printf("1\n");
				printf("%d %d\n",hh1[i1]+1,hh1[j1]);
				return 0;
			}
			bool f=true;
			c2=0;
			hh2[c2]=0;
			c2++;
			for (int k=1;k<n;k++){
				if (abs(a[k]-a[k-1])!=1 || (k>=2 && ((a[k]-a[k-1]==1 && a[k-1]-a[k-2]==-1) || (a[k]-a[k-1]==-1 && a[k-1]-a[k-2]==1)))){
					hh2[c2]=k;
					c2++;
					if (c2>=27){
						f=false;
						break;
					}
				}
			}
			if (!f){
				continue;
			}
			hh2[c2]=n;
			c2++;
			for (int i2=0;i2<c2;i2++){
				for (int j2=i2+1;j2<c2;j2++){
					if (hh2[i2]==hh2[j2]-1){
						continue;
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					if (i1==0 && j1==2){
						cout<<i2<<' '<<j2<<endl;
					}
					*/
					if (check()){
						printf("2\n");
						printf("%d %d\n",hh1[i1]+1,hh1[j1]);
						printf("%d %d\n",hh2[i2]+1,hh2[j2]);
						return 0;
					}
					bool f=true;
					c3=0;
					hh3[c3]=0;
					c3++;
					for (int k=1;k<n;k++){
						if (abs(a[k]-a[k-1])!=1 || (k>=2 && ((a[k]-a[k-1]==1 && a[k-1]-a[k-2]==-1) || (a[k]-a[k-1]==-1 && a[k-1]-a[k-2]==1)))){
							hh3[c3]=k;
							c3++;
							if (c3>=27){
								f=false;
								break;
							}
						}
					}
					if (!f){
						continue;
					}
					hh3[c3]=n;
					c3++;
					for (int i3=0;i3<c3;i3++){
						for (int j3=i3+1;j3<c3;j3++){
							if (hh3[i3]==hh3[j3]-1){
								continue;
							}
							//cout<<1<<endl;
							/*
							if (i1==0 && j1==1 && i2==0 && j2==1){
								cout<<i3<<' '<<j3<<endl;
							}
							*/
							jh(hh3[i3],hh3[j3]-1);
							if (check()){
								printf("3\n");
								printf("%d %d\n",hh1[i1]+1,hh1[j1]);
								printf("%d %d\n",hh2[i2]+1,hh2[j2]);
								printf("%d %d\n",hh3[i3]+1,hh3[j3]);
								//output();
								return 0;
							}
							jh(hh3[i3],hh3[j3]-1);
						}
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					for (int i=0;i<n;i++){
						cout<<a[i]<<' ';
					}
					cout<<endl;
					*/
				}
			}
			jh(hh1[i1],hh1[j1]-1);
			/*
			for (int i=0;i<n;i++){
				cout<<a[i]<<' ';
			}
			cout<<endl;
			*/
		}
	}
	//cout<<1<<endl;
	return 0;
}
/*
input:
100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 70 71 72 73 74 26 25 24 23 22
98 99 100
*/


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int inf=2e9+7;
const ll linf=2e18+7;
int a[1007];
int n;
int hh1[27],c1=0,hh2[27],c2=0,hh3[27],c3=0;
int jh(int l,int r){
	for (int i=l;i<=(l+r)/2;i++){
		swap(a[i],a[l+r-i]);
	}
}
bool check(){
	for (int i=0;i<n;i++){
		if (a[i]!=i+1){
			return false;
		}
	}
	return true;
}
void output(){
	for (int i=0;i<n;i++){
		cout<<a[i]<<' '<<i+1<<endl;
	}
}
int main(){
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	scanf("%d",&n);
	for (int i=0;i<n;i++){
		scanf("%d",a+i);
	}
	hh1[c1]=0;
	c1++;
	for (int i=0;i<n;i++){
		//cout<<1<<endl;
		if (i>=1 && (abs(a[i]-a[i-1]))!=1 || (i>=2 && ((a[i]-a[i-1]==1 && a[i-1]-a[i-2]==-1) || (a[i]-a[i-1]==-1 && a[i-1]-a[i-2]==1)))){
			hh1[c1]=i;
			//cout<<i<<endl;
			c1++;
			hh1[c1]=i+1;
			//cout<<i+1<<endl;
			c1++;
			i++;
		}
	}
	hh1[c1]=n;
	c1++;
	//cout<<1<<endl;
	if (check()){
		printf("0\n");
		return 0;
	}
	for (int i1=0;i1<c1;i1++){
		for (int j1=i1+1;j1<c1;j1++){
			if (hh1[i1]==hh1[j1]-1){
				continue;
			}
			jh(hh1[i1],hh1[j1]-1);
			if (check()){
				printf("1\n");
				printf("%d %d\n",hh1[i1]+1,hh1[j1]);
				return 0;
			}
			bool f=true;
			c2=0;
			hh2[c2]=0;
			c2++;
			for (int k=1;k<n;k++){
				if (abs(a[k]-a[k-1])!=1 || (k>=2 && ((a[k]-a[k-1]==1 && a[k-1]-a[k-2]==-1) || (a[k]-a[k-1]==-1 && a[k-1]-a[k-2]==1)))){
					hh2[c2]=k;
					c2++;
					hh2[c2]=k+1;
					c2++;
					if (c2>=27){
						f=false;
						break;
					}
					k++;
				}
			}
			if (!f){
				continue;
			}
			hh2[c2]=n;
			c2++;
			for (int i2=0;i2<c2;i2++){
				for (int j2=i2+1;j2<c2;j2++){
					if (hh2[i2]==hh2[j2]-1){
						continue;
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					if (i1==0 && j1==2){
						cout<<i2<<' '<<j2<<endl;
					}
					*/
					if (check()){
						printf("2\n");
						printf("%d %d\n",hh2[i2]+1,hh2[j2]);
						printf("%d %d\n",hh1[i1]+1,hh1[j1]);
						return 0;
					}
					bool f=true;
					c3=0;
					hh3[c3]=0;
					c3++;
					for (int k=1;k<n;k++){
						if (abs(a[k]-a[k-1])!=1 || (k>=2 && ((a[k]-a[k-1]==1 && a[k-1]-a[k-2]==-1) || (a[k]-a[k-1]==-1 && a[k-1]-a[k-2]==1)))){

							hh3[c3]=k;
							c3++;
							hh3[c3]=k+1;
							c3++;
							if (c3>=27){
								f=false;
								break;
							}
							k++;
						}
					}
					if (!f){
						continue;
					}
					hh3[c3]=n;
					c3++;
					for (int i3=0;i3<c3;i3++){
						for (int j3=i3+1;j3<c3;j3++){
							if (hh3[i3]==hh3[j3]-1){
								continue;
							}
							/*
							if (i1==0 && j1==1 && i2==0 && j2==1){
								cout<<i3<<' '<<j3<<endl;
							}
							*/
							jh(hh3[i3],hh3[j3]-1);
							if (check()){
								printf("3\n");
								printf("%d %d\n",hh3[i3]+1,hh3[j3]);
								printf("%d %d\n",hh2[i2]+1,hh2[j2]);
								printf("%d %d\n",hh1[i1]+1,hh1[j1]);
								//output();
								return 0;
							}
							jh(hh3[i3],hh3[j3]-1);
						}
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					for (int i=0;i<n;i++){
						cout<<a[i]<<' ';
					}
					cout<<endl;
					*/
				}
			}
			jh(hh1[i1],hh1[j1]-1);
			/*
			for (int i=0;i<n;i++){
				cout<<a[i]<<' ';
			}
			cout<<endl;
			*/
		}
	}
	//cout<<1<<endl;
	return 0;
}
/*
input:
100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 70 71 72 73 74 26 25 24 23 22
98 99 100
*/


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int inf=2e9+7;
const ll linf=2e18+7;
int n;
int a[1007];
int hh1[17],c1=0,hh2[17],c2=0,hh3[17],c3=0;
int jh(int l,int r){
	for (int i=l;i<=(l+r)/2;i++){
		swap(a[i],a[l+r-i]);
	}
}
bool check(){
	for (int i=0;i<n;i++){
		if (a[i]!=i+1){
			return false;
		}
	}
	return true;
}
int main(){
	scanf("%d",&n);
	hh1[c1]=0;
	c1++;
	for (int i=0;i<n;i++){
		scanf("%d",a+i);
		if (i>=1 && abs(a[i]-a[i-1])!=1){
			hh1[c1]=i;
			//cout<<i<<endl;
			c1++;
		}
	}
	hh1[c1]=n;
	c1++;
	if (check()){
		printf("0\n");
		return 0;
	}
	for (int i1=0;i1<c1;i1++){
		for (int j1=i1+1;j1<c1;j1++){
			//cout<<i1<<' '<<j1<<endl;
			if (hh1[i1]==hh1[j1]-1){
				continue;
			}
			jh(hh1[i1],hh1[j1]-1);
			if (check()){
				printf("1\n");
				printf("%d %d\n",hh1[i1]+1,hh1[j1]);
				return 0;
			}
			c2=0;
			hh2[c2]=0;
			c2++;
			for (int k=1;k<n;k++){
				if (abs(a[k]-a[k-1])!=1){
					hh2[c2]=k;
					c2++;
					if (c2>=7){
						continue;
					}
				}
			}
			hh2[c2]=n;
			c2++;
			for (int i2=0;i2<c2;i2++){
				for (int j2=i2+1;j2<c2;j2++){
					if (hh2[i2]==hh2[j2]-1){
						continue;
					}
					jh(hh2[i2],hh2[j2]-1);
					//if (i1==2 && j1==4 && i2==1 && j2==)
					if (check()){
						printf("2\n");
						printf("%d %d\n",hh1[i1]+1,hh1[j1]);
						printf("%d %d\n",hh2[i2]+1,hh2[j2]);
						return 0;
					}
					c3=0;
					hh3[c3]=0;
					c3++;
					for (int k=1;k<n;k++){
						if (abs(a[k]-a[k-1])!=1){
							hh3[c3]=k;
							c3++;
							if (c3>=7){
								continue;
							}
						}
					}
					hh3[c3]=n;
					c3++;
					for (int i3=0;i3<c3;i3++){
						for (int j3=i3+1;j3<c3;j3++){
							if (hh3[i3]==hh3[j3]-1){
								continue;
							}
							//cout<<1<<endl;
							/*
							if (i1==0 && j1==1 && i2==0 && j2==1){
								cout<<i3<<' '<<j3<<endl;
							}
							*/
							jh(hh3[i3],hh3[j3]-1);
							if (check()){
								printf("3\n");
								printf("%d %d\n",hh1[i1]+1,hh1[j1]);
								printf("%d %d\n",hh2[i2]+1,hh2[j2]);
								printf("%d %d\n",hh3[i3]+1,hh3[j3]);
								return 0;
							}
							jh(hh3[i3],hh3[j3]-1);
						}
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					for (int i=0;i<n;i++){
						cout<<a[i]<<' ';
					}
					cout<<endl;
					*/
				}
			}
			jh(hh1[i1],hh1[j1]-1);
			/*
			for (int i=0;i<n;i++){
				cout<<a[i]<<' ';
			}
			cout<<endl;
			*/
		}
	}
	return 0;
}
/*
input:
11
1 5 4 3 10 9 8 7 6 2 11
*/


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int inf=2e9+7;
const ll linf=2e18+7;
int a[1007];
int n;
int hh1[27],c1=0,hh2[27],c2=0,hh3[27],c3=0;
int jh(int l,int r){
	for (int i=l;i<=(l+r)/2;i++){
		swap(a[i],a[l+r-i]);
	}
}
bool check(){
	for (int i=0;i<n;i++){
		if (a[i]!=i+1){
			return false;
		}
	}
	return true;
}
void output(){
	for (int i=0;i<n;i++){
		cout<<a[i]<<' '<<i+1<<endl;
	}
}
int main(){
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	scanf("%d",&n);
	for (int i=0;i<n;i++){
		scanf("%d",a+i);
	}
	hh1[c1]=0;
	c1++;
	hh1[c1]=1;
	c1++;
	for (int i=2;i<n;i++){
		//cout<<1<<endl;
		if (abs(a[i]-a[i-1])!=1 || ((a[i]-a[i-1]==1 && a[i-1]-a[i-2]==-1) || (a[i]-a[i-1]==-1 && a[i-1]-a[i-2]==1))){
			hh1[c1]=i;
			//cout<<i<<endl;
			c1++;
			hh1[c1]=i+1;
			//cout<<i+1<<endl;
			c1++;
			i++;
		}
	}
	hh1[c1]=n;
	c1++;
	//cout<<1<<endl;
	if (check()){
		printf("0\n");
		return 0;
	}
	for (int i1=0;i1<c1;i1++){
		for (int j1=i1+1;j1<c1;j1++){
			if (hh1[i1]==hh1[j1]-1){
				continue;
			}
			jh(hh1[i1],hh1[j1]-1);
			if (check()){
				printf("1\n");
				printf("%d %d\n",hh1[i1]+1,hh1[j1]);
				return 0;
			}
			bool f=true;
			c2=0;
			hh2[c2]=0;
			c2++;
			hh2[c2]=1;
			c2++;
			for (int k=2;k<n;k++){
				if (abs(a[k]-a[k-1])!=1 || ((a[k]-a[k-1]==1 && a[k-1]-a[k-2]==-1) || (a[k]-a[k-1]==-1 && a[k-1]-a[k-2]==1))){
					hh2[c2]=k;
					c2++;
					hh2[c2]=k+1;
					c2++;
					if (c2>=27){
						f=false;
						break;
					}
					k++;
				}
			}
			if (!f){
				continue;
			}
			hh2[c2]=n;
			c2++;
			for (int i2=0;i2<c2;i2++){
				for (int j2=i2+1;j2<c2;j2++){
					if (hh2[i2]==hh2[j2]-1){
						continue;
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					if (i1==0 && j1==2){
						cout<<i2<<' '<<j2<<endl;
					}
					*/
					if (check()){
						printf("2\n");
						printf("%d %d\n",hh2[i2]+1,hh2[j2]);
						printf("%d %d\n",hh1[i1]+1,hh1[j1]);
						return 0;
					}
					bool f=true;
					c3=0;
					hh3[c3]=0;
					c3++;
					hh3[c3]=1;
					c3++;
					for (int k=2;k<n;k++){
						if (abs(a[k]-a[k-1])!=1 || ((a[k]-a[k-1]==1 && a[k-1]-a[k-2]==-1) || (a[k]-a[k-1]==-1 && a[k-1]-a[k-2]==1))){
							hh3[c3]=k;
							c3++;
							hh3[c3]=k+1;
							c3++;
							if (c3>=27){
								f=false;
								break;
							}
							k++;
						}
					}
					if (!f){
						continue;
					}
					hh3[c3]=n;
					c3++;
					for (int i3=0;i3<c3;i3++){
						for (int j3=i3+1;j3<c3;j3++){
							if (hh3[i3]==hh3[j3]-1){
								continue;
							}
							/*
							if (i1==0 && j1==1 && i2==0 && j2==1){
								cout<<i3<<' '<<j3<<endl;
							}
							*/
							jh(hh3[i3],hh3[j3]-1);
							if (check()){
								printf("3\n");
								printf("%d %d\n",hh3[i3]+1,hh3[j3]);
								printf("%d %d\n",hh2[i2]+1,hh2[j2]);
								printf("%d %d\n",hh1[i1]+1,hh1[j1]);
								//output();
								return 0;
							}
							jh(hh3[i3],hh3[j3]-1);
						}
					}
					jh(hh2[i2],hh2[j2]-1);
					/*
					for (int i=0;i<n;i++){
						cout<<a[i]<<' ';
					}
					cout<<endl;
					*/
				}
			}
			jh(hh1[i1],hh1[j1]-1);
			/*
			for (int i=0;i<n;i++){
				cout<<a[i]<<' ';
			}
			cout<<endl;
			*/
		}
	}
	//cout<<1<<endl;
	return 0;
}
/*
input:
100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 70 71 72 73 74 26 25 24 23 22
98 99 100
*/


#include<iostream>
#include<queue>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
int vec[26][200008];
int main()
{
	int  n,m,ans,t;
	int mark[28];

	memset(vec, 0, sizeof(int) * 26 * 200008);
	cin >> n;
	string str,cp;
	cin >> cp;
	vec[cp[0] - 'a'][0]++;
	for (int i = 1; i<cp.length(); i++)
	{
		for (int j = 0; j < 26; j++)
			vec[j][i] = vec[j][i - 1];
		vec[cp[i] - 'a'][i]++;
	}
	cin >> m;
	while (m--)
	{
		cin >> str;
		ans = 0;
		t = 0;
		memset(mark, 0, sizeof(int)*28);
		for (int i = 0; i < str.length(); i++)
		{

				mark[str[i] - 'a']++;

		}
		for (int i = 0; i < 26; i++)
		{
			int sn = 0;
			while (vec[i][sn]<mark[i])
			{
				sn++;
			}
			if (sn > ans)
				ans = sn;
		}

		cout << ans+1<<endl;

	}
		return 0;
}


#include<iostream>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;

int main()
{
	int  n,m,ans,t;
	int mark[28];
	cin >> n;
	char *cp = new char[n];
	string str;
	cin >> cp;
	cin >> m;
	while (m--)
	{
		cin >> str;
		ans = 0;
		t = 0;
		memset(mark, 0, sizeof(int)*28);
		for (int i = 0; i < str.length(); i++)
		{

				mark[str[i] - 'a']++;
				t++;

		}
		for (int i = 0; i < n; i++)
		{
			if (mark[cp[i] - 'a'] !=0)
			{
				t--;
				mark[cp[i] - 'a']--;
			}
			if (t == 0)
			{
				ans = i + 1;
				break;
			}
		}
		cout << ans<<endl;

	}
		return 0;
}


#include<iostream>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;

int main()
{
	int  n,m,ans,t;
	int mark[28];
	cin >> n;
	string str,cp;
	cin >> cp;
	cin >> m;
	while (m--)
	{
		cin >> str;
		ans = 0;
		t = 0;
		memset(mark, 0, sizeof(int)*28);
		for (int i = 0; i < str.length(); i++)
		{

				mark[str[i] - 'a']++;
				t++;

		}
		for (int i = 0; i <cp.length(); i++)
		{
			if (mark[cp[i] - 'a'] !=0)
			{
				t--;
				mark[cp[i] - 'a']--;
			}
			if (t == 0)
			{
				ans = i + 1;
				break;
			}
		}

		cout << ans<<endl;

	}
		return 0;
}


#include<iostream>
#include<queue>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
int vec[26][200008];
int main()
{
	int  n,m,ans,t;
	int mark[28];

	memset(vec, 0, sizeof(int) * 26 * 200008);
	cin >> n;
	string str,cp;
	cin >> cp;
	vec[cp[0] - 'a'][0]++;
	for (int i = 1; i<cp.length(); i++)
	{
		for (int j = 0; j < 26; j++)
			vec[j][i] = vec[j][i - 1];
		vec[cp[i] - 'a'][i]++;
	}
	cin >> m;
	while (m--)
	{
		cin >> str;
		ans = 0;
		t = 0;
		memset(mark, 0, sizeof(int)*28);
		for (int i = 0; i < str.length(); i++)
		{

				mark[str[i] - 'a']++;

		}
		for (int i = 0; i < 26; i++)
		{
			int sn = 0;
			sn=lower_bound(vec[i], vec[i] + cp.length(), mark[i])- vec[i];
			if (sn > ans)
				ans = sn;
		}

		cout << ans+1<<endl;

	}
		return 0;
}


#include <iostream>
#include <string>

using namespace std;

struct position {
	int values[26];
	char addition;
	position(int* val, char vale) {
		for (int i = 0; i < 26; ++i) {
			values[i] = val[i];
		}
		addition = vale;
	}
	position() {
		for (int i = 0; i < 26; ++i) {
			values[i] = 0;
		}
		return;
	}
};

int compare(position arPos, int* struct2) { // -1 means it is to the right. 0 means to the left. 1 means it is this value.
	int count = 0;
	bool hasMet = false;
	for (int i = 0; i < 26; ++i) {
		if (arPos.values[i] < struct2[i]) {
			return -1;
		}
		else if (arPos.values[i] == struct2[i] && char(i + 'a') == arPos.addition) {
			count++;
			hasMet = true;
		}
		else if (arPos.values[i] >= struct2[i]) {
			count++;
		}
	}
	if (count == 26 && hasMet) {
		return 1;
	}
	return 0;
}

int* countString(string value) {
	int* args = new int[26];
	for (int i = 0; i < 26; ++i) {
		args[i] = 0;
	}
	for (int i = 0; i < value.length(); ++i) {
		args[value[i] - 'a']++;
	}
	return args;
}

int binarySearchPosition(position* arr, int value[26], int size) {
	int left = 0;
	int right = size;
	int mid = size / 2;
	int i = 0;
	while (i++ < size) {

		int dir = compare(arr[mid], value);
		if (dir == 1) {
			return mid;
		}
		else if (dir == -1) {
			left = mid + 1;
			mid = (left + right) / 2;
		}
		else {
			right = mid - 1;
			mid = (left + right) / 2;
		}
	}
	cout << "error element not found";
	return -1;
}

int* copy(int* alphabet1, int* alphabet2) {
	for (int i = 0; i < 26; ++i) {
		alphabet2[i] = alphabet1[i];
	}
	return alphabet2;
}

int main() {
	int n, m;
	string s;
	cin >> n >> s >> m;
	position* arr = new position[n];
	arr[0].values[s[0] - 'a'] = 1;
	arr[0].addition = s[0];
	for (int i = 1; i < n; ++i) {
		copy(arr[i - 1].values, arr[i].values);
		arr[i].values[s[i] - 'a']++;
		arr[i].addition = s[i];
	}
	string val;
	for (int i = 0; i < m; ++i) {
		cin >> val;
		int* mapVal = countString(val);
		cout << binarySearchPosition(arr, mapVal, n) + 1 << "\n";
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
char zifu[200010];
char s[27][200010];
int freq[27];
char f[200010];
int max(int a,int b)
{
	return a>b?a:b;
}
int main()
{
	int n;
	cin>>n;
	cin>>zifu;
	for(int i=0;i<n;i++)
	{
		char c=zifu[i];
		s[c-'a'][++freq[c-'a']]=i+1;
	}
	int m;
	cin>>m;
	int ans;
	while(m--)
	{
		int need[27]={0};
		ans=0;
		scanf("%s",f);
		int len=strlen(f);
		if(len>n)
		{
			cout<<n<<endl;
			continue;
		}
		for(int i=0;i<len;i++)
		{
			need[f[i]-'a']++;
			if(need[f[i]-'a']>freq[f[i]-'a'])
			{
				ans=n;
				break;
			}
			ans=max(ans,s[f[i]-'a'][need[f[i]-'a']]);
		}
		cout<<ans<<endl;
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
char zifu[200010];
int s[27][200010];
int freq[27];
char f[200010];
int max(int a,int b)
{
	return a>b?a:b;
}
int main()
{
	int n;
	cin>>n;
	cin>>zifu;
	for(int i=0;i<n;i++)
	{
		char c=zifu[i];
		s[c-'a'][++freq[c-'a']]=i+1;
	}
	int m;
	cin>>m;
	int ans;
	while(m--)
	{
		int need[27]={0};
		ans=0;
		scanf("%s",f);
		int len=strlen(f);
		if(len>n)
		{
			cout<<n<<endl;
			continue;
		}
		for(int i=0;i<len;i++)
		{
			need[f[i]-'a']++;
			if(need[f[i]-'a']>freq[f[i]-'a'])
			{
				ans=n;
				break;
			}
			ans=max(ans,s[f[i]-'a'][need[f[i]-'a']]);
		}
		cout<<ans<<endl;
	}

	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<limits.h>
#include<queue>
#include<math.h>
#define Inf 0x3f3f3f3f
#define ll long long
using namespace std;

char s[200005],r[200005];
int num[26][200005];
int k[27],vis[27];

int main()
{
	int t,n,m;
	char rubbish[10];
	scanf("%d",&n);
	gets(rubbish);
	gets(s);
	scanf("%d",&m);
	gets(rubbish);
	memset(k,0,sizeof(k));
	memset(num,0,sizeof(num));
	for (int i=0;i<n;i++)
	{
		num[s[i]-'a'][k[s[i]-'a']]=i;
		k[s[i]-'a']++;
	}/*
	for (int i=0;i<26;i++)
	{
		printf("%c:",i+'a');
		for (int j=0;j<k[i];j++) printf("%d ",num[i][j]);
		printf("\n");
	}*/
	while (m--)
	{
    	memset(vis,0,sizeof(vis));
		gets(r);
		int l=strlen(r);
		int ans=0;
		for (int i=0;i<l;i++)
		{
			vis[r[i]-'a']++;
		}
		for (int i=0;i<26;i++)
		{
			if (vis[i]&&num[i][vis[i]-1]>ans) ans=num[i][vis[i]-1];
		}
		printf("%d\n",ans+1);

	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<limits.h>
#include<queue>
#include<math.h>
#define Inf 0x3f3f3f3f
#define ll long long
using namespace std;

int letter[30];
char s[200005],r[200005];

int main()
{
	int t,n,m;
	char rubbish[10];
	scanf("%d",&n);
	gets(rubbish);
	gets(s);
	scanf("%d",&m);
	gets(rubbish);
	while (m--)
	{
		memset(letter,0,sizeof(letter));
		gets(r);
		int l=strlen(r);
		for (int i=0;i<l;i++) letter[r[i]-'a']++;
		int ans=0;
		while (l)
		{
			//printf("%d %d\n",l,ans);
			if (letter[s[ans]-'a'])
			{
				l--;
				letter[s[ans]-'a']--;
			}
			ans++;
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<limits.h>
#include<queue>
#include<math.h>
#define Inf 0x3f3f3f3f
#define ll long long
using namespace std;

int letter[30];
char s[200005],r[200005];

int main()
{
	int t,n,m;
	char rubbish[10];
	scanf("%d",&n);
	gets(rubbish);
	gets(s);
	scanf("%d",&m);
	gets(rubbish);
	memset(letter,0,sizeof(letter));
	while (m--)
	{

		gets(r);
		int l=strlen(r);
		for (int i=0;i<l;i++) letter[r[i]-'a']++;

		int ans=0;
		while (l)
		{
			//printf("%d %d\n",l,ans);
			if (letter[s[ans]-'a'])
			{
				l--;
				letter[s[ans]-'a']--;
			}
			ans++;
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<limits.h>
#include<queue>
#include<math.h>
#define Inf 0x3f3f3f3f
#define ll long long
using namespace std;

char s[200005],r[200005];
int num[26][200005];
int k[26],vis[26];

int main()
{
	int t,n,m;
	char rubbish[10];
	scanf("%d",&n);
	gets(rubbish);
	gets(s);
	scanf("%d",&m);
	gets(rubbish);
	memset(k,0,sizeof(k));
	memset(num,0,sizeof(num));
	for (int i=0;i<n;i++)
	{
		num[s[i]-'a'][k[s[i]-'a']]=i;
		k[s[i]-'a']++;
	}
	while (m--)
	{
    	memset(vis,0,sizeof(vis));
		gets(r);
		int l=strlen(r);
		int ans=0;
		for (int i=0;i<l;i++)
		{
			vis[r[i]-'a']++;
		}
		for (int i=0;i<26;i++)
		{
			if (num[i][vis[i]-1]>ans) ans=num[i][vis[i]-1];
		}
		printf("%d\n",ans+1);

	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<string.h>
using namespace std;
#define maxn 1000100
typedef long long ll;

vector<int>v[30];
int num[30];
int n,m;
string s,t;

int main()
{
	cin>>n;
	cin>>s;
	for(int i=0;i<n;i++)
	{
		int t=s[i]-'a';
		v[t].push_back(i+1);
	}
	cin>>m;
	while(m--)
	{
		cin>>t;
		memset(num,0,sizeof(num));
		int ma=1;
		int l=t.size();
		for(int i=0;i<l;i++)
		{
			int tt=t[i]-'a';
			ma=max(ma,v[tt][num[tt]]);
			num[tt]++;
		}
		cout<<ma;
		if(m>0)cout<<endl;
	}

	return 0;
}


#include<stdio.h>
#include<string.h>
const int maxn=200005;
char s[maxn],str[maxn];
int rec[maxn][26]={0},num[26];
bool judge(int pos)
{for(int i=0;i<26;i++)
if(num[i]>rec[pos][i])
	return 0;
return 1;}
int main()
{
int n,m;
scanf("%d",&n);
scanf("%s",s+1);
for(int i=1;i<=n;i++)
{for(int j=0;j<26;j++)
rec[i][j]=rec[i-1][j];
rec[i][s[i]-'a']++;}
scanf("%d",&m);
for(int j=0;j<m;j++)
	{scanf("%s",str);
	for(int i=0;i<26;i++)
	num[i]=0;
	for(int i=0;i<strlen(str);i++)
		num[str[i]-'a']++;
	int l=0,r=n,mid;
	while(l<r-1)
	{mid=(l+r)/2;
	if(judge(mid))
		r=mid;
	else
		l=mid;}
	for(int i=l;i<=r;i++)
		if(judge(i))
		{printf("%d\n",i);break;}}}


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

using namespace std;

vector<int> v[27];

int main(){
	int n;
	cin >> n;

	string s;
	cin >> s;

	for(int i = 0; i < s.length(); i++){
		int x = s[i] - 97;

		v[x].push_back(i + 1);
	}

	int m;
	cin >> m;

	int a[27];
	string t;

	for(int i = 0; i < m; i++){
		for(int j = 0; j < 27; j++)
			a[j] = 0;

		cin >> t;
		for(int j = 0; j < t.length(); j++){
			int x = t[j] - 97;

			a[x] ++;
		}
		int mxl = 0;

		for(int j = 0; j < 27; j++){
			if(a[j] > 0){
				mxl = max(v[j][a[j] - 1], mxl);
			}
		}

		cout << mxl << endl;
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<string.h>
#include<map>
using namespace std;
char s[200010];
char f[200010];
int max(int a,int b)
{
	return a>b?a:b;
}
int main()
{
	int n;
	cin>>n;
	scanf("%s",s);
	int m;
	cin>>m;
	int ans=0;
	int t=1;
	while(m--)
	{
		ans=0;
		scanf("%s",f);
		int len=strlen(f);
		for(int i=0;i<len;i++)
		{
			char* temp=strchr(s,f[i]);
			ans=max(ans,temp-s+1);
		}
		cout<<ans<<endl;
	}

	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2e5+10;
int a[26],c[26][maxn];
char st[maxn];
int main()
{
	int n;
	scanf("%d %s",&n,st);
	for(int i=0;i<n;i++)
	{
		int x=st[i]-'a';
		a[x]++;
		c[x][a[x]]=i+1;
	}
	int m;
	scanf("%d",&m);
	while(m--)
	{
		int ans=0;
		for(int i=0;i<26;i++)
			a[i]=0;
		scanf("%s",st);
		int len=strlen(st);
		for(int i=0;i<len;i++)
		{
			int x=st[i]-'a';
			a[x]++;
			ans=max(ans,c[x][a[x]]);
		}
		printf("%d\n",ans);
	}
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=2e5;
int a[26][maxn],b[26],c[26];
int main()
{
	int n,m;
	scanf("%d",&n);
	memset(c,0,sizeof(c));
	getchar();
	for(int i=0;i<n;i++)
	{
		char x;
		scanf("%c",&x);
		c[x-'a']++;
		a[x-'a'][c[x-'a']]=i+1;
		//printf("%c",x);
	}
	//printf("\n");
	scanf("%d",&m);
	getchar();
	while(m--)
	{
		memset(b,0,sizeof(b));
		char x;
		int ma=0;
		while(scanf("%c",&x))
		{
			if(x>'z'||x<'a')
				break;
			b[x-'a']++;
			ma=max(ma,a[x-'a'][b[x-'a']]);
		}
		printf("%d\n",ma);
	}
	return 0;
}


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

using namespace std;

int main()
{
    int n = 0, m = 0, l = 0;
    string s1 = "", s2 = "";
    cin >> n >> s1 >> n;
    while (cin >> s2) {
        sort(s2.begin(), s2.end());
        s2 += ' ';
        n = s2.size();
        m = s1.find(s2[0]);
        for (int i = 1; i < n; i++) {
            if (s2[i] != s2[i - 1]) {
                if (m == -1) {
                    m = s1.find(s2[i - 1]);
                }
                if (m > l) {
                    l = m;
                }
                m = -1;
            }
            else {
                m = s1.find(s2[i], m + 1);
            }
        }
        cout << l + 1 << endl;
        l = 0;
    }
}


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

using namespace std;

int main()
{
    int n = 0, m = 26, l = -1, k = 0;
    string abc = "abcdefghijklmnopqrstuvwxyz", s = "";
    vector<vector<int>> v(m);
    cin >> n >> s;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            l = s.find(abc[i], l + 1);
            if (l > -1) {
                v[i].push_back(l);
            }
            else {
                l = -1;
                break;
            }
        }
    }
    cin >> n;
    m = 0;
    while (cin >> s) {
        sort(s.begin(), s.end());
        s += ' ';
        n = s.size();
        for (int i = 1; i < n; i++) {
            if (s[i] == s[i - 1]) {
                m++;
            }
            else {
                l = v[int(s[i - 1] - 97)][m];
                m = 0;
                if (l > k) {
                    k = l;
                }
            }
        }
        cout << k + 1 << endl;
        k = 0;
    }
}


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

using namespace std;

int main()
{
    int n = 0, m = 0, l = 0;
    string s1 = "", s2 = "";
    cin >> n >> s1 >> n;
    while (cin >> s2) {
        sort(s2.begin(), s2.end());
        s2 += "  ";
        n = s2.size();
        m = s1.find(s2[0]);
        for (int i = 1; i < n; i++) {
            if (s2[i] != s2[i - 1]) {
                if (m > l) {
                    l = m;
                }
                m = -1;
            }
            m = s1.find(s2[i], m + 1);
        }
        cout << l + 1 << endl;
        l = 0;
    }
}


from collections import Counter
n=int(input())
s=input()
q=int(input())
d=dict()
j=0
for i in range(97,123):
    d[chr(i)]=None
for c in s:
    if(d[c]==None):
        d[c]=[j]
    else:
        d[c]=d[c]+[j]
    j=j+1
#print(d)
for i in range(0,q):
    s1=input()
    n1=len(s1)
    d1=Counter(s1)
    m=-1
    for k in d1.keys():
        if(len(d[k])>0):
            if(d[k][d1[k]-1]>m):
                m=d[k][d1[k]-1]
        else:
            if(d[k]>m):
                m=d[k]
        if(m==n1):
            break
    print(m+1)


from collections import Counter
n=int(input())
s=input()
q=int(input())
l=list()
d=dict()
j=0
for c in s:
    if(d.get(c)==None):
        d[c]=[j]
    else:
        d[c]=d[c]+[j]
    j=j+1
p=d.copy()
for i in range(0,q):
    s1=input()
    d1=Counter(s1)
    m=-1
    for k in d1.keys():
        if(len(d[k])>0):
            if(d[k][d1[k]-1]>m):
                m=d[k][d1[k]-1]
        else:
            if(d[k]>m):
                m=d[k]
    print(m+1)


n=int(input())
s=input()
q=int(input())
l=list()
for _ in range(0,q):
    l.append(input())
d=dict()
j=0
for c in s:
    d[c]=None
for c in s:
    if(d[c]==None):
        d[c]=[j]
    else:
        d[c]=d[c]+[j]
    j=j+1
p=d.copy()
for i in range(0,q):
    s1=l[i]
    m=-1
    d=p.copy()
    #print(p)
    #print(s1)
    for j in s1:
        if(len(d[j])>1):
            #print("start",d[j])
            f=d[j]
            d[j]=d[j][1:]
           # print("end",d[j])
        else:
            f=d[j]
        f=f[0]
        #print(j,f)
        if(f>m):
            m=f
    print(m+1)


from collections import Counter
n=int(input())
s=input()
q=int(input())
d=dict()
j=0
for i in range(97,123):
    d[chr(i)]=[]
for c in s:
    if(d[c]==[]):
        d[c]=[j]
        #print("1sst",d[c])
    else:
        #print(d[c],c)
        d[c].append(j)
    j=j+1
for i in range(0,q):
    s1=input()
    d1=Counter(s1)
    m=-1
    for k in d1.keys():
        if(len(d[k])>0):
            if(d[k][d1[k]-1]>m):
                m=d[k][d1[k]-1]
        else:
            if(d[k]>m):
                m=d[k]
    print(m+1)


import sys
#sys.stdin = open('in', 'r')
n = int(input())
s = input()
m = int(input())
inds = [[] for _ in range(27)]
for i in range(n):
    ind = ord(s[i]) & 31
    inds[ind].append(i + 1)
for mi in range(m):
    ti = input()
    cnt = [0]*27
    for c in ti:
        cnt[ord(c) & 31] += 1
    res = 0
    for j in range(1, 27):
        if cnt[j] > 0:
            res = max(res, inds[j][cnt[j]-1])
    sys.stdout.write(f'{res}\n')

#sys.stdout.write('YES\n')

#sys.stdout.write(f'{y1} {x1} {y2} {x2}\n')


import sys
from collections import Counter
input = sys.stdin.readline

def main():
    _ = int(input())
    s = input().rstrip()
    m = int(input())

    for i in range(m):
        t = Counter(input().rstrip())
        for ni, c in enumerate(s):
            t[c] -= 1
            if t - Counter() == Counter():
                print(ni + 1)
                break

if __name__ == '__main__':
    main()


# import sys
from collections import Counter, defaultdict
# input = sys.stdin.readline

def main():
    _ = int(input())
    s = input().rstrip()

    poss = defaultdict(list)
    for ic, c in enumerate(s):
        poss[c].append(ic)

    m = int(input())

    for i in range(m):
        t = Counter(input().rstrip())
        maxpos = -1
        for c, nc in t.items():
            cpos = poss[c][nc - 1]
            if cpos > maxpos:
                maxpos = cpos
        print(maxpos + 1)

if __name__ == '__main__':
    main()


from collections import Counter

def main():
    _ = int(input())
    s = input()
    m = int(input())

    for i in range(m):
        t = Counter(input())
        for ni, c in enumerate(s):
            if t[c] > 0:
                t[c] -= 1
            if t - Counter() == Counter():
                print(ni + 1)
                break

if __name__ == '__main__':
    main()


import sys
from collections import Counter
input = sys.stdin.readline

def main():
    _ = int(input())
    s = input()
    m = int(input())

    for i in range(m):
        t = Counter(input())
        for ni, c in enumerate(s):
            t[c] -= 1
            if t - Counter() == Counter():
                print(ni + 1)
                break

if __name__ == '__main__':
    main()


n = int(input())

s = input()
d = {}

for i in range(n):
    if s[i] in d:
        d[s[i]].append(i)
    else:
        d[s[i]] = [i]

#print(d)
r = int(input())
for i in range(r):
        mk = []
        t = input()
        j = 0
        k = {}
        for j in t:
            if j in k :
                k[j] = k[j]+1
            else:
                k[j] = 1

        for key in k.keys():
            x = k[key]
            xx = sorted(d[key])
            mk.append(xx[x-1]+1)
        print(max(mk))


n = int(input())

s = input()
d = {}

for i in range(n):
    if s[i] in d:
        d[s[i]].append(i)
    else:
        d[s[i]] = [i]

#print(d)
r = int(input())
for i in range(r):
        mk = []
        t = input()
        j = 0
        k = {}
        for j in t:
            if j in k :
                k[j] = k[j]+1
            else:
                k[j] = 1
        a = -1
        for key in k.keys():
            x = k[key]
            xx = d[key]
            if xx[x-1]+1 > a :
                a = xx[x-1]+1

        print(a)


m = int(input())
s = input()
x = int(input())
for i in range(x):
	p = input()
	c = len(p)
	t = [0 for x in range(26)]
	for y in p:
		index = ord(y) - 97
		t[index] += 1
	count = 0
	for y in s:
		index = ord(y) - 97
		count += 1
		if t[index] > 0:
			c -= 1
			t[index] -= 1
		else:
			continue
		if c == 0:
			print(count)
			break


m = int(input())
s = input()
x = int(input())
l = [[] for i in range(26)]
for i, c in enumerate(s):
	index = ord(c) - 97
	l[index].append(i + 1)
for _ in range(x):
	p = input()
	t = [0 for x in range(26)]
	for c in p:
		index = ord(c) - 97
		t[index] += 1
	ans = 1
	for i, num in enumerate(t):
		if num != 0:
			ans = max(ans, l[i][num - 1])
	print(ans)


# -*- coding: utf-8 -*-
# @Date    : 2019-06-30 20:25:45
# @Author  : raj lath (oorja.halt@gmail.com)
# @Link    : link
# @Version : 1.0.0

import sys
sys.setrecursionlimit(10**5+1)

inf     =  int(10 ** 20)
max_val =  inf
min_val = -inf

RW  = lambda : sys.stdin.readline().strip()
RI  = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]

from collections import OrderedDict
import copy

def get_prefix(dic, word):
    need = 0
    #print(dic)
    for i in word:
        if i in  dic and len(dic[i]) > 0:
            need = max(need, dic[i].pop(0))
            #has_list[i].pop(0)
        else:
            continue
    return need

lens = RI()
has  = RW()
has_list = OrderedDict()
for i, v  in enumerate(has):
    if v in has_list:
        has_list[v].append(i)
    else:
        has_list[v] = []
        has_list[v].append(i + 1)

nb_friends = RI()
for i in range(nb_friends):
    curr = input()
    dic =  copy.deepcopy(has_list)
    print(get_prefix(dic, curr))


# -*- coding: utf-8 -*-
# @Date    : 2019-06-30 20:25:45
# @Author  : raj lath (oorja.halt@gmail.com)
# @Link    : link
# @Version : 1.0.0

import sys
sys.setrecursionlimit(10**5+1)

inf     =  int(10 ** 20)
max_val =  inf
min_val = -inf

RW  = lambda : sys.stdin.readline().strip()
RI  = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]

from collections import OrderedDict

lens = RI()
has  = RW()
has_list = OrderedDict()
for i, v  in enumerate(has):
    if v in has_list:
        has_list[v].append(i)
    else:
        has_list[v] = []
        has_list[v].append(i + 1)
nb_friends = RI()
for i in range(nb_friends):
    curr = input()
    need = 0
    for i in curr:
        if i in  has_list:
            need = max(need, has_list[i][0])
        else:
            continue
    print(need)


# -*- coding: utf-8 -*-
# @Date    : 2019-06-30 20:25:45
# @Author  : raj lath (oorja.halt@gmail.com)
# @Link    : link
# @Version : 1.0.0

import sys
sys.setrecursionlimit(10**5+1)

inf     =  int(10 ** 20)
max_val =  inf
min_val = -inf

RW  = lambda : sys.stdin.readline().strip()
RI  = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]

from collections import OrderedDict
import copy

def get_prefix(dic, word):
    need = 0
    for i in word:
        need = max(need, dic[i].pop(0))
    return need

lens = RI()
has  = RW()
has_list = OrderedDict()
for i, v  in enumerate(has):
    if v in has_list:
        has_list[v].append(i + 1)
    else:
        has_list[v] = []
        has_list[v].append(i + 1)

nb_friends = RI()
for i in range(nb_friends):
    curr = input()
    dic =  copy.deepcopy(has_list)
    print(get_prefix(dic, curr))


# -*- coding: utf-8 -*-
# @Date    : 2019-07-01 07:44:43
# @Author  : raj lath (oorja.halt@gmail.com)
# @Link    : link
# @Version : 1.0.0

import sys
sys.setrecursionlimit(10**5+1)

inf     =  int(10 ** 20)
max_val =  inf
min_val = -inf

RW  = lambda : sys.stdin.readline().strip()
RI  = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]

from collections import defaultdict
has = defaultdict(list)
lens = RI()
given = input()
for i, v in enumerate(given):
    has[v].append(i + 1)
nb_word = RI()
for i in range(nb_word):
    answer = 0
    current = input()
    dicts = defaultdict(int)
    for i,v in enumerate(current):
        dicts[v] += 1
        answer = (max(answer, has[v][dicts[v] - 1] ))
    print(answer)


from sys import stdin
from collections import defaultdict
input = stdin.readline
for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    for i in range(n):
        if a[i] != b[i]:
            i_end = i
            while i_end < n-1 and b[i_end] <= b[i+1]:
                i_end += 1
            if i_end == i:
                print('NO')
                break
            else:
                i_end = i
                d = defaultdict(int)
                for j in range(i, i_end+1):
                    d[b[j]] += 1
                for c in a[i:]:
                    i_end += 1
                    try:
                        d[c] -= 1
                    except:
                        pass
                    if max(d.values()) == 0:
                        break
                a = a[:i] + sorted(a[i:i_end+1]) + a[i_end+1:]
    else:
        print('YES')


from sys import stdin
from collections import defaultdict
input = stdin.readline
n = int(input())
s = input().rstrip()
m = int(input())
t = [input().rstrip() for _ in range(m)]
for name in t:
    ans = 0
    for c in s:
        ans += 1
        if c in name:
            i = name.index(c)
            name = name[:i] + name[i+1:]
        if not name:
            print(ans)
            break


n = int(input())
s = input().rstrip()
m = int(input())
t = [input().rstrip() for _ in range(m)]
for name in t:
    d = {b: name.count(b) for b in set(name)}
    ans = 0
    for c in s:
        ans += 1
        if c in d:
            d[c] -= 1
            if max(set(d.values())) == 0:
                print(ans)
                break


from sys import stdin
from collections import defaultdict
input = stdin.readline
n = int(input())
s = input().rstrip()
m = int(input())
t = [input().rstrip() for _ in range(m)]
for name in t:
    d = defaultdict(int)
    for c in name:
        d[c] += 1
    ans = 0
    for c in s:
        ans += 1
        try:
            d[c] -= 1
        except:
            pass
        if max(d.values()) == 0:
            print(ans)
            break


from sys import stdin
#from collections import defaultdict
input = stdin.readline
n = int(input())
s = input()
m = int(input())
t = [input() for _ in range(m)]
for name in t:
    ans = 0
    for c in s:
        ans += 1
        if c in name:
            i = name.index(c)
            name = name[:i] + name[i+1:]
        if not name:
            print(ans)
            break


from sys import stdin
from collections import defaultdict
input = stdin.readline
n = int(input())
s = input()
ds = defaultdict(list)
for i in range(n):
    ds[s[i]].append(i)
m = int(input())
t = [input()[:-1] for _ in range(m)]
for name in t:
    d = defaultdict(int)
    for c in name:
        d[c] += 1
    m = 0
    for k, v in d.items():
        i = ds[k][v-1]
        if i > m:
            m = i
    print(m+1)


from sys import stdin
input = stdin.readline
n = int(input())
s = input().rstrip()
m = int(input())
t = [input().rstrip() for _ in range(m)]
for name in t:
    d = {b: name.count(b) for b in set(name)}
    ans = 0
    for c in s:
        ans += 1
        if c in d:
            d[c] -= 1
            if max(d.values()) == 0:
                print(ans)
                break


from collections import Counter
n = int(input())
s = input()
m = int(input())
for i in range(m):
	ss = s
	a = input()
	x = []
	for j in list(a):
		idx = ss.find(j)
		x.append(idx)
		ss = ss[:idx] + "$" + ss[idx+1:]
	print (max(x)+1)


from collections import Counter
n = int(input())
s = input()
alp = [[0] for i in range(26)]
for i, j in enumerate(list(s)):
	b = ord(j)-97
	alp[b].append(i)

m = int(input())
for i in range(m):
	a = input()
	x = []
	cntr = [0 for ii in range(26)]
	for j in list(a):
		y = ord(j) - 97
		cntr[y] += 1
	ans = 0
	for j in range(26):
		ans = max(ans, alp[j][cntr[j]]+1)
	print (ans)


from collections import Counter
n = int(input())
s = input()
m = int(input())
for i in range(m):
	ss = s
	a = input()
	cnta = len(a)
	x = []
	for j in list(a):
		idx = s.find(j)
		x.append(idx)
		ss = ss[:idx] + "$" + ss[idx+1:]
	print (max(x)+1)


'''def foo(p,j,s):
	while(s[p]!=j and p<len(s)):
		p=p+1
	return p'''

n=int(input())
s=input()
s=list(s)
t=int(input())
'''for i in range(0,t):
	l=[0]*n
	mx=0
	s1=input()
	s1=list(s1)
	for j in s1:
		if(j in s):
			k=0
			p1=foo(k,j,s)
			if(l[p1]==1):
				while(l[p1]==1):
					p1=foo(p1+1,j,s)
					#puts("ok")
					#print("ok")
			l[p1]=1
			if(p1>mx):
				mx=p1
		#print(j)

	print(mx+1)'''
d=dict()
for i in range(0,n):
	if(s[i] not in d):
		d[s[i]]=[i]
	else:
		d[s[i]].append(i)

#print(d)
for i in range(0,t):
	s1=input()
	s1=list(s1)
	mx=0
	d1=dict()
	for j in range(0,len(s1)):
		#print(s1[j])
		if(s1[j] not in d1):
			d1[s1[j]]=0
		else:
			d1[s1[j]]+=1
	#print(d1)
	for j in d1.keys():
		x=d1[j]
		p=d[j][x]
		if(p>mx):
			mx=p
	print(mx+1)


def foo(p,j,s):
	while(s[p]!=j and p<len(s)):
		p=p+1
	return p

n=int(input())
s=input()
s=list(s)
t=int(input())
for i in range(0,t):
	l=[0]*n
	mx=0
	s1=input()
	s1=list(s1)
	for j in s1:
		if(j in s):
			k=0
			p1=foo(k,j,s)
			if(l[p1]==1):
				while(l[p1]==1):
					p1=foo(p1+1,j,s)
					#puts("ok")
					#print("ok")
			l[p1]=1
			if(p1>mx):
				mx=p1
		#print(j)

	print(mx+1)


n=int(input())
s=input()
m=int(input())
d=[[] for i in range(26)]
for i in range(n):
   d[ord(s[i])-ord('a')].append(i+1)
for i in range(m):
   w=input()
   dd=[0]*26
   for x in w:
      dd[ord(x)-ord('a')]+=1
   c=0
   for x in w:
      c=max(c,d[ord(x)-ord('a')][dd[ord(x)-ord('a')]-1])
   print(c)


n=int(input())
s=input()
m=int(input())
for i in range(m):
   w=list(input())
   l=len(w)
   c=0
   ss=[]
   for i in s:
      if len(ss)==l:
         break
      elif i in w:
         ss.append(i)
         w.remove(i)
         c+=1
      elif i in ss:
         c+=1
      else:
         c+=1
         continue
   print(c)


n = int(input())
word = input()
for i in range(int(input())):
    pre = input()
    d,e = {},{}
    for i in pre:
        if i not in e:
            e[i] = 0
        if i in d:
            d[i]+=1
        else:
            d[i] = 1
    ans = ''
    for i in word:
        if d==e:
            break
        if i in d:
            if d[i]==0:
                ans+=i
            else:
                d[i]-=1
                ans+=i
        else:
            ans+=i
    print(len(ans))


n = int(input())
word = input()
for i in range(int(input())):
    d = {}
    c = []
    e = {}
    pre = input()
    for i in pre:
        if i in d:
            d[i]+=1
        else:
            d[i] = 1
    for i in word:
        if i not in d:
            c.append(i)
        else:
            if i not in e:
                e[i] = 1
                if d==e:
                    break
            else:
                if e[i]==d[i]:
                    c.append(i)
                else:
                    e[i]+=1
                    if d==e:
                        break
    print(len(c)+len(pre))


from collections import defaultdict
n = int(input())
word = input()
d = defaultdict(list)
for i in range(n):
    d[word[i]].append(i)
print(d)
for i in range(int(input())):
    pre = input()
    c = {}
    for i in pre:
        if i not in c:
            c[i] = 1
        else:
            c[i]+=1
    f = -1
    for i in c:
        t = d[i]
        t = t[c[i]-1]
        f = max(f,t)
    print(f+1)


from collections import defaultdict
n = int(input())
word = input()
d = defaultdict(list)
for i in range(n):
    d[word[i]].append(i)
# print(d)
for i in range(int(input())):
    pre = input()
    c = {}
    for i in pre:
        if i not in c:
            c[i] = 1
        else:
            c[i]+=1
    f = -1
    for i in c:
        t = d[i]
        t = t[c[i]-1]
        f = max(f,t)
    print(f+1)


def readInt():
	return int(input())

def readInts():
	return [int(x) for x in input().split()]

N = readInt()
S = input()
M = readInt()

idx = {}

for i, c in enumerate(S):
	if c not in idx:
		idx[c] = []
	idx[c].append(i)

print(idx)

def countChars(s):
	result = {}
	for c in s:
		result[c] = result.get(c, 0) + 1
	return result

for i in range(M):
	name = input()
	counts = countChars(name)
	lastIdx = 0
	# print("Processing {}".format(name))
	for c in counts:
		num = counts[c]
		# print("Need ", num, " ", c)
		pos = idx[c][num - 1]
		lastIdx = max(lastIdx, pos)
	print(lastIdx + 1)


def readInt():
	return int(input())

def readInts():
	return [int(x) for x in input().split()]

N = readInt()
S = input()
M = readInt()

idx = {}

for i, c in enumerate(S):
	if c not in idx:
		idx[c] = []
	idx[c].append(i)

def countChars(s):
	result = {}
	for c in s:
		result[c] = result.get(c, 0) + 1
	return result

for i in range(M):
	name = input()
	counts = countChars(name)
	lastIdx = 0
	# print("Processing {}".format(name))
	for c in counts:
		num = counts[c]
		# print("Need ", num, " ", c)
		pos = idx[c][num - 1]
		lastIdx = max(lastIdx, pos)
	print(lastIdx + 1)


"""
B. Letters Shop
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The letters shop showcase is a string s, consisting of n lowercase Latin
letters. As the name tells, letters are sold in the shop.

Letters are sold one by one from the leftmost to the rightmost. Any customer
can only buy some prefix of letters from the string s.

There are m friends, the i-th of them is named ti. Each of them is planning to
estimate the following value: how many letters (the length of the shortest
prefix) would s/he need to buy if s/he wanted to construct her/his name of
bought letters. The name can be constructed if each letter is presented in the
equal or greater amount.

For example, for s="arrayhead" and ti="arya" 5 letters have to be bought
("arrayhead").
For example, for s="arrayhead" and ti="harry" 6 letters have to be bought
("arrayhead").
For example, for s="arrayhead" and ti="ray" 5 letters have to be bought
("arrayhead").
For example, for s="arrayhead" and ti="r" 2 letters have to be bought
("arrayhead").
For example, for s="arrayhead" and ti="areahydra" all 9 letters have to be
bought ("arrayhead").
It is guaranteed that every friend can construct her/his name using the letters
from the string s.

Note that the values for friends are independent, friends are only estimating
them but not actually buying the letters.

Input
The first line contains one integer n (1≤n≤2⋅105) — the length of showcase
string s.

The second line contains string s, consisting of exactly n lowercase Latin
letters.

The third line contains one integer m (1≤m≤5⋅104) — the number of friends.

The i-th of the next m lines contains ti (1≤|ti|≤2⋅105) — the name of the i-th
friend.

It is guaranteed that ∑i=1m|ti|≤2⋅105.

Output
For each friend print the length of the shortest prefix of letters from s s/he
would need to buy to be able to construct her/his name of them. The name can be
constructed if each letter is presented in the equal or greater amount.

It is guaranteed that every friend can construct her/his name using the letters
from the string s.

Example
"""
from collections import Counter
def make_dict(s):
    res=dict()
    count_dict = dict()
    for ind, char in enumerate(s):
        count = count_dict.get(char, 0)
        count=count+1
        count_dict[char]=count
        key=char*count
        res[key] = ind
    return res
def find_least(s_dict, t):
    char_counts = Counter(t)
    ind_ls=[]
    for key, val in char_counts.items():
        new_key = key*val
        ind_ls.append(s_dict[new_key])
    return max(ind_ls)+1

if __name__ == "__main__":
    N = int(input())
    s = str(input())
    m = int(input())
    s_dict = make_dict(s)
    for i in range(m):
        print(find_least(s_dict, str(input())))


"""
B. Letters Shop
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The letters shop showcase is a string s, consisting of n lowercase Latin
letters. As the name tells, letters are sold in the shop.

Letters are sold one by one from the leftmost to the rightmost. Any customer
can only buy some prefix of letters from the string s.

There are m friends, the i-th of them is named ti. Each of them is planning to
estimate the following value: how many letters (the length of the shortest
prefix) would s/he need to buy if s/he wanted to construct her/his name of
bought letters. The name can be constructed if each letter is presented in the
equal or greater amount.

For example, for s="arrayhead" and ti="arya" 5 letters have to be bought
("arrayhead").
For example, for s="arrayhead" and ti="harry" 6 letters have to be bought
("arrayhead").
For example, for s="arrayhead" and ti="ray" 5 letters have to be bought
("arrayhead").
For example, for s="arrayhead" and ti="r" 2 letters have to be bought
("arrayhead").
For example, for s="arrayhead" and ti="areahydra" all 9 letters have to be
bought ("arrayhead").
It is guaranteed that every friend can construct her/his name using the letters
from the string s.

Note that the values for friends are independent, friends are only estimating
them but not actually buying the letters.

Input
The first line contains one integer n (1≤n≤2⋅105) — the length of showcase
string s.

The second line contains string s, consisting of exactly n lowercase Latin
letters.

The third line contains one integer m (1≤m≤5⋅104) — the number of friends.

The i-th of the next m lines contains ti (1≤|ti|≤2⋅105) — the name of the i-th
friend.

It is guaranteed that ∑i=1m|ti|≤2⋅105.

Output
For each friend print the length of the shortest prefix of letters from s s/he
would need to buy to be able to construct her/his name of them. The name can be
constructed if each letter is presented in the equal or greater amount.

It is guaranteed that every friend can construct her/his name using the letters
from the string s.

Example
"""
from collections import Counter
def make_dict(s):
    res=dict()
    count_dict = dict()
    for ind, char in enumerate(s):
        count = count_dict.get(char, 0)
        count=count+1
        count_dict[char]=count
        key=char+"_"+str(count)
        res[key] = ind
    return res
def find_least(s_dict, t):
    char_counts = Counter(t)
    ind_ls=[]
    for key, val in char_counts.items():
        new_key = key+"_"+str(val)
        ind_ls.append(s_dict[new_key])
    return max(ind_ls)+1

if __name__ == "__main__":
    N = int(input())
    s = str(input())
    m = int(input())
    s_dict = make_dict(s)
    for i in range(m):
        print(find_least(s_dict, str(input())))


"""
B. Letters Shop
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The letters shop showcase is a string s, consisting of n lowercase Latin
letters. As the name tells, letters are sold in the shop.

Letters are sold one by one from the leftmost to the rightmost. Any customer
can only buy some prefix of letters from the string s.

There are m friends, the i-th of them is named ti. Each of them is planning to
estimate the following value: how many letters (the length of the shortest
prefix) would s/he need to buy if s/he wanted to construct her/his name of
bought letters. The name can be constructed if each letter is presented in the
equal or greater amount.

For example, for s="arrayhead" and ti="arya" 5 letters have to be bought
("arrayhead").
For example, for s="arrayhead" and ti="harry" 6 letters have to be bought
("arrayhead").
For example, for s="arrayhead" and ti="ray" 5 letters have to be bought
("arrayhead").
For example, for s="arrayhead" and ti="r" 2 letters have to be bought
("arrayhead").
For example, for s="arrayhead" and ti="areahydra" all 9 letters have to be
bought ("arrayhead").
It is guaranteed that every friend can construct her/his name using the letters
from the string s.

Note that the values for friends are independent, friends are only estimating
them but not actually buying the letters.

Input
The first line contains one integer n (1≤n≤2⋅105) — the length of showcase
string s.

The second line contains string s, consisting of exactly n lowercase Latin
letters.

The third line contains one integer m (1≤m≤5⋅104) — the number of friends.

The i-th of the next m lines contains ti (1≤|ti|≤2⋅105) — the name of the i-th
friend.

It is guaranteed that ∑i=1m|ti|≤2⋅105.

Output
For each friend print the length of the shortest prefix of letters from s s/he
would need to buy to be able to construct her/his name of them. The name can be
constructed if each letter is presented in the equal or greater amount.

It is guaranteed that every friend can construct her/his name using the letters
from the string s.

Example
"""
from collections import Counter
def make_dict(s):
    res=dict()
    count_dict = dict()
    for ind, char in enumerate(s):
        count = count_dict.get(char, 0)
        count=count+1
        count_dict[char]=count
        key=char*count
        res[key] = ind
    return res
def find_least(s_dict, t):
    char_counts = Counter(t)
    ind_ls=[]
    for key, val in char_counts.items():
        new_key = key*val
        ind_ls.append(s_dict[new_key])
    return max(ind_ls)+1

if __name__ == "__main__":
    N = int(input())
    s = str(input())
    m = int(input())
    friends=[]
    for i in range(m):
        friends.append(str(input()))
    s_dict = make_dict(s)
    for  friend in friends:
        print(find_least(s_dict, friend))


# from collections import Counter
# # a = [1,2,1]
# # print(a.index(2))
# # print(dict1==dict2)

import sys
n = int(sys.stdin.readline().rstrip())
s = sys.stdin.readline().rstrip()

for _  in range(int(input())):
    s2 = list(s)
    name = sys.stdin.readline().rstrip()
    temp_t = 0
    for i in name:
        t = s2.index(i)
        s2[t] = '0'
        if t>temp_t:
            temp_t=t
    print(temp_t+1)


import sys
input = sys.stdin.readline
n = int(input())
string = input().rstrip()
s = [[] for i in range(26)]
# print(s)
# print(s) #debug
for i in range(n):
    s[ord(string[i])-ord('a')].append(i)
# print(s) #debug
# print(s)
for _ in range(int(input())):
    name_li = [0]*26
    name = input().rstrip()
    for i in name:
        name_li[ord(i) - ord('a')]+=1
    # print(name_li)
    ans = 0
    for j in range(26):
        if name_li[j]>0:
            ans = max(ans, s[j][name_li[j]-1])
    print(ans+1)

'''__________my sol'''
# import sys
# n = int(sys.stdin.readline().rstrip())
# s = sys.stdin.readline().rstrip()
#
# for _  in range(int(input())):
#     s2 = list(s)
#     name = sys.stdin.readline().rstrip()
#     temp_t = 0
#     for i in name:
#         t = s2.index(i)
#         s2[t] = '0'
#         if t>temp_t:
#             temp_t=t
#     print(temp_t+1)