// luogu-judger-enable-o2
// luogu-judger-enable-o2
// luogu-judger-enable-o2
#pragma GCC  optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
 #define lson  now<<1
 #define rson now<<1|1
 #define maxn 300000+5
 #define mod 2147483647
 #define re register int
 #define LL long long
 LL hash1[maxn<<2],hash2[maxn<<2];
 LL Pow[maxn];
 LL ari[maxn];
 LL n;
 using namespace std;
 inline LL read()
 {
     LL  x=0,f=1;
 	char ch=getchar();
 	while(!isdigit(ch))
 	{
 		if(ch=='-')  f=-1;
 		ch=getchar();
	 }
	 while(isdigit(ch))
	 {
	 	x=(x<<3)+(x<<1)+ch-'0';
	 	ch=getchar();
	 }
	 return x*f;
}
 void pushup(LL now,LL val){
 	LL a=val>>1;
    hash1[now]=(hash1[lson]*Pow[a]+hash1[rson])%mod;
    hash2[now]=(hash2[rson]*Pow[val-a]+hash2[lson])%mod;
 }
 void update(LL val,LL l,LL r,LL now)
 {
 	if(l==r){
	 hash1[now]=hash2[now]=1;
	 return;}
 	int mid=(l+r)>>1;
 	if(val<=mid)  update(val,l,mid,lson);
 	else update(val,mid+1,r,rson);
 	pushup(now,r-l+1);
 }
 LL query1(LL l,LL r,LL L,LL R,LL now)
 {
 	if(l>r) return 0;
 	if(l==L&&r==R) return hash1[now];
 	LL mid=(L+R)>>1;
 	if(r<=mid) return query1(l,r,L,mid,lson);
 	if(l>mid) return query1(l,r,mid+1,R,rson);
 	return (query1(l,mid,L,mid,lson)*Pow[r-mid]+query1(mid+1,r,mid+1,R,rson))%mod;
 }
 LL query2(LL l,LL r,LL L,LL R,LL now)
 {
 	if(l>r) return 0;
 	if(l==L&&r==R) return hash2[now];
 	LL mid=(L+R)>>1;
 	if(r<=mid) return query2(l,r,L,mid,lson);
 	if(l>mid) return query2(l,r,mid+1,R,rson);
 	return (query2(l,mid,L,mid,lson)+query2(mid+1,r,mid+1,R,rson)*Pow[mid-l+1])%mod;
 }
 int main()
 {
 	Pow[1]=3;
 	for(re i=2;i<=300000;i++) Pow[i]=(3*Pow[i-1])%mod;
 		memset(hash1,0,sizeof(hash1));
 		memset(hash2,0,sizeof(hash2));
 		n=read();
 		bool flag=false;
 		for(re i=1;i<=n;i++) ari[i]=read();
 		for(re i=1;i<=n;i++)
 		{
 			LL len=min(ari[i]-1,n-ari[i]);
 			if(query1(ari[i]-len,ari[i]-1,1,n,1)!=query2(ari[i]+1,ari[i]+len,1,n,1))
 			{
 				flag=true;
				 break;			 }
				update(ari[i],1,n,1);
		}
		if(flag) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	 return 0;
 }


#include <bits/stdc++.h>
using namespace std;
const int N=30000+5;
#define int long long
#define MOD 20040901
#define Is(c) ('0'<=c && c<='9')
inline int read(){
	int x=0,f=1;char c=getchar();
	while(!Is(c)){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(Is(c)) x=x*10+c-48,c=getchar();
	return x*f;
}
int T,n,b=103;
int po[N],val[N*4][2];
inline int ls(int x){return x<<1;}
inline int rs(int x){return x<<1|1;}
void insert(int x,int l,int r,int p,int v,int id){
	if(l==r){
		val[x][id]=v%MOD;
		return ;
	}
	int mid=(l+r)>>1;
	if(p<=mid) insert(ls(x),l,mid,p,v,id);
	else insert(rs(x),mid+1,r,p,v,id);
	val[x][id]=(val[ls(x)][id]+val[rs(x)][id])%MOD;
}
int ask(int x,int l,int r,int L,int R,int id){
	if(L<=l && R>=r) return val[x][id]%MOD;
	int mid=(l+r)>>1,ans=0;
	if(L<=mid) ans=(ans+ask(ls(x),l,mid,L,R,id))%MOD;
	if(R>mid) ans=(ans+ask(rs(x),mid+1,r,L,R,id))%MOD;
	return ans;
}
signed main(){
//	T=read();
	po[0]=1;
	n=read();
	for(int i=1;i<=n;i++) po[i]=po[i-1]*(n+1)%MOD;
//	while(T--){
//		memset(val,0,sizeof(val));
//		n=read();
//		bool flag=false;
		for(int i=1;i<=n;i++){
			int num=read(),len=min(num-1,n-num);
			int l=num-len,_l=n-num-len+1;
			int ans1=ask(1,1,n,l,num,0),ans2=ask(1,1,n,_l,n-num+1,1);
			if(l>_l) ans2=ans2*po[l-_l];
			else ans1=ans1*po[_l-l];
			if(ans1!=ans2){ printf("YES\n"); return 0; }
			insert(1,1,n,num,po[num],0);insert(1,1,n,n-num+1,po[n-num+1],1);
		}
		printf("NO\n");
//	}
	return 0;
}


#include <set>
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn (300010)
#define inf (0x3f3f3f3f)
int n, c[maxn], L[maxn], R[maxn], a[maxn];

inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
		ch == '-' ? f = -1, ch = getchar() : ch = getchar();
	while (ch >= '0' && ch <= '9')
		x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
	return x * f;
}

namespace TreeArray {
	void add(int x, int y) {
		for (; x <= n; x += x & (-x)) c[x] += y;
	}

	int ask(int x) {
		int sum = 0;
		for (; x > 0; x -= x & (-x)) sum += c[x];
		return sum;
	}
}
using namespace TreeArray;

signed main() {
	n = read();
	for (int i = 1; i <= n; ++i) a[i] = read();
	for (int i = 1; i <= n; ++i) {
		if (2 * a[i] - 1 <= n) L[i] = ask(2 * a[i] - 1);
		else L[i] = ask(n) - ask(2 * a[i] - n - 1);
		add(a[i], a[i]);
	}
	memset(c, 0, sizeof c);
	for (int i = n; i >= 1; --i) {
		if (2 * a[i] - 1 <= n) R[i] = ask(2 * a[i] - 1);
		else R[i] = ask(n) - ask(2 * a[i] - n - 1);
		add(a[i], a[i]);
	}
	for (int i = 2; i <= n - 1; ++i)
		if (L[i] % (2 * a[i]) && R[i] % (2 * a[i]))
			puts("YES"), exit(0);
	puts("NO");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=30000+5;
#define int long long
#define Is(c) ('0'<=c && c<='9')
inline int read(){
	int x=0,f=1;char c=getchar();
	while(!Is(c)){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(Is(c)) x=x*10+c-48,c=getchar();
	return x*f;
}
int T,n,b=103;
int po[N],val[N*4][2];
inline int ls(int x){return x<<1;}
inline int rs(int x){return x<<1|1;}
void insert(int x,int l,int r,int p,int v,int id){
	if(l==r){
		val[x][id]=v;
		return ;
	}
	int mid=(l+r)>>1;
	if(p<=mid) insert(ls(x),l,mid,p,v,id);
	else insert(rs(x),mid+1,r,p,v,id);
	val[x][id]=val[ls(x)][id]+val[rs(x)][id];
}
int ask(int x,int l,int r,int L,int R,int id){
	if(L<=l && R>=r) return val[x][id];
	int mid=(l+r)>>1,ans=0;
	if(L<=mid) ans+=ask(ls(x),l,mid,L,R,id);
	if(R>mid) ans+=ask(rs(x),mid+1,r,L,R,id);
	return ans;
}
signed main(){
//	T=read();
	po[0]=1;
	for(int i=1;i<=N-5;i++) po[i]=po[i-1]*b;
//	while(T--){
		memset(val,0,sizeof(val));
		n=read();bool flag=false;
		for(int i=1;i<=n;i++){
			int num=read(),len=min(num-1,n-num);
			int l=num-len,_l=n-num-len+1;
			int ans1=ask(1,1,n,l,num,0),ans2=ask(1,1,n,_l,n-num+1,1);
			if(l>_l) ans2*=po[l-_l];
			else ans1*=po[_l-l];
			if(ans1!=ans2){ printf("YES\n"); flag=true; break; }
			insert(1,1,n,num,po[num],0);insert(1,1,n,n-num+1,po[n-num+1],1);
		}
		if(!flag) printf("NO\n");
//	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#define mp make_pair
#define Fst first
#define Sec second
using namespace std;
typedef unsigned long long ull;
inline char nc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int red(){
    int res=0,f=1;char ch=nc();
    while (ch<'0'||'9'<ch) {if (ch=='-') f=-f;ch=nc();}
    while ('0'<=ch&&ch<='9') res=(res<<3)+(res<<1)+ch-48,ch=nc();
    return res*f;
}

const int maxn=300005;
int n;
ull p[maxn];
struct node{
    node *l,*r;
    int L,R;ull a,b;
    node () {}
    node (int _L,int _R):L(_L),R(_R),a(0),b(0) {}
    void pushup(){
        if (L==R) return;
        a=l->a*p[r->R - r->L +1]+r->a;
        b=l->b+ r->b*p[l->R - l->L +1];
    }
}base[maxn<<1],nil;
typedef node* P_node;
P_node null,Rot,len;
void init(){
    nil=node(0,0);null=&nil;
    null->l=null->r=null;len=base;
}
P_node newnode(int L,int R){
    *len=node(L,R);
    len->l=len->r=null;
    return len++;
}
P_node build(int L,int R){
    P_node x=newnode(L,R);
    if (L==R) return x;
    int mid=L+R>>1;
    x->l=build(L,mid);x->r=build(mid+1,R);
    return x;
}
void insert(P_node x,int k){
    if (x->L==x->R) {x->a=x->b=1;return;}
    int mid=x->L+x->R>>1;
    if (k<=mid) insert(x->l,k);else insert(x->r,k);
    x->pushup();
}
pair<int,int> A,B;
void queryA(P_node x,int L,int R){
    if (R<x->L||x->R<L) return;
    if (L<=x->L&&x->R<=R) {A.Fst=x->a*p[A.Sec]+A.Fst;A.Sec+=x->R-x->L+1;return;}
    queryA(x->r,L,R);queryA(x->l,L,R);
}
void queryB(P_node x,int L,int R){
    if (R<x->L||x->R<L) return;
    if (L<=x->L&&x->R<=R) {B.Fst=x->b*p[B.Sec]+B.Fst;B.Sec+=x->R-x->L+1;return;}
    queryB(x->l,L,R);queryB(x->r,L,R);
}
int main(){
    n=red();p[0]=1;
    for (int i=1;i<=n;i++) p[i]=p[i-1]*233;
    init();Rot=build(1,n);
    for (int i=1;i<=n;i++){
        int x=red(),k=min(x-1,n-x);
        A=B=mp(0,0);
        queryA(Rot,x-k,x);queryB(Rot,x,x+k);
        if (A.Fst!=B.Fst) return printf("YES"),0;
        insert(Rot,x);
    }
    printf("NO");
    return 0;
}


# include <bits/stdc++.h>
# define LL long long
# define imax(a,b) ((a)>(b))?(a):(b)
# define imin(a,b) ((a)<(b))?(a):(b)
# define is(a) (a>='0'&&a<='9')
# define hash 13331
using namespace  std;
const int maxn=300000+5;
LL Mod=1e9+5,w[maxn],a[maxn];
LL n;
struct data
{
	int s[3];
	int ro;
}t[maxn<<4];
void read(LL &x)
{
	int f=0;
	x=0;
	char ch=getchar();
	for (; !is(ch); ch=getchar() )  f|=ch=='-';
	for (; is (ch); ch=getchar()) x=(x<<3)+(x<<1)+(ch^48);
	x=(f)?-x:x;
}
void up(int ro)
{
	t[ro].s[0]=(t[ro<<1].s[0]+t[ro<<1|1].s[0])%Mod;
	t[ro].s[1]=(t[ro<<1].s[1]+t[ro<<1|1].s[1])%Mod;
}
void  update(int ro,int L,int R,int li,int val)
{
	if (L>li||R<li) return;
	if (L==R)
	{
		t[ro].s[val]=w[L];
		return;
	}
	int mid=(L+R)>>1;
	update(ro<<1,L,mid,li,val);
	update(ro<<1|1,mid+1,R,li,val);
	up(ro);
}
LL query(int ro,int L,int R,int li,int ri,int val)
{
	if (L>ri||R<li) return 0;
	if (L>=li&&R<=ri) return t[ro].s[val];
	int mid=(L+R)>>1;
	LL x=query(ro<<1,L,mid,li,ri,val)%Mod;
	LL y=query(ro<<1|1,mid+1,R,li,ri,val)%Mod;
	return (x+y)%Mod;
}
void  work()
{
	for (int i=1; i<=n; i++)
	{
		int r=a[i];
		int len=imin(a[i]-1,n-a[i]);
		if (!len)
		{
			update(1,1,n,a[i],0);
			update(1,1,n,n-a[i]+1,1);
			continue;
		}
		LL A =r-len,B=n-(r+len)+1;
		LL x,y;
		LL a =query(1,1,n,A,r,0), b=query(1,1,n,B,n-r+1,1);
		if (A>B)b=b*w[A-B]%Mod; else	a=a*w[B-A]%Mod;
		if (a^b)
		{
			printf("YES");
			return;
		}
		update(1, 1, n, r, 0), update(1, 1, n, n - r + 1, 1);

	}
	printf("NO");
}
int main()
{
	read(n);
	w[0]=1;
	for (int i=1; i<=n; i++) read(a[i]),w[i]=w[i-1]*hash;
	work();
}


#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const int N=2e5+100;
int tr[N],n;
int lowbit(int x) {return x&(-x);}
void add(int x) {for(int i=x;i<=n;i+=lowbit(i)) tr[i]+=x;}
int query(int x)
{
	int sum=0;
	for(int i=x;i;i-=lowbit(i)) sum+=tr[i];
	return sum;
}
int main()
{
	memset(tr,0,sizeof(tr));
	int F=0;
	scanf("%d",&n);
	for(int i=1,a;i<=n;i++)
	{
		scanf("%d",&a);
		if(a==n) {add(a);continue;}
		int x=query(min(n,2*a-1));
		int y=query(max(1,2*a-n)-1);
		if((x-y)%(2*a)!=0)
		{
			if(!F) puts("YES");
			F=1;
		}
		add(a);
	}
	if(!F) puts("NO");
	return 0;
}


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

inline void read(int&x){
	char c11=getchar();x=0;while(!isdigit(c11))c11=getchar();
	while(isdigit(c11))x=x*10+c11-'0',c11=getchar();
}

const int N=301000,p=1005004331;
ll hs[N<<2][2];
int bs[N],n;

void update(int l,int r,int x,int ps){
	if(l==r){hs[x][0]=hs[x][1]=1;return ;}
	int mid(l+r>>1);
	if(ps<=mid)update(l,mid,x<<1,ps);
	else update(mid+1,r,x<<1|1,ps);
	hs[x][0]=(hs[x<<1][0]*bs[r-mid]+hs[x<<1|1][0])%p;
	hs[x][1]=(hs[x<<1|1][1]*bs[mid-l+1]+hs[x<<1][1])%p;
}

ll query(int l,int r,int x,int L,int R,int k){
	L=max(L,l),R=min(R,r);
	if(L<=l&&r<=R)return hs[x][k];
	int mid(l+r>>1);ll res(0);
	if(!k){
		if(L<=mid)res=query(l,mid,x<<1,L,R,k);
		if(mid<R)res=(res*bs[R-mid]+query(mid+1,r,x<<1|1,L,R,k))%p;
		return res;
	}else {
		if(mid<R)res=query(mid+1,r,x<<1|1,L,R,k);
		if(L<=mid)res=(res*bs[mid-L+1]+query(l,mid,x<<1,L,R,k))%p;
		return res;
	}
}

int main(){
	read(n);bs[0]=1;
	for(int i=1;i<=n;++i)bs[i]=2ll*bs[i-1]%p;
	for(int i=1,x;i<=n;++i){
		read(x);update(1,n,1,x);
		int L=min(x-1,n-x);if(L)
		if(query(1,n,1,x-L,x-1,1)!=query(1,n,1,x+1,x+L,0))
			{puts("YES");return 0;}
	}
	puts("NO");
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define re register
#define ll long long
#define ull unsigned long long
#define N 300000
const int seed=2;

inline int read()
{
	char ch=getchar();
	int x=0,f=1;
	for(;ch>'9'||ch<'0';ch=getchar())if(ch=='-')f=-1;
	for(;ch>='0'&&ch<='9';ch=getchar())x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}

struct node{
	int l,r;
	ull hash1,hash2;
}tree[N*4+10];
int n,a[N+10];
ull Bit[N+10];

inline void up(int t)
{
	tree[t].hash1=tree[t<<1].hash1*Bit[tree[t].r-tree[t<<1].r]+tree[t<<1|1].hash1;
	tree[t].hash2=tree[t<<1|1].hash2*Bit[tree[t<<1].r-tree[t].l+1]+tree[t<<1].hash2;
}

inline void build(int t,int l,int r)
{
	tree[t].l=l;tree[t].r=r;tree[t].hash1=tree[t].hash2=0;
	if(l==r)return;
	int mid=l+r>>1;
	build(t<<1,l,mid);build(t<<1|1,mid+1,r);
	up(t);
}

inline void update(int t,int k,int x)
{
	if(tree[t].l>k||tree[t].r<k)return;
	if(tree[t].l==k&&tree[t].r==k)
	{
		if(x)tree[t].hash1=tree[t].hash2=1;
		else tree[t].hash1=tree[t].hash2=0;
		return;
	}
	update(t<<1,k,x);update(t<<1|1,k,x);
//	puts("pre::");
//	printf("t==%d l==%d r==%d hash1==%lld hash2==%lld\n",t,tree[t].l,tree[t].r,tree[t].hash1,tree[t].hash2);
	up(t);
//	puts("after::");
//	printf("t==%d l==%d r==%d hash1==%lld hash2==%lld\n",t,tree[t].l,tree[t].r,tree[t].hash1,tree[t].hash2);
}

inline ull query1(int t,int l,int r)
{
	if(l>r)return 0;
	if(l==tree[t].l&&tree[t].r==r)return tree[t].hash1;
	int mid=tree[t<<1].r;
	if(r<=mid)return query1(t<<1,l,r);
	else if(l>mid)return query1(t<<1|1,l,r);
	else return query1(t<<1,l,mid)*Bit[r-mid]+query1(t<<1|1,mid+1,r);
}

inline ull query2(int t,int l,int r)
{
	if(l>r)return 0;
//	printf("t==%d l==%d r==%d x==%d y==%d hash1==%lld hash2==%lld\n",t,tree[t].l,tree[t].r,l,r,tree[t].hash1,tree[t].hash2);
	if(l==tree[t].l&&tree[t].r==r)return tree[t].hash2;
	int mid=tree[t<<1].r;
	if(r<=mid)return query1(t<<1,l,r);
	else if(l>mid)return query1(t<<1|1,l,r);
	else return query2(t<<1|1,mid+1,r)*Bit[mid-l+1]+query2(t<<1,l,mid);
}

int main()
{
	n=read();
	for(re int i=1;i<=n;++i)a[i]=read();
	Bit[0]=1;
	for(re int i=1;i<=n;++i)Bit[i]=Bit[i-1]*seed;
	build(1,1,n);
	for(re int i=1;i<=n;++i)
	{
		update(1,a[i],1);
		int d=min(a[i]-1,n-a[i]);//求出回文串长度
		if(!d)continue;
//		printf("i==%d l==%lld r==%lld\n",i,query2(1,a[i]-d,a[i]-1),query1(1,a[i]+1,a[i]+d));
		if(query2(1,a[i]-d,a[i]-1)^query1(1,a[i]+1,a[i]+d)){puts("YES");return 0;}
	}
	puts("NO");
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;
const ll Maxn = 3e5 + 5, Mod = 0x7fffffff;
ll N, T, Temp, Pow[Maxn]{1}, Hash[Maxn << 2][2];

inline void PushUp(ll X, ll L, ll R)
{
    ll Mid = (L + R) >> 1;
    Hash[X][0] = (Hash[X << 1][0] + Hash[X << 1 | 1][0] * Pow[Mid - L + 1]) % Mod;
    Hash[X][1] = (Hash[X << 1][1] * Pow[R - Mid] + Hash[X << 1 | 1][1]) % Mod;
}

void UpDate(ll L, ll R, ll P, ll X = 1)
{
    if (L == R)
    {
        Hash[X][0] = Hash[X][1] = 1;
        return ;
    }
    ll Mid = (L + R) >> 1;
    if (P <= Mid) UpDate(L, Mid, P, X << 1);
    else UpDate(Mid + 1, R, P, X << 1 | 1);
    PushUp(X, L, R);
}

ll QueryF(ll L, ll R, ll Tl, ll Tr, ll X = 1)
{
    if (L >= Tl && R <= Tr) return Hash[X][0];
    ll Mid = (L + R) >> 1;
    if (Tr <= Mid) return QueryF(L, Mid, Tl, Tr, X << 1);
    if (Tl > Mid) return QueryF(Mid + 1, R, Tl, Tr, X << 1 | 1);
    return (QueryF(L, Mid, Tl, Tr, X << 1) + QueryF(Mid + 1, R, Tl, Tr, X << 1 | 1) * Pow[Mid - L + 1]) % Mod;
}

ll QueryB(ll L, ll R, ll Tl, ll Tr, ll X = 1)
{
    if (L >= Tl && R <= Tr) return Hash[X][1];
    ll Mid = (L + R) >> 1;
    if (Tr <= Mid) return QueryB(L, Mid, Tl, Tr, X << 1);
    if (Tl > Mid) return QueryB(Mid + 1, R, Tl, Tr, X << 1 | 1);
    return (QueryB(L, Mid, Tl, Tr, X << 1) * Pow[R - Mid] + QueryB(Mid + 1, R, Tl, Tr, X << 1 | 1)) % Mod;
}

int main()
{
    for (int i = 1; i < Maxn; ++i)Pow[i] = 2 * Pow[i - 1] %  Mod;
    scanf("%lld", &N);
    bool Flag(1);
    for (int i = 1; i <= N; ++i)
    {
        scanf("%lld", &Temp);
        ll Len = min(N - Temp, Temp - 1);
        if (Len && QueryF(1, N, Temp - Len, Temp - 1) != QueryB(1, N, Temp + 1, Temp + Len))
        {
            Flag = 0;
            break;
        }
        UpDate(1, N, Temp);
    }
    if (Flag)puts("NO");
    else puts("YES");
}


#include <bits/stdc++.h>
using namespace std;
const int N=5e5+10;
int T,n,a[N],flg;
int rev[N];
int main(){
   cin>>T;
   while(T--){
      cin>>n;flg=0;
      for(int i=1;i<=n;i++)
         cin>>a[i],rev[a[i]]=i;
      sort(a+1,a+1+n);
      for(int d=1;d<=n/2;d++)
         for(int i=1;i+d+d<=n;i++)
            if(rev[i]>rev[i+d]&&rev[i+d]>rev[i+d+d]||
               rev[i]<rev[i+d]&&rev[i+d]<rev[i+d+d])
                  {flg++;goto fi;}
      fi:;
      if(flg) cout<<"Y"<<endl;
      else cout<<"N"<<endl;
   }
   return 0;
}


#include<bits/stdc++.h>
#define ull unsigned long long
#define hash orz
#define ls(a) (a<<1)
#define rs(a) (a<<1|1)
using namespace std;
const int maxn=300005,hg=100007;
int n,last;
int a[maxn];
bool flag=0;
ull bas[maxn];
ull hh1,hh2,hash;
struct node{
	int l,r;
	ull h1,h2;//正反哈希
}t[maxn*4];
int read()
{
	int s=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9')if (ch=='-')f=-1,ch=getchar();
	while (ch>='0'&&ch<='9')s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
	return s*f;
}
int getlen(int p)
{
	return t[p].r-t[p].l+1;
}
void push_up(int p)
{
	int len1=getlen(ls(p)),len2=getlen(rs(p));
	t[p].h1=t[ls(p)].h1*bas[len2]+t[rs(p)].h1;
	t[p].h2=t[rs(p)].h2*bas[len1]+t[ls(p)].h2;
}
void build(int p,int l,int r)
{
	t[p].l=l;t[p].r=r;
	if (l==r){
		t[p].h1=t[p].h2=0;//开始状态为零
		return;
	}
	int mid=(l+r)>>1;
	build(ls(p),l,mid);build(rs(p),mid+1,r);
}
void update(int p,int x)
{
	int l=t[p].l,r=t[p].r;
	if (l==r){
		t[p].h1=t[p].h2=1;
		return;
	}
	int mid=(l+r)>>1;
	if (mid>=x)update(ls(p),x);else update(rs(p),x);
	push_up(p);
}
void query1(int p,int x,int y)
{
	int l=t[p].l,r=t[p].r;
	if (l>=x&&r<=y){
		hash+=t[p].h1*bas[last];
		last+=getlen(p);
		return;
	};
	int mid=(l+r)>>1;
	if (mid<y)query1(rs(p),x,y);
	if (mid>=x)query1(ls(p),x,y);
}
void query2(int p,int x,int y)
{
	int l=t[p].l,r=t[p].r;
	if (l>=x&&r<=y){
		hash+=t[p].h2*bas[last];
		last+=getlen(p);
		return;
	}
	int mid=(l+r)>>1;
	if (mid>=x)query2(ls(p),x,y);
	if (mid<y)query2(rs(p),x,y);
}
int main()
{
    int i;
    n=read();
    bas[0]=1;
    for (i=1;i<=n;i++)
    a[i]=read(),bas[i]=bas[i-1]*hg;
    build(1,1,n);//初始化t的l,r
    for (i=1;i<=n;i++)
    {
    	int k=min(a[i]-1,n-a[i]);
    	hash=0;last=0;query1(1,a[i]-k,a[i]-1);hh1=hash;
    	hash=0;last=0;query2(1,a[i]+1,a[i]+k);hh2=hash;
    	if (hh1!=hh2)flag=1;
    	update(1,a[i]);
	}
    puts(flag?"YES":"NO");
    return 0;
}


#include<stdio.h>
#include<algorithm>
#include<queue>
#include<cstring>
#define re register
#define O_O 300005
#define int long long
#define oo 1000003
using namespace std;
inline int read()
{
	re int x=0,f=1;
	re char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-') f=-1;
		c=getchar();
	}
	while('0'<=c&&c<='9')
	{
		x=(x<<1)+(x<<3)+(c^48);
		c=getchar();
	}
	return x*f;
}

int n,len;
int a[O_O];
unsigned long long p[O_O];

namespace SEG
{
	#define ls (u<<1)
	#define rs (u<<1|1)
	unsigned long long hl[O_O*2],hr[O_O*2];

	void add(int u,int l,int r,int k)
	{
		if(l==r)
		{
			hl[u]=hr[u]=1;
			return ;
		}
		re int mid=(l+r)>>1;
		if(mid>=k) add(ls,l,mid,k);
		else add(rs,mid+1,r,k);
		hl[u]=hl[rs]+hl[ls]*p[r-mid];
		hr[u]=hr[ls]+hr[rs]*p[mid-l+1];
	}

	unsigned long long ql(int u,int l,int r,int a,int b)
	{
		if(l==a&&b==r) return hl[u];
		re int mid=(l+r)>>1;
		if(mid>=b) return ql(ls,l,mid,a,b);
		else if(mid<a) return ql(rs,mid+1,r,a,b);
		return ql(rs,mid+1,r,mid+1,b)+ql(ls,l,mid,a,mid)*p[r-mid];
	}

	unsigned long long qr(int u,int l,int r,int a,int b)
	{
		if(l==a&&b==r) return hr[u];
		re int mid=(l+r)>>1;
		if(mid>=b) return qr(ls,l,mid,a,b);
		else if(mid<a) return qr(rs,mid+1,r,a,b);
		return qr(ls,l,mid,a,mid)+qr(rs,mid+1,r,mid+1,b)*p[mid-l+1];
	}
}
using namespace SEG;

inline int min(int a,int b) { return a<b?a:b; }
signed main()
{
	n=read();
	p[0]=1;
	for(re int i=1;i<=n;++i) a[i]=read();
	for(re int i=1;i<=n;++i) p[i]=p[i-1]*oo;
	for(re int i=1;i<=n;++i)
	{
		add(1,1,n,a[i]);
		len=min(a[i]-1,n-a[i]);
		if(ql(1,1,n,a[i]-len,a[i])^qr(1,1,n,a[i],a[i]+len))
		return 0*puts("YES");
	}
	puts("NO");
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;
const ll Maxn = 3e5 + 5, Mod = 0x7fffffff;
ll N, T, Temp, Pow[Maxn]{1}, Hash[Maxn << 2][2];

inline void PushUp(ll X, ll L, ll R)
{
    ll Mid = (L + R) >> 1;
    Hash[X][0] = (Hash[X << 1][0] + Hash[X << 1 | 1][0] * Pow[Mid - L + 1]) % Mod;
    Hash[X][1] = (Hash[X << 1][1] * Pow[R - Mid] + Hash[X << 1 | 1][1]) % Mod;
}

void UpDate(ll L, ll R, ll P, ll X = 1)
{
    if (L == R)
    {
        Hash[X][0] = Hash[X][1] = 1;
        return ;
    }
    ll Mid = (L + R) >> 1;
    if (P <= Mid) UpDate(L, Mid, P, X << 1);
    else UpDate(Mid + 1, R, P, X << 1 | 1);
    PushUp(X, L, R);
}

ll QueryF(ll L, ll R, ll Tl, ll Tr, ll X = 1)
{
    if (L >= Tl && R <= Tr) return Hash[X][0];
    ll Mid = (L + R) >> 1;
    if (Tr <= Mid) return QueryF(L, Mid, Tl, Tr, X << 1);
    if (Tl > Mid) return QueryF(Mid + 1, R, Tl, Tr, X << 1 | 1);
    return (QueryF(L, Mid, Tl, Mid, X << 1) + QueryF(Mid + 1, R, Mid + 1, Tr, X << 1 | 1) * Pow[Mid - L + 1]) % Mod;
}

ll QueryB(ll L, ll R, ll Tl, ll Tr, ll X = 1)
{
    if (L >= Tl && R <= Tr) return Hash[X][1];
    ll Mid = (L + R) >> 1;
    if (Tr <= Mid) return QueryB(L, Mid, Tl, Tr, X << 1);
    if (Tl > Mid) return QueryB(Mid + 1, R, Tl, Tr, X << 1 | 1);
    return (QueryB(L, Mid, Tl, Mid, X << 1) * Pow[R - Mid] + QueryB(Mid + 1, R, Mid + 1, Tr, X << 1 | 1)) % Mod;
}

int main()
{
    for (int i = 1; i < Maxn; ++i)Pow[i] = 2 * Pow[i - 1] %  Mod;
    scanf("%lld", &N);
    bool Flag(1);
    for (int i = 1; i <= N; ++i)
    {
        scanf("%lld", &Temp);
        ll Len = min(N - Temp, Temp - 1);
        if (Len && QueryF(1, N, Temp - Len, Temp - 1) != QueryB(1, N, Temp + 1, Temp + Len))
        {
            Flag = 0;
            break;
        }
        UpDate(1, N, Temp);
    }
    if (Flag)puts("NO");
    else puts("YES");
}


#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define reg register
using namespace std;
typedef unsigned long long ll;
const int N=3e5+5,base=127;
int n,a[N];
ll pw[N],hs1[N<<2],hs2[N<<2];
inline int read()
{
	int x=0,w=1;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') c=getchar(),w=-1;
	while (isdigit(c))
	{
		x=(x<<1)+(x<<3)+c-'0';
		c=getchar();
	}
	return x*w;
}
inline void pushup(int now,int l,int r)
{
	hs1[now]=hs1[now<<1]*pw[r]+hs1[now<<1|1];
	hs2[now]=hs2[now<<1]+hs2[now<<1|1]*pw[l];
}
void update(int l,int r,int now,int x)
{
	if (l==r) {hs1[now]=hs2[now]=1; return;}
	int mid=(l+r)>>1;
	if (x<=mid) update(l,mid,now<<1,x);
	else update(mid+1,r,now<<1|1,x);
	pushup(now,mid-l+1,r-mid);
}
int query1(int L,int R,int l,int r,int now)
{
	if (l>R||r<L) return 0;
	if (l>=L&&r<=R) return hs1[now];
	int mid=(l+r)>>1;
	if (mid>=R) return query1(L,R,l,mid,now<<1);
	if (mid<L) return query1(L,R,mid+1,r,now<<1|1);
	return query1(L,mid,l,mid,now<<1)*pw[R-mid]+query1(mid+1,R,mid+1,r,now<<1|1);
}
int query2(int L,int R,int l,int r,int now)
{
	if (l>R||r<L) return 0;
	if (l>=L&&r<=R) return hs2[now];
	int mid=(l+r)>>1;
	if (mid>=R) return query2(L,R,l,mid,now<<1);
	if (mid<L) return query2(L,R,mid+1,r,now<<1|1);
	return query2(L,mid,l,mid,now<<1)+query2(mid+1,R,mid+1,r,now<<1|1)*pw[mid-L+1];
}
int main()
{
	n=read(); bool flag=0;
	for (reg int i=1;i<=n;i++) a[i]=read(),pw[i]=pw[i-1]*base;
	for (reg int i=1;i<=n;i++)
	{
		int len=min(a[i]-1,n-a[i]);
		if (query1(a[i]-len,a[i]-1,1,n,1)!=query2(a[i]+1,a[i]+len,1,n,1)){flag=1; break;}
		update(1,n,1,a[i]);
	}
	if (flag) puts("YES"); else puts("NO");
	return 0;
}


// luogu-judger-enable-o2
//Code by : Y-k-y
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#define ll long long
const int N=100010;
using namespace std;
inline int read(){
	int res=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){res=res*10+ch-'0';ch=getchar();}
	return res*f;
}
inline void wr(int x){
    if(x<0){putchar('-');x=-x;}if(x>9) wr(x/10);putchar(x%10+'0');
}
int T,n,flag;
int a[N],t[N];
int main(){
	T=1;
	while(T--){
		flag=0;
		n=read();
		for(int i=1;i<=n;i++)a[i]=read(),t[a[i]]=i;
		sort(a+1,a+1+n);
		for(int j=1;j<=n/2;j++){
			for(int i=1;i<=n-j-j;i++){
				if((t[i]<t[i+j]&&t[i+j]<t[i+j+j])||(t[i]>t[i+j]&&t[i+j]>t[i+j+j]))
				{
					flag=1;
					break;
				}

			}if(flag)break;
		}
		if(flag)puts("YES");
		else puts("NO");
		}
	return 0;
}


# include <bits/stdc++.h>
# define LL long long
# define imax(a,b) ((a)>(b))?(a):(b)
# define imin(a,b) ((a)<(b))?(a):(b)
# define is(a) (a>='0'&&a<='9')
# define hash 13331
using namespace  std;
const int maxn=300000+5;
LL Mod=1e9+7,w[maxn],a[maxn];
LL n;
struct data
{
	int s[3];
	int ro;
}t[maxn<<2];
void read(LL &x)
{
	int f=0;
	x=0;
	char ch=getchar();
	for (; !is(ch); ch=getchar() )  f|=ch=='-';
	for (; is (ch); ch=getchar()) x=(x<<3)+(x<<1)+(ch^48);
	x=(f)?-x:x;
}
void up(int ro)
{
	t[ro].s[0]=(t[ro<<1].s[0]+t[ro<<1|1].s[0])%Mod;
	t[ro].s[1]=(t[ro<<1].s[1]+t[ro<<1|1].s[1])%Mod;
}
void  update(int ro,int L,int R,int li,int val)
{
	if (L>li||R<li) return;
	if (L==R)
	{
		t[ro].s[val]=w[L];
		return;
	}
	int mid=(L+R)>>1;
	update(ro<<1,L,mid,li,val);
	update(ro<<1|1,mid+1,R,li,val);
	up(ro);
}
LL query(int ro,int L,int R,int li,int ri,int val)
{
	if (L>ri||R<li) return 0;
	if (L>=li&&R<=ri) return t[ro].s[val];
	int mid=(L+R)>>1;
	LL x=query(ro<<1,L,mid,li,ri,val)%Mod;
	LL y=query(ro<<1|1,mid+1,R,li,ri,val)%Mod;
	return (x+y)%Mod;
}
void  work()
{
	for (int i=1; i<=n; i++)
	{
		int r=a[i];
		int len=imin(a[i]-1,n-a[i]);
		if (!len)
		{
			update(1,1,n,a[i],0);
			update(1,1,n,n-a[i]+1,1);
			continue;
		}
		LL A =r-len,B=n-(r+len)+1;
		LL x,y;
		LL a =query(1,1,n,A,r,0), b=query(1,1,n,B,n-r+1,1);
		if (A>B)b=b*w[A-B]%Mod; else	a=a*w[B-A]%Mod;
		if (a^b)
		{
			printf("YES");
			return;
		}
		update(1, 1, n, r, 0), update(1, 1, n, n - r + 1, 1);

	}
	printf("NO");
}
int main()
{
	read(n);
	w[0]=1;
	for (int i=1; i<=n; i++) read(a[i]),w[i]=w[i-1]*hash;
	work();
}


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

const int maxn=300005;
const unsigned mo=233;

inline int read(){
	register int x=0,w=1,ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')w=-1;
	for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-48;
	return w*x;
}

int n,a[maxn];
unsigned long long pw[maxn],val[maxn<<2][2];

unsigned long long query(int p,int l,int r,int L,int R,int k){
	if(L<=l&&r<=R)return val[p][k];
	int mid=(l+r)>>1;
	if(R<=mid)return query(p<<1,l,mid,L,R,k);
	if(mid<L)return query(p<<1|1,mid+1,r,L,R,k);
	return query(p<<1,l,mid,L,R,k)+query(p<<1|1,mid+1,r,L,R,k);
}

void modify(int p,int l,int r,int pos,int v,int k){
	if(l==r){val[p][k]+=v;return ;}
	int mid=(l+r)>>1;
	if(pos<=mid)modify(p<<1,l,mid,pos,v,k);
	else modify(p<<1|1,mid+1,r,pos,v,k);
	val[p][k]=val[p<<1][k]+val[p<<1|1][k];
}

int main(){
	n=read();pw[0]=1;
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)pw[i]=pw[i-1]*mo;
	for(int i=1;i<=n;i++){
		int len=min(a[i]-1,n-a[i]);
		int l1=a[i]-len,l2=n-a[i]-len+1;
		unsigned long long x=query(1,1,n,l1,a[i],0);
		unsigned long long y=query(1,1,n,l2,n-a[i]+1,1);
		if(l1>l2)y*=pw[l1-l2];else x*=pw[l2-l1];
		if(x!=y){puts("YES");return 0;}
		modify(1,1,n,a[i],pw[a[i]],0);
		modify(1,1,n,n-a[i]+1,pw[n-a[i]+1],1);
	}
	puts("NO");
}


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

const int maxn=500005;
const unsigned mo=233;

inline int read(){
	register int x=0,w=1,ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')w=-1;
	for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-48;
	return w*x;
}

int n,a[maxn];
unsigned long long pw[maxn],val[maxn<<2][2];

unsigned long long query(int p,int l,int r,int L,int R,int k){
	if(L<=l&&r<=R)return val[p][k];
	int mid=(l+r)>>1;
	if(R<=mid)return query(p<<1,l,mid,L,R,k);
	if(mid<L)return query(p<<1|1,mid+1,r,L,R,k);
	return query(p<<1,l,mid,L,R,k)+query(p<<1|1,mid+1,r,L,R,k);
}

void modify(int p,int l,int r,int pos,int v,int k){
	if(l==r){val[p][k]=v;return ;}
	int mid=(l+r)>>1;
	if(pos<=mid)modify(p<<1,l,mid,pos,v,k);
	else modify(p<<1|1,mid+1,r,pos,v,k);
	val[p][k]=val[p<<1][k]+val[p<<1|1][k];
}

int main(){
	n=read();pw[0]=1;
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)pw[i]=pw[i-1]*mo;
	for(int i=1;i<=n;i++){
		int len=min(a[i]-1,n-a[i]);
		int l1=a[i]-len,l2=n-a[i]-len+1;
		unsigned long long x=query(1,1,n,l1,a[i],0);
		unsigned long long y=query(1,1,n,l2,n-a[i]+1,1);
		if(l1>l2)y*=pw[l1-l2];else x*=pw[l2-l1];
		if(x!=y){puts("YES");return 0;}
		modify(1,1,n,a[i],pw[a[i]],0);
		modify(1,1,n,n-a[i]+1,pw[n-a[i]+1],1);
	}
	puts("NO");
}


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

const int maxn=500005;
const unsigned mo=233;

inline int read(){
	register int x=0,w=1,ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')w=-1;
	for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-48;
	return w*x;
}

int n,a[maxn];
unsigned long long pw[maxn],val[maxn<<2][2];

unsigned long long query(int p,int l,int r,int L,int R,int k){
	if(L<=l&&r<=R)return val[p][k];
	int mid=(l+r)>>1;
	if(R<=mid)return query(p<<1,l,mid,L,R,k);
	if(mid<L)return query(p<<1|1,mid+1,r,L,R,k);
	return query(p<<1,l,mid,L,R,k)+query(p<<1|1,mid+1,r,L,R,k);
}

void modify(int p,int l,int r,int pos,unsigned long long v,int k){
	if(l==r){val[p][k]=v;return ;}
	int mid=(l+r)>>1;
	if(pos<=mid)modify(p<<1,l,mid,pos,v,k);
	else modify(p<<1|1,mid+1,r,pos,v,k);
	val[p][k]=val[p<<1][k]+val[p<<1|1][k];
}

int main(){
	n=read();pw[0]=1;
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)pw[i]=pw[i-1]*mo;
	for(int i=1;i<=n;i++){
		int len=min(a[i]-1,n-a[i]);
		int l1=a[i]-len,l2=n-a[i]-len+1;
		unsigned long long x=query(1,1,n,l1,a[i],0);
		unsigned long long y=query(1,1,n,l2,n-a[i]+1,1);
		if(l1>l2)y*=pw[l1-l2];else x*=pw[l2-l1];
		if(x!=y){puts("YES");return 0;}
		modify(1,1,n,a[i],pw[a[i]],0);
		modify(1,1,n,n-a[i]+1,pw[n-a[i]+1],1);
	}
	puts("NO");
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll maxx = 3e5 + 20, base = 5e7 + 29, mod = 1e8 + 9;

ll st1[4 * maxx + 10], st2[4 * maxx + 10], n, a[maxx], base_pw[maxx];

void modify1(int x, int id = 1, int l = 0, int r = n)
{
	if (l + 1 == r)
	{
		st1[id] = 1;
		return;
	}
	int mid = (l + r) / 2;
	if (x < mid)
		modify1(x, id * 2, l, mid);
	else
		modify1(x, id * 2 + 1, mid, r);
	st1[id] = ((st1[id * 2] * base_pw[r - mid]) + st1[id * 2 + 1]) % mod;
}

void modify2(int x, int id = 1, int l = 0, int r = n)
{
	if (l + 1 == r)
	{
		st2[id] = 1;
		return;
	}
	int mid = (l + r) / 2;
	if (x < mid)
		modify2(x, id * 2, l, mid);
	else
		modify2(x, id * 2 + 1, mid, r);
	st2[id] = ((st2[id * 2] * base_pw[r - mid]) + st2[id * 2 + 1]) % mod;
}

pair<ll, int> get_ans1(int x, int y, int id = 1, int l = 0, int r = n)
{
	if (x >= r || l >= y) return {0, -1};
	if (x <= l && r <= y) return {st1[id], r - l};
	int mid = (l + r) / 2;
	pair<ll, ll> a1 = get_ans1(x, y, id * 2, l, mid);
	pair<ll, ll> a2 = get_ans1(x, y, id * 2 + 1, mid, r);
	ll e1; int e2;
	if (a2.second == -1) return a1;
	if (a1.second == -1) return a2;
	e2 = a1.second + a2.second;
	e1 = ((a1.first * base_pw[a2.second]) + a2.first) % mod;
	return {e1, e2};
}

pair<ll, int> get_ans2(int x, int y, int id = 1, int l = 0, int r = n)
{
	if (x >= r || l >= y) return {0, -1};
	if (x <= l && r <= y) return {st2[id], r - l};
	int mid = (l + r) / 2;
	pair<ll, ll> a1 = get_ans2(x, y, id * 2, l, mid);
	pair<ll, ll> a2 = get_ans2(x, y, id * 2 + 1, mid, r);
	ll e1; int e2;
	if (a2.second == -1) return a1;
	if (a1.second == -1) return a2;
	e2 = a1.second + a2.second;
	e1 = ((a1.first * base_pw[a2.second]) + a2.first) % mod;
	return {e1, e2};
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n;
	base_pw[0] = 1;
	for(ll i = 1; i <= n; i++) base_pw[i] = (base_pw[i - 1] * base) % mod;
	for(ll i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
	}
	for(ll i = 0; i < n; i++)
	{
		modify1(a[i]);
		modify2(n - 1 - a[i]);
	//	cout << i << " " << st1[1] << " " << st2[1] << endl;
		ll dis = min(a[i], n - 1 - a[i]);
		if (!dis) continue;
		ll x1 = get_ans1(a[i] - dis, a[i]).first;
		ll x2 = get_ans2(n - (a[i] + 1 + dis), n - (a[i] + 1)).first;

//		cout << i << " # " << endl;
//		cout << a[i] - dis << " "<< a[i] << endl;
//		cout << n - (a[i] + 1 + dis) << " "<< n - (a[i] + 1) << endl;
//		cout << (n - a[i]) << " "<< (n - a[i] + dis) << endl;
//		cout << x1 << " , " << x2 << endl;
//		cout << endl << endl;

		if (x1 == x2) continue;
		return cout << "YES" << endl, 0;
	}
	cout << "NO" << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxx = 3e5 + 20, base = 10, mod = 1e8 + 9;

int st1[4 * maxx + 10], st2[4 * maxx + 10], n, a[maxx], base_pw[maxx];

void modify1(int x, int id = 1, int l = 0, int r = n)
{
	if (l + 1 == r)
	{
		st1[id] = 1;
		return;
	}
	int mid = (l + r) / 2;
	if (x < mid)
		modify1(x, id * 2, l, mid);
	else
		modify1(x, id * 2 + 1, mid, r);
	st1[id] = ((st1[id * 2] * base_pw[r - mid]) + st1[id * 2 + 1]) % mod;
}

void modify2(int x, int id = 1, int l = 0, int r = n)
{
	if (l + 1 == r)
	{
		st2[id] = 1;
		return;
	}
	int mid = (l + r) / 2;
	if (x < mid)
		modify2(x, id * 2, l, mid);
	else
		modify2(x, id * 2 + 1, mid, r);
	st2[id] = ((st2[id * 2] * base_pw[r - mid]) + st2[id * 2 + 1]) % mod;
}

pair<int, int> get_ans1(int x, int y, int id = 1, int l = 0, int r = n)
{
	if (x >= r || l >= y) return {0, -1};
	if (x <= l && r <= y) return {st1[id], r - l};
	int mid = (l + r) / 2;
	pair<int, int> a1 = get_ans1(x, y, id * 2, l, mid);
	pair<int, int> a2 = get_ans1(x, y, id * 2 + 1, mid, r);
	int e1, e2;
	if (a2.second == -1) return a1;
	if (a1.second == -1) return a2;
	e2 = a1.second + a2.second;
	e1 = ((a1.first * base_pw[a2.second]) + a2.first) % mod;
	return {e1, e2};
}

pair<int, int> get_ans2(int x, int y, int id = 1, int l = 0, int r = n)
{
	if (x >= r || l >= y) return {0, -1};
	if (x <= l && r <= y) return {st2[id], r - l};
	int mid = (l + r) / 2;
	pair<int, int> a1 = get_ans2(x, y, id * 2, l, mid);
	pair<int, int> a2 = get_ans2(x, y, id * 2 + 1, mid, r);
	int e1, e2;
	if (a2.second == -1) return a1;
	if (a1.second == -1) return a2;
	e2 = a1.second + a2.second;
	e1 = ((a1.first * base_pw[a2.second]) + a2.first) % mod;
	return {e1, e2};
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n;
	base_pw[0] = 1;
	for(int i = 1; i <= n; i++) base_pw[i] = (base_pw[i - 1] * base) % mod;
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
	}
	for(int i = 0; i < n; i++)
	{
		modify1(a[i]);
		modify2(n - 1 - a[i]);
	//	cout << i << " " << st1[1] << " " << st2[1] << endl;
		int dis = min(a[i], n - 1 - a[i]);
		if (!dis) continue;
		int x1 = get_ans1(a[i] - dis, a[i]).first;
		int x2 = get_ans2(n - (a[i] + 1 + dis), n - (a[i] + 1)).first;

//		cout << i << " # " << endl;
//		cout << a[i] - dis << " "<< a[i] << endl;
//		cout << n - (a[i] + 1 + dis) << " "<< n - (a[i] + 1) << endl;
//		cout << (n - a[i]) << " "<< (n - a[i] + dis) << endl;
//		cout << x1 << " , " << x2 << endl;
//		cout << endl << endl;

		if (x1 == x2) continue;
		return cout << "YES" << endl, 0;
	}
	cout << "NO" << endl, 0;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll maxx = 3e5 + 20, base = 5e7 + 29, mod = 1e8 + 9;

ll st1[4 * maxx + 10], st2[4 * maxx + 10], n, a[maxx], base_pw[maxx];

void modify1(ll x, ll id = 1, ll l = 0, ll r = n)
{
	if (l + 1 == r)
	{
		st1[id] = 1;
		return;
	}
	ll mid = (l + r) / 2;
	if (x < mid)
		modify1(x, id * 2, l, mid);
	else
		modify1(x, id * 2 + 1, mid, r);
	st1[id] = ((st1[id * 2] * base_pw[r - mid]) + st1[id * 2 + 1]) % mod;
}

void modify2(ll x, ll id = 1, ll l = 0, ll r = n)
{
	if (l + 1 == r)
	{
		st2[id] = 1;
		return;
	}
	ll mid = (l + r) / 2;
	if (x < mid)
		modify2(x, id * 2, l, mid);
	else
		modify2(x, id * 2 + 1, mid, r);
	st2[id] = ((st2[id * 2] * base_pw[r - mid]) + st2[id * 2 + 1]) % mod;
}

pair<ll, ll> get_ans1(ll x, ll y, ll id = 1, ll l = 0, ll r = n)
{
	if (x >= r || l >= y) return {0, -1};
	if (x <= l && r <= y) return {st1[id], r - l};
	ll mid = (l + r) / 2;
	pair<ll, ll> a1 = get_ans1(x, y, id * 2, l, mid);
	pair<ll, ll> a2 = get_ans1(x, y, id * 2 + 1, mid, r);
	ll e1, e2;
	if (a2.second == -1) return a1;
	if (a1.second == -1) return a2;
	e2 = a1.second + a2.second;
	e1 = ((a1.first * base_pw[a2.second]) + a2.first) % mod;
	return {e1, e2};
}

pair<ll, ll> get_ans2(ll x, ll y, ll id = 1, ll l = 0, ll r = n)
{
	if (x >= r || l >= y) return {0, -1};
	if (x <= l && r <= y) return {st2[id], r - l};
	ll mid = (l + r) / 2;
	pair<ll, ll> a1 = get_ans2(x, y, id * 2, l, mid);
	pair<ll, ll> a2 = get_ans2(x, y, id * 2 + 1, mid, r);
	ll e1, e2;
	if (a2.second == -1) return a1;
	if (a1.second == -1) return a2;
	e2 = a1.second + a2.second;
	e1 = ((a1.first * base_pw[a2.second]) + a2.first) % mod;
	return {e1, e2};
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n;
	base_pw[0] = 1;
	for(ll i = 1; i <= n; i++) base_pw[i] = (base_pw[i - 1] * base) % mod;
	for(ll i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
	}
	for(ll i = 0; i < n; i++)
	{
		modify1(a[i]);
		modify2(n - 1 - a[i]);
	//	cout << i << " " << st1[1] << " " << st2[1] << endl;
		ll dis = min(a[i], n - 1 - a[i]);
		if (!dis) continue;
		ll x1 = get_ans1(a[i] - dis, a[i]).first;
		ll x2 = get_ans2(n - (a[i] + 1 + dis), n - (a[i] + 1)).first;

//		cout << i << " # " << endl;
//		cout << a[i] - dis << " "<< a[i] << endl;
//		cout << n - (a[i] + 1 + dis) << " "<< n - (a[i] + 1) << endl;
//		cout << (n - a[i]) << " "<< (n - a[i] + dis) << endl;
//		cout << x1 << " , " << x2 << endl;
//		cout << endl << endl;

		if (x1 == x2) continue;
		return cout << "YES" << endl, 0;
	}
	cout << "NO" << endl, 0;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
using namespace std;
const int maxn = 3e5+10;
const int mod = 998244353;

int read(){
	int x=0,f=1;
	char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<3)+(x<<1)+c-48;c=getchar();}
	return x*f;
}

int k,m,n;
int p[maxn],a[maxn];
int f[2][maxn<<2];

inline void add(int k,int l,int r,int ad,int op){
	(f[op][k]=(long long)f[op][k]+p[ad])%=mod;
	if(l==r)return;
	int mid=(l+r)>>1;
	if(ad<=mid)add(k<<1,l,mid,ad,op);
	else add(k<<1|1,mid+1,r,ad,op);
	return;
}

int query(int k,int l,int r,int ls,int rs,int op){
	//cout<<k<<" "<<l<<" "<<r<<" "<<op<<" "<<f[op][k]<<endl;
	if(ls<=l && r<=rs)return f[op][k];
	int mid=(l+r)>>1,sum=0;
	if(mid>=ls)(sum=(long long)sum+query(k<<1,l,mid,ls,rs,op))%=mod;
	if(mid+1<=rs)(sum=(long long)sum+query(k<<1|1,mid+1,r,ls,rs,op))%=mod;
	return sum;
}

int main(){
	n=read();
	for(register int i=1;i<=n;i++)a[i]=read();p[0]=1;
	for(register int i=1;i<=n;i++)(p[i]=(long long)p[i-1]*(n+1))%mod;
	for(register int i=1;i<=n;i++){
		int len=min(a[i]-1,n-a[i]),s1=a[i],s2=n-a[i]+1;
		int h1=query(1,1,n,s1-len,s1,0),h2=query(1,1,n,s2-len,s2,1);
		if(s1<s2)(h1=(long long)h1*p[s2-s1])%=mod;
		else (h2=(long long)h2*p[s1-s2])%=mod;
		if(h1!=h2){
			puts("YES");
			return 0;
		}
		add(1,1,n,a[i],0);add(1,1,n,n-a[i]+1,1);
	}
	puts("NO");
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#define int long long
using namespace std;
const int maxn = 3e5+10;
const int mod = 1e9+7;

int read(){
	int x=0,f=1;
	char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<3)+(x<<1)+c-48;c=getchar();}
	return x*f;
}

int k,m,n;
int p[maxn],a[maxn];
int f[2][maxn<<2];

inline void add(int k,int l,int r,int ad,int op){
	(f[op][k]=(long long)f[op][k]+p[ad])%=mod;
	if(l==r)return;
	int mid=(l+r)>>1;
	if(ad<=mid)add(k<<1,l,mid,ad,op);
	else add(k<<1|1,mid+1,r,ad,op);
	return;
}

int query(int k,int l,int r,int ls,int rs,int op){
	//cout<<k<<" "<<l<<" "<<r<<" "<<op<<" "<<f[op][k]<<endl;
	if(ls<=l && r<=rs)return f[op][k];
	int mid=(l+r)>>1,sum=0;
	if(mid>=ls)(sum=(long long)sum+query(k<<1,l,mid,ls,rs,op))%=mod;
	if(mid+1<=rs)(sum=(long long)sum+query(k<<1|1,mid+1,r,ls,rs,op))%=mod;
	return sum;
}

signed main(){
	n=read();
	for(register int i=1;i<=n;i++)a[i]=read();p[0]=1;
	for(register int i=1;i<=n;i++)(p[i]=(long long)p[i-1]*(n+1))%mod;
	for(register int i=1;i<=n;i++){
		int len=min(a[i]-1,n-a[i]),s1=a[i],s2=n-a[i]+1;
		int h1=query(1,1,n,s1-len,s1,0),h2=query(1,1,n,s2-len,s2,1);
		if(s1<s2)(h1=(long long)h1*p[s2-s1])%=mod;
		else (h2=(long long)h2*p[s1-s2])%=mod;
		if(h1!=h2){
			puts("YES");
			return 0;
		}
		add(1,1,n,a[i],0);add(1,1,n,n-a[i]+1,1);
	}
	puts("NO");
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
using namespace std;
const int maxn = 3e5+10;
const int mod = 1e9+7;

int read(){
	int x=0,f=1;
	char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<3)+(x<<1)+c-48;c=getchar();}
	return x*f;
}

int k,m,n;
int p[maxn],a[maxn];
int f[2][maxn<<2];

inline void add(int k,int l,int r,int ad,int op){
	f[op][k]=((long long)f[op][k]+p[ad])%mod;
	if(l==r)return;
	int mid=(l+r)>>1;
	if(ad<=mid)add(k<<1,l,mid,ad,op);
	else add(k<<1|1,mid+1,r,ad,op);
	return;
}

int query(int k,int l,int r,int ls,int rs,int op){
	//cout<<k<<" "<<l<<" "<<r<<" "<<op<<" "<<f[op][k]<<endl;
	if(ls<=l && r<=rs)return f[op][k];
	int mid=(l+r)>>1,sum=0;
	if(mid>=ls)sum=((long long)sum+query(k<<1,l,mid,ls,rs,op))%mod;
	if(mid+1<=rs)sum=((long long)sum+query(k<<1|1,mid+1,r,ls,rs,op))%mod;
	return sum;
}

int main(){
	n=read();
	for(register int i=1;i<=n;i++)a[i]=read();p[0]=1;
	for(register int i=1;i<=n;i++)p[i]=((long long)p[i-1]*(n+1))%mod;
	for(register int i=1;i<=n;i++){
		int len=min(a[i]-1,n-a[i]),s1=a[i],s2=n-a[i]+1;
		int h1=query(1,1,n,s1-len,s1,0),h2=query(1,1,n,s2-len,s2,1);
		if(s1<s2)h1=((long long)h1*p[s2-s1])%mod;
		else h2=((long long)h2*p[s1-s2])%mod;
		if(h1!=h2){
			puts("YES");
			return 0;
		}
		add(1,1,n,a[i],0);add(1,1,n,n-a[i]+1,1);
	}
	puts("NO");
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#define int long long
using namespace std;
const int maxn = 3e5+10;
const int mod = 998244353;

int read(){
	int x=0,f=1;
	char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<3)+(x<<1)+c-48;c=getchar();}
	return x*f;
}

int k,m,n;
int p[maxn],a[maxn];
int f[2][maxn<<2];

inline void add(int k,int l,int r,int ad,int op){
	(f[op][k]=(long long)f[op][k]+p[ad])%=mod;
	if(l==r)return;
	int mid=(l+r)>>1;
	if(ad<=mid)add(k<<1,l,mid,ad,op);
	else add(k<<1|1,mid+1,r,ad,op);
	return;
}

int query(int k,int l,int r,int ls,int rs,int op){
	//cout<<k<<" "<<l<<" "<<r<<" "<<op<<" "<<f[op][k]<<endl;
	if(ls<=l && r<=rs)return f[op][k];
	int mid=(l+r)>>1,sum=0;
	if(mid>=ls)(sum=(long long)sum+query(k<<1,l,mid,ls,rs,op))%=mod;
	if(mid+1<=rs)(sum=(long long)sum+query(k<<1|1,mid+1,r,ls,rs,op))%=mod;
	return sum;
}

signed main(){
	n=read();
	for(register int i=1;i<=n;i++)a[i]=read();p[0]=1;
	for(register int i=1;i<=n;i++)(p[i]=(long long)p[i-1]*(n+1))%mod;
	for(register int i=1;i<=n;i++){
		int len=min(a[i]-1,n-a[i]),s1=a[i],s2=n-a[i]+1;
		int h1=query(1,1,n,s1-len,s1,0),h2=query(1,1,n,s2-len,s2,1);
		if(s1<s2)(h1=(long long)h1*p[s2-s1])%=mod;
		else (h2=(long long)h2*p[s1-s2])%=mod;
		if(h1!=h2){
			puts("YES");
			return 0;
		}
		add(1,1,n,a[i],0);add(1,1,n,n-a[i]+1,1);
	}
	puts("NO");
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
using namespace std;
const int maxn = 3e5+10;
const int mod = 1e9+7;

int read(){
	int x=0,f=1;
	char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<3)+(x<<1)+c-48;c=getchar();}
	return x*f;
}

int k,m,n;
int p[maxn],a[maxn];
int f[2][maxn<<2];

inline void add(int k,int l,int r,int ad,int op){
	(f[op][k]=(long long)f[op][k]+p[ad])%=mod;
	if(l==r)return;
	int mid=(l+r)>>1;
	if(ad<=mid)add(k<<1,l,mid,ad,op);
	else add(k<<1|1,mid+1,r,ad,op);
	return;
}

int query(int k,int l,int r,int ls,int rs,int op){
	//cout<<k<<" "<<l<<" "<<r<<" "<<op<<" "<<f[op][k]<<endl;
	if(ls<=l && r<=rs)return f[op][k];
	int mid=(l+r)>>1,sum=0;
	if(mid>=ls)(sum=(long long)sum+query(k<<1,l,mid,ls,rs,op))%=mod;
	if(mid+1<=rs)(sum=(long long)sum+query(k<<1|1,mid+1,r,ls,rs,op))%=mod;
	return sum;
}

int main(){
	n=read();
	for(register int i=1;i<=n;i++)a[i]=read();p[0]=1;
	for(register int i=1;i<=n;i++)(p[i]=(long long)p[i-1]*(n+1))%=mod;
	for(register int i=1;i<=n;i++){
		int len=min(a[i]-1,n-a[i]),s1=a[i],s2=n-a[i]+1;
		int h1=query(1,1,n,s1-len,s1,0),h2=query(1,1,n,s2-len,s2,1);
		if(s1<s2)(h1=(long long)h1*p[s2-s1])%=mod;
		else (h2=(long long)h2*p[s1-s2])%=mod;
		if(h1!=h2){
			puts("YES");
			return 0;
		}
		add(1,1,n,a[i],0);add(1,1,n,n-a[i]+1,1);
	}
	puts("NO");
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#define int long long
using namespace std;
const int maxn = 3e5+10;
const int mod = 1e9+7;

int read(){
	int x=0,f=1;
	char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<3)+(x<<1)+c-48;c=getchar();}
	return x*f;
}

int k,m,n;
int p[maxn],a[maxn];
int f[2][maxn<<2];

inline void add(int k,int l,int r,int ad,int op){
	(f[op][k]=(long long)f[op][k]+p[ad])%=mod;
	if(l==r)return;
	int mid=(l+r)>>1;
	if(ad<=mid)add(k<<1,l,mid,ad,op);
	else add(k<<1|1,mid+1,r,ad,op);
	return;
}

int query(int k,int l,int r,int ls,int rs,int op){
	//cout<<k<<" "<<l<<" "<<r<<" "<<op<<" "<<f[op][k]<<endl;
	if(ls<=l && r<=rs)return f[op][k];
	int mid=(l+r)>>1,sum=0;
	if(mid>=ls)(sum=(long long)sum+query(k<<1,l,mid,ls,rs,op))%=mod;
	if(mid+1<=rs)(sum=(long long)sum+query(k<<1|1,mid+1,r,ls,rs,op))%=mod;
	return sum;
}

signed main(){
	n=read();
	for(register int i=1;i<=n;i++)a[i]=read();p[0]=1;
	for(register int i=1;i<=n;i++)(p[i]=(long long)p[i-1]*(n+1))%=mod;
	for(register int i=1;i<=n;i++){
		int len=min(a[i]-1,n-a[i]),s1=a[i],s2=n-a[i]+1;
		int h1=query(1,1,n,s1-len,s1,0),h2=query(1,1,n,s2-len,s2,1);
		if(s1<s2)(h1=(long long)h1*p[s2-s1])%=mod;
		else (h2=(long long)h2*p[s1-s2])%=mod;
		if(h1!=h2){
			puts("YES");
			return 0;
		}
		add(1,1,n,a[i],0);add(1,1,n,n-a[i]+1,1);
	}
	puts("NO");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
inline int read(){
	int w = 0, f = 1; char ch = getchar();
	while(ch < '0' or ch > '9') {if(ch == '-') f = -f; ch = getchar();}
	while(ch >= '0' and ch <= '9') w = w*10 + ch - '0', ch = getchar();
	return w*f;
}
struct node{
	int l, r;
	unsigned long long sum;
}t[maxn<<2][2];
unsigned long long P[maxn];
int N, A[maxn];
void build(int p, int l, int r, int k){
	t[p][k].l = l, t[p][k].r = r;
	if(l == r) return ;
	int mid = (l + r)>>1;
	build(p<<1, l, mid, k);
	build(p<<1|1, mid+1, r, k);
}
void add(int p, int x, unsigned long long z, int k){
	if(t[p][k].l == t[p][k].r){
		t[p][k].sum = z;
		return ;
	}
	int mid = (t[p][k].l + t[p][k].r)>>1;
	if(x <= mid) add(p<<1, x, z, k);
	else add(p<<1|1, x, z, k);
	t[p][k].sum = t[p<<1][k].sum + t[p<<1|1][k].sum;
}
unsigned long long ask(int p, int l, int r, int k){
	if(t[p][k].l >= l and t[p][k].r <= r) return t[p][k].sum;
	int mid = (t[p][k].l + t[p][k].r)>>1; unsigned long long val = 0llu;
	if(l <= mid) val += ask(p<<1, l, r, k);
	if(r > mid) val += ask(p<<1|1, l, r, k);
	return val;
}
int main(){
	N = read();
	for(int i=1; i<=N; i++) A[i] = read();
	P[0] = 1llu;
	for(int i=1; i<=N; i++) P[i] = P[i-1]*233llu;
	build(1, 1, N, 0);
	build(1, 1, N, 1);
	bool flag = false;
	for(int i=1; i<=N; i++){
		int len = min(A[i]-1, N-A[i]);
		int s1 = A[i] - len, s2 = N - A[i] - len + 1;
		unsigned long long x = ask(1, s1, A[i], 0), y = ask(1, s2, N - A[i] + 1, 1);
		if(s1 > s2) y = y*P[s1 - s2];
		else x = x*P[s2 - s1];
		if(x != y) {flag = true; break;}
		add(1, A[i], P[A[i]], 0);
		add(1, N-A[i]+1, P[N-A[i]+1], 1);
	}
	return printf(flag?"YES\n":"NO\n"), 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#define int long long
using namespace std;

const int MAXN = 3e5 + 5;
const int MOD  = 1e9 + 7;
const int BASE = 23;
int inline read(){
	int x = 0, f = 0; char ch = getchar();
	while(ch < '0' || ch > '9') f ^= (ch == '-'), ch = getchar();
	while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

int n;
int a[MAXN], pow[MAXN] = {1};

struct segmentree{
	int l, r, hash, revh, len;
}tree[MAXN * 4];
int inline lc(int k) { return k << 1; }
int inline rc(int k) { return k << 1 | 1; }
int inline siz(int k) { return tree[k].r - tree[k].l + 1; }
void build(int k, int l, int r){
	tree[k].l = l; tree[k].r = r; tree[k].len = r - l + 1;
	if(l == r) return;
	int mid = l + r >> 1;
	build(lc(k), l, mid); build(rc(k), mid + 1, r);
}
void modify(int k, int pos, int v){
	if(pos < tree[k].l || tree[k].r < pos) return;
	if(pos == tree[k].l && tree[k].r == pos) tree[k].hash = tree[k].revh = v;
	else {
		modify(lc(k), pos, v); modify(rc(k), pos, v);
		tree[k].hash = (tree[lc(k)].hash * pow[siz(rc(k))] % MOD + tree[rc(k)].hash) % MOD;
		tree[k].revh = (tree[rc(k)].revh * pow[siz(lc(k))] % MOD + tree[lc(k)].revh) % MOD;
	}
}
segmentree calc(segmentree a, segmentree b){
	segmentree res;
	res.len = a.len + b.len;
	res.hash = a.hash * pow[b.len] % MOD + b.hash; res.hash %= MOD;
	res.revh = b.revh * pow[a.len] % MOD + a.revh; res.revh %= MOD;
	return res;
}
segmentree query(int k, int l, int r){
	if(r < tree[k].l || tree[k].r < l) return segmentree{0, 0, 0, 0, 0};
	if(l <= tree[k].l && tree[k].r <= r) return tree[k];
//	segmentree lefthash = query(lc(k), l, r, rev);
//	segmentree righhash = query(rc(k), l, r, rev);
//	printf("query : %5lld %7lld %7lld %7lld [ %7lld, %7lld ] : %7lld %7lld\n", k, l, r, (int)rev, tree[k].l, tree[k].r, lefthash, righhash);
	return calc(query(lc(k), l, r), query(rc(k), l, r));

//	if(rev) return (righhash * pow[max(0ll, tree[rc(k)].l - l)] % MOD + lefthash) % MOD;
//	else return (lefthash * pow[max(0ll, r - tree[lc(k)].r)] % MOD + righhash) % MOD;
}

signed main(){
	//freopen("Permutation.in", "r", stdin);

	n = read();
	for(int i = 1; i <= n; ++i) a[i] = read();
	for(int i = 1; i <= n; ++i) pow[i] = pow[i - 1] * BASE % MOD;

	build(1, 1, n);
	for(int i = 1; i <= n; ++i){
		modify(1, a[i], 1);
		int len = min(a[i] - 1, n - a[i]);
		if(query(1, a[i] - len, a[i]).revh ^ query(1, a[i], a[i] + len).hash) {
			puts("yes"), exit(0);
		}
	}
	puts("no");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#define int long long
using namespace std;

const int MAXN = 3e5 + 5;
const int MOD  = 1e9 + 7;
const int BASE = 23;
int inline read(){
	int x = 0, f = 0; char ch = getchar();
	while(ch < '0' || ch > '9') f ^= (ch == '-'), ch = getchar();
	while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

int n;
int a[MAXN], pow[MAXN] = {1};

struct segmentree{
	int l, r, hash, revh, len;
}tree[MAXN * 4];
int inline lc(int k) { return k << 1; }
int inline rc(int k) { return k << 1 | 1; }
int inline siz(int k) { return tree[k].r - tree[k].l + 1; }
void build(int k, int l, int r){
	tree[k].l = l; tree[k].r = r; tree[k].len = r - l + 1;
	if(l == r) return;
	int mid = l + r >> 1;
	build(lc(k), l, mid); build(rc(k), mid + 1, r);
}
void modify(int k, int pos, int v){
	if(pos < tree[k].l || tree[k].r < pos) return;
	if(pos == tree[k].l && tree[k].r == pos) tree[k].hash = tree[k].revh = v;
	else {
		modify(lc(k), pos, v); modify(rc(k), pos, v);
		tree[k].hash = (tree[lc(k)].hash * pow[siz(rc(k))] % MOD + tree[rc(k)].hash) % MOD;
		tree[k].revh = (tree[rc(k)].revh * pow[siz(lc(k))] % MOD + tree[lc(k)].revh) % MOD;
	}
}
segmentree calc(segmentree a, segmentree b, bool rev){
	segmentree res;
	res.len = a.len + b.len;
	if(!rev) res.hash = a.hash * pow[b.len] % MOD + b.hash, res.hash %= MOD;
	else res.revh = b.revh * pow[a.len] % MOD + a.revh, res.revh %= MOD;
	return res;
}
segmentree query(int k, int l, int r, bool rev){
	if(r < tree[k].l || tree[k].r < l) return segmentree{0, 0, 0, 0, 0};
	if(l <= tree[k].l && tree[k].r <= r) return tree[k];
	return calc(query(lc(k), l, r, rev), query(rc(k), l, r, rev), rev);
}

signed main(){
//	freopen("Permutation.in", "r", stdin);

	n = read();
	for(int i = 1; i <= n; ++i) a[i] = read();
	for(int i = 1; i <= n; ++i) pow[i] = pow[i - 1] * BASE % MOD;

	build(1, 1, n);
	for(int i = 1; i <= n; ++i){
		modify(1, a[i], 1);
		int len = min(a[i] - 1, n - a[i]);
		if(query(1, a[i] - len, a[i], 1).revh ^ query(1, a[i], a[i] + len, 0).hash) {
			puts("yes"), exit(0);
		}
	}
	puts("no");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#define int long long
using namespace std;

const int MAXN = 3e5 + 5;
const int MOD  = 1e9 + 7;
const int BASE = 23;
int inline read(){
	int x = 0, f = 0; char ch = getchar();
	while(ch < '0' || ch > '9') f ^= (ch == '-'), ch = getchar();
	while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

int n;
int a[MAXN], pow[MAXN] = {1};

struct segmentree{
	int l, r, hash, revh;
}tree[MAXN * 4];
int inline lc(int k) { return k << 1; }
int inline rc(int k) { return k << 1 | 1; }
int inline siz(int k) { return tree[k].r - tree[k].l + 1; }
void build(int k, int l, int r){
	tree[k].l = l; tree[k].r = r;
	if(l == r) return;
	int mid = l + r >> 1;
	build(lc(k), l, mid); build(rc(k), mid + 1, r);
}
void modify(int k, int pos, int v){
	if(pos < tree[k].l || tree[k].r < pos) return;
	if(pos == tree[k].l && tree[k].r == pos) tree[k].hash = tree[k].revh = v;
	else {
		modify(lc(k), pos, v); modify(rc(k), pos, v);
		tree[k].hash = (tree[lc(k)].hash * pow[siz(rc(k))] % MOD + tree[rc(k)].hash) % MOD;
		tree[k].revh = (tree[rc(k)].revh * pow[siz(lc(k))] % MOD + tree[lc(k)].revh) % MOD;
	}
}
int query(int k, int l, int r, bool rev){
	if(r < tree[k].l || tree[k].r < l) return 0;
	if(l <= tree[k].l && tree[k].r <= r) return rev ? (tree[k].revh) : (tree[k].hash);
	int lefthash = query(lc(k), l, r, rev);
	int righhash = query(rc(k), l, r, rev);
//	printf("query : %5lld %7lld %7lld %7lld [ %7lld, %7lld ] : %7lld %7lld\n", k, l, r, (int)rev, tree[k].l, tree[k].r, lefthash, righhash);
	if(rev) return (righhash * pow[max(0ll, tree[rc(k)].l - l)] % MOD + lefthash) % MOD;
	else return (lefthash * pow[max(0ll, r - tree[lc(k)].r)] % MOD + righhash) % MOD;
}

signed main(){
	freopen("Permutation.in", "r", stdin);

	n = read();
	for(int i = 1; i <= 10; ++i) a[i] = read();
	for(int i = 1; i <= n; ++i) pow[i] = pow[i - 1] * BASE % MOD;

	printf("%d ", pow[500]);

	build(1, 1, n);
	for(int i = 1; i <= 3; ++i){
		modify(1, a[i], 1);
		int len = min(a[i] - 1, n - a[i]);
		printf("%d %d %d %d\n", a[i], len, query(1, a[i] - len, a[i], 0), query(1, a[i] + len - 500, a[i] + len, 1));
//		if(query(1, a[i] - len, a[i], 0) ^ query(1, a[i], a[i] + len, 1)) {
//			if(n == 300000) printf("fuck %d", i);
//			puts("yes"), exit(0);
//		}
	}
	puts("no");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#define int long long
using namespace std;

const int MAXN = 3e5 + 5;
const int MOD  = 1e9 + 7;
const int BASE = 23;
int inline read(){
	int x = 0, f = 0; char ch = getchar();
	while(ch < '0' || ch > '9') f ^= (ch == '-'), ch = getchar();
	while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

int n;
int a[MAXN], pow[MAXN] = {1};

struct segmentree{
	int l, r, hash, revh;
}tree[MAXN * 4];
int inline lc(int k) { return k << 1; }
int inline rc(int k) { return k << 1 | 1; }
int inline siz(int k) { return tree[k].r - tree[k].l + 1; }
void build(int k, int l, int r){
	tree[k].l = l; tree[k].r = r;
	if(l == r) return;
	int mid = l + r >> 1;
	build(lc(k), l, mid); build(rc(k), mid + 1, r);
}
void modify(int k, int pos, int v){
	if(pos < tree[k].l || tree[k].r < pos) return;
	if(pos == tree[k].l && tree[k].r == pos) tree[k].hash = tree[k].revh = v;
	else {
		modify(lc(k), pos, v); modify(rc(k), pos, v);
		tree[k].hash = (tree[lc(k)].hash * pow[siz(rc(k))] % MOD + tree[rc(k)].hash) % MOD;
		tree[k].revh = (tree[rc(k)].revh * pow[siz(lc(k))] % MOD + tree[lc(k)].revh) % MOD;
	}
}
int query(int k, int l, int r, bool rev){
	if(r < tree[k].l || tree[k].r < l) return 0;
	if(l <= tree[k].l && tree[k].r <= r) return rev ? (tree[k].revh) : (tree[k].hash);
	int lefthash = query(lc(k), l, r, rev);
	int righhash = query(rc(k), l, r, rev);
	if(rev) return (righhash * pow[max(0ll, tree[rc(k)].l - l)] % MOD + lefthash) % MOD;
	else return (lefthash * pow[max(0ll, r - tree[lc(k)].r)] % MOD + righhash) % MOD;
}

signed main(){
	//freopen("Permutation.in", "r", stdin);

	n = read();
	for(int i = 1; i <= n; ++i) a[i] = read(), pow[i] = pow[i - 1] * BASE % MOD;

	build(1, 1, n);
	for(int i = 1; i <= n; ++i){
		modify(1, a[i], 1);
		int len = min(a[i] - 1, n - a[i]);
	//	printf("%d %d %d\n", len, query(1, a[i] - len, a[i], 0), query(1, a[i], a[i] + len, 1));
		if(query(1, a[i] - len, a[i], 0) ^ query(1, a[i], a[i] + len, 1)) {
			if(n == 300000) printf("fuck %d", i);
			puts("yes"), exit(0);
		}
	}
	puts("no");
	return 0;
}


a=map(int,raw_input().split())
print sum(max(0,max(a)-x-1) for x in a)


b,d,s = map(int,raw_input().split())
#print b,d,s
m = max(b,d,s)
def f(a,b):
    if a==b:
        return 0
    elif a==b+1:
        return 0
    else:
        return a-b-1
print  f(m,b)+f(m,d)+f(m,s)


in_put = sorted(map(long,raw_input().split(" ")))

golden_num = in_put[2] - 1
output = 0
if golden_num - in_put[0] > 0:
    output+= golden_num - in_put[0]
if golden_num - in_put[1] > 0:
    output+= golden_num - in_put[1]
print output


bds=map(long,raw_input().split())
ans=-1
for i in range(3):
    for j in range(3):
        n=0
        for k in range(3):
            u=bds[k]
            if k>=i:u-=1
            if k<=j:u-=1
            n=max(n,u)
        best=0
        for k in range(3):
            u=n
            if k>=i:u+=1
            if k<=j:u+=1
            u-=bds[k]
            best+=u
        if ans==-1 or best<ans:ans=best
for i in range(3):
    for j in range(i,3):
        total=[0,0,0]
        ok=True
        bet=0
        for k in range(3):
            if i<=k and k<=j: total[k]+=1
            if bds[k]>total[k]:ok=False
            bet+=total[k]-bds[k]
        if ok:
            if ans==-1 or bet<ans: ans=bet
print ans


#from operator import itemgetter, attrgetter, methodcaller
#from collections import Counter, defaultdict
#from fractions import gcd
#import sys
#import math

def main():
    m = map(int, raw_input().split())
    m.sort()
    d1 = max(0, m[2] - m[0] - 1)
    d2 = max(0, m[2] - m[1] - 1)
    print str(d1+d2)

if __name__ == "__main__":
    main()


import sys
import math

a,b,c = raw_input().split()
a = int(a)
b = int(b)
c = int(c)

m = max(a,b,c)
count = 0;
if (a == m):
    if (b+1<m):
        count += m-b-1
    if (c+1<m):
        count += m-c-1
    print(count)
    sys.exit(0)

if (b == m):
    if (a+1<m):
        count += m-a-1
    if (c+1<m):
        count += m-c-1
    print(count)
    sys.exit(0)

if (c == m):
    if (a+1<m):
        count += m-a-1
    if (b+1<m):
        count += m-b-1
    print(count)
    sys.exit(0)


import sys
import math

a,b,c = raw_input().split()
a = int(a)
b = int(b)
c = int(c)

m = max(a,b,c)
count = 0;
if (a == m):
    if (b+1<m):
        count += m-b-1
    if (c+1<m):
        count += m-c-1
    print(count)
    sys.exit(0)

if (b == m):
    if (a+1<m):
        count += m-a-1
    if (c+1<m):
        count += m-c-1
    print(count)
    sys.exit(0)

if (c == m):
    if (a+1<m):
        count += m-a-1
    if (b+1<m):
        count += m-b-1
    print(count)
    sys.exit(0)

print(count)


a = map(int, raw_input().strip().split())

a = sorted(a)
x, y, z = int(a[2]), int(a[1]), int(a[0])

x = x - z
y = y - z
res = 0
if x > y:
    res = y
    x = x - y

    if x > 0:
        res += (x - 1) * 2
else:
    if y > 0:
        res = y - 1
print res


while True :
    try :
        b, d, s = map(int, raw_input().split())
        minn = min(b, d, s)
        b = b - minn
        d = d - minn
        s = s - minn
        if b == 0 :
            b = d
            d = s
        elif d == 0 :
            d = b
            b = s
        if b == d :
            print max(0, b - 1)
            continue
        ans = min(d, b)
        minn = min(d, b)
        ans = max(0, 2 * (max(b, d) - min(b, d) - 1) + ans)
        print ans
    except :
        break;


a = map(int, raw_input().split())
print(sum(max(0, max(a) - x - 1) for x in a))


b, d, s = map(int, raw_input().split())

ans = int(1e20)
if b <= 1 and d <= 1 and s <= 1:
    if b == 1 and d == 0 and s == 1:
        ans = 1
    else:
        ans = 0

first_states = [(1, 1, 1), (0, 1, 1), (0, 0, 1), (0, 0, 0)]
last_states = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (1, 1, 1)]

for s1 in first_states:
    for s2 in last_states:
        bl, dl, sl = b - s1[0] - s2[0], d - s1[1] - s2[1], s - s1[2] - s2[2]
        extra_days = max(bl, dl, sl)
        # print extra_days, s1, s2
        ans = min(ans, extra_days * 3 + sum(s1) + sum(s2) - b - d - s)

print ans


def do_solve(a, b, c):
    u = max(b, c)
    if a in [u, u + 1]:
        return u + u - b - c
    elif a > u + 1:
        u = a - 1
        return u + u - b - c
    else:
        return u * 3 - a - b - c

def solve(a, b, c):
    return min(
        do_solve(a, b, c),
        do_solve(b, c, a),
        do_solve(c, a, b)
    )

def test():
    assert solve(3, 2, 1) == 1
    assert solve(1, 0, 0) == 0
    assert solve(100, 0, 100) == 99

(a, b, c) = map(int, raw_input().split())
print solve(a, b, c)


def do_solve(a, b, c):
    u = max(b, c)
    if a in [u - 1, u, u + 1]:
        return u + u - b - c
    elif a > u + 1:
        u = a - 1
        return u + u - b - c
    else:
        return u * 3 - a - b - c - 1

def solve(a, b, c):
    return min(
        do_solve(a, b, c),
        do_solve(b, c, a),
        do_solve(c, a, b)
    )

def test():
    assert solve(3, 2, 1) == 1
    assert solve(1, 0, 0) == 0
    assert solve(100, 0, 100) == 99

test()

(a, b, c) = map(int, raw_input().split())
print solve(a, b, c)


from collections import defaultdict
import copy,sys
sys.setrecursionlimit(5000000)
a=[int(x) for x in raw_input().split()]
a.sort(reverse=True)
x=a[0]
y=a[0]-1

print min(x-a[1]+x-a[2],x-a[1]+y-a[2],y-a[1]+y-a[2])


from collections import defaultdict
import copy,sys
sys.setrecursionlimit(5000000)
a=[int(x) for x in raw_input().split()]
if a[0]==a[1]==a[2]:
    print 0
else:
    x=max(a)
    if a[0]==a[1] and a[0]==x:
        print x-a[2]-1
    elif a[2]==a[1] and a[1]==x:
        print x-a[0]-1
    elif a[0]==a[2] and a[0]==x:
        print x-a[1]-1
    elif a[0]==x:
        print x-a[1]+x-a[2]-2
    elif a[1]==x:
        print x-a[0]+x-a[2]-2
    elif a[2]==x:
        print x-a[1]+x-a[0]-2

#print max(0,min(x-a[1]+x-a[2],x-a[1]+y-a[2],y-a[1]+y-a[2]))


from collections import defaultdict
import copy,sys
sys.setrecursionlimit(5000000)
a=[int(x) for x in raw_input().split()]
a.sort(reverse=True)
x=a[0]
y=a[0]-1

aa=x-a[1]+x-a[2]
bb=x-a[1]+y-a[2]
cc=y-a[1]+y-a[2]

if aa>=0 and bb>=0 and cc>=0:
    print min(aa,bb,cc)
elif aa>=0 and bb>=0:
    print min(aa,bb)
elif bb>=0 and cc>=0:
    print min(bb,cc)
elif cc>=0 and aa>=0:
    print min(aa,cc)
elif aa>=0:
    print aa
elif bb>=0:
    print bb
elif cc>=0:
    print cc
else:
    print 0

#print max(0,min(x-a[1]+x-a[2],x-a[1]+y-a[2],y-a[1]+y-a[2]))


from collections import defaultdict
import copy,sys
sys.setrecursionlimit(5000000)
a=[int(x) for x in raw_input().split()]
a.sort(reverse=True)
x=a[0]
y=a[0]-1

print max(0,min(x-a[1]+x-a[2],x-a[1]+y-a[2],y-a[1]+y-a[2]))


import sys
b, d, s = map(int, raw_input().split())
if len(set([b,d,s])) == 1:
  print 0
  sys.exit(0)
bc = max([b,d,s])
ans = 0

if bc == b and bc == s:
  ans = abs((bc-1)-d)
elif bc == b and bc == d:
  ans = abs((bc-1)-s)
elif bc == s and bc == d:
  ans = abs((bc-1)-b)
elif bc == b:
  #bc-1 sd
  ans = abs((bc-1)-s) + abs((bc-1)-d)
elif bc == s:
  ans = abs((bc-1)-b) + abs((bc-1)-d)
elif bc == d:
  ans = abs((bc-1)-b) + abs((bc-1)-s)

print ans


a=map(int,raw_input().split(' '))
m=max(a)
ans=0
for i in a:
    ans=ans+max(0,m-1-i)

print ans


IN = lambda : map(int, raw_input().split())
b, d, s = IN()
arrive_candidate = [[0, 0, 0], [1, 1, 1], [0, 1, 1], [0, 0, 1]]
leave_candidate = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [1, 1, 1]]
ans = float('inf')
for ar in arrive_candidate:
    for le in leave_candidate:
        sum_vec = [ar[i] + le[i] for i in xrange(3)]
        remain_vec = [b - sum_vec[0], d - sum_vec[1], s - sum_vec[2]]
        ans = min(ans, sum([max(max(remain_vec), 0) - v for v in remain_vec]))
if max(b, d, s) == 1:
    ans = 1 if [b, d, s] == [1, 0, 1] else 0
print ans


IN = lambda : map(int, raw_input().split())
b, d, s = IN()
def fq1(b, d, s):
    arrive_candidate = [[0, 0, 0], [1, 1, 1], [0, 1, 1], [0, 0, 1]]
    leave_candidate = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [1, 1, 1]]
    ans = float('inf')
    for ar in arrive_candidate:
        for le in leave_candidate:
            sum_vec = [ar[i] + le[i] for i in xrange(3)]
            remain_vec = [b - sum_vec[0], d - sum_vec[1], s - sum_vec[2]]
            ans = min(ans, sum([max(remain_vec) - v for v in remain_vec]))
    if max(b, d, s) == 1:
        ans = 1 if [b, d, s] == [1, 0, 1] else 0
def fq2(b, d, s):
    c = max(b, d, s)
    c -= 1
    ans = 2 * c - (b + d + s) + max(b, d, s)
    return ans
print fq2(b, d, s)


IN = lambda : map(int, raw_input().split())
b, d, s = IN()
def fq1(b, d, s):
    arrive_candidate = [[0, 0, 0], [1, 1, 1], [0, 1, 1], [0, 0, 1]]
    leave_candidate = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [1, 1, 1]]
    ans = float('inf')
    for ar in arrive_candidate:
        for le in leave_candidate:
            sum_vec = [ar[i] + le[i] for i in xrange(3)]
            remain_vec = [b - sum_vec[0], d - sum_vec[1], s - sum_vec[2]]
            ans = min(ans, sum([max(remain_vec) - v for v in remain_vec]))
    if max(b, d, s) == 1:
        ans = 1 if [b, d, s] == [1, 0, 1] and False else 0
    return ans
def fq2(b, d, s):
    nums = [b, d, s]
    c = max(nums)
    c -= 1
    ans = 0
    for num in nums:
        if c > num:
            ans += c - num
    #ans = 2 * c - (b + d + s) + max(b, d, s)
    return ans
'''
import random
for cnt in xrange(10000):
    b = random.randint(0, 5)
    d = random.randint(0, 5)
    s = random.randint(0, 5)
    if fq1(b, d, s) != fq2(b, d, s):
        print b, d, s
b, d, s = (1, 0, 1)
'''
print fq1(b, d, s)


IN = lambda : map(int, raw_input().split())
b, d, s = IN()
arrive_candidate = [[0, 0, 0], [1, 1, 1], [0, 1, 1], [0, 0, 1]]
leave_candidate = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [1, 1, 1]]
ans = float('inf')
for ar in arrive_candidate:
    for le in leave_candidate:
        sum_vec = [ar[i] + le[i] for i in xrange(3)]
        remain_vec = [b - sum_vec[0], d - sum_vec[1], s - sum_vec[2]]
        ans = min(ans, sum([max(remain_vec) - v for v in remain_vec]))
if max(b, d, s) == 1:
    ans = 1 if [b, d, s] == [1, 0, 1] else 0
print ans


IN = lambda : map(int, raw_input().split())
b, d, s = IN()
arrive_candidate = [[0, 0, 0], [1, 1, 1], [0, 1, 1], [0, 0, 1]]
leave_candidate = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [1, 1, 1]]
ans = float('inf')
for ar in arrive_candidate:
    for le in leave_candidate:
        sum_vec = [ar[i] + le[i] for i in xrange(3)]
        remain_vec = [b - sum_vec[0], d - sum_vec[1], s - sum_vec[2]]
        ans = min(ans, sum([max(max(remain_vec), 0) - v for v in remain_vec]))
if max(b, d, s) == 1 and [b, d, s] == [1, 0, 1]:
    ans = 1
print ans


IN = lambda : map(int, raw_input().split())
b, d, s = IN()
def fq1(b, d, s):
    arrive_candidate = [[0, 0, 0], [1, 1, 1], [0, 1, 1], [0, 0, 1]]
    leave_candidate = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [1, 1, 1]]
    ans = float('inf')
    for ar in arrive_candidate:
        for le in leave_candidate:
            sum_vec = [ar[i] + le[i] for i in xrange(3)]
            remain_vec = [b - sum_vec[0], d - sum_vec[1], s - sum_vec[2]]
            ans = min(ans, sum([max(remain_vec) - v for v in remain_vec]))
    if max(b, d, s) == 1:
        ans = 1 if [b, d, s] == [1, 0, 1] else 0
def fq2(b, d, s):
    nums = [b, d, s]
    c = max(nums)
    c -= 1
    ans = 0
    for num in nums:
        if c > num:
            ans += c - num
    #ans = 2 * c - (b + d + s) + max(b, d, s)
    return ans
print fq2(b, d, s)


IN = lambda : map(int, raw_input().split())
b, d, s = IN()
arrive_candidate = [[0, 0, 0], [1, 1, 1], [0, 1, 1], [0, 0, 1]]
leave_candidate = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [1, 1, 1]]
ans = float('inf')
for ar in arrive_candidate:
    for le in leave_candidate:
        sum_vec = [ar[i] + le[i] for i in xrange(3)]
        remain_vec = [b - sum_vec[0], d - sum_vec[1], s - sum_vec[2]]
        ans = min(ans, sum([max(remain_vec) - v for v in remain_vec]))
if max(b, d, s) == 1 and [b, d, s] == [1, 0, 1]:
    ans = 1
print ans


a = map(int, raw_input().split())
mx = max(a)
c = 0
for i in a:
    if i != mx:
        c += mx-1-i
print c


l=lambda:map(int,raw_input().split())
a=l()
mean = max(a)-1
print sum(mean-v if v<mean else 0 for v in a)


import sys

arr=map(int,sys.stdin.readline().strip().split(' '))

m = max(arr[0], max(arr[1], arr[2]))
m -= 1
a = 0

if m>arr[0]:
    a += m - arr[0]
if m>arr[1]:
    a += m - arr[1]
if m>arr[2]:
    a += m - arr[2]

print a


import sys

arr=map(int,sys.stdin.readline().strip().split(' '))

m = max(arr[0], max(arr[1], arr[2]))
m =- 1
a = 0

if m>arr[0]:
    a += m - arr[0]
if m>arr[1]:
    a += m - arr[1]
if m>arr[2]:
    a += m - arr[2]

print a


[b, d, s] = map(int, raw_input().strip().split(' '))

if b == d and d == s:
	print "0"
elif b == d:
    if d > s:
        print d - 1 - s
    else:
        print (s-1)*3 + 1 - b - d - s

elif d == s:
    if b > d:
        print (b-1)*2 - d - s

    else:
        print s - 1 - b

elif b == s:
    if b > d:
        print b - 1 - d
    else:
        print (d-2)*3 + 4 - b - d - s
else:
    if b == max(b,d,s):
        print (b-1)*3 +1 -b- d- s
    elif d == max(b,d,s):
        print (d-2)*3 + 4 - b - d -s
    else:
        print 1 + (s-1)*3 - b - d-s


[b, d, s] = map(int, raw_input().strip().split(' '))

if b == d and d == s:
	print "0"

elif b == d and d > s:
	print (d - 1)*3 + 2 - b - d - s

elif b < d  and d == s:
	print s * 3 - b - d - s

elif b > d and b == s:
	print s * 2 - b - d - 1

else:
	if b == max(b, d, s):
		print (b-1)*3 + 1 - b - d - s

	elif d == max(b, d, s):
		print (d-1)*3 + 2 - b - d - s

	else:
		print s * 3 - b - d - s


[b, d, s] = map(int, raw_input().strip().split(' '))

if b == d and d == s:
	print "0"

elif b == d and d > s:
	print (d - 1)*3 + 2 - b - d - s

elif b < d  and d == s:
	print s * 3 - b - d - s

elif b > d and b == s:
	print s * 2 - b - d

else:
	if b == max(b, d, s):
		print (b-1)*3 + 1 - b - d - s

	elif d == max(b, d, s):
		print (d-1)*3 + 2 - b - d - s

	else:
		print s * 3 - b - d - s


[b, d, s] = map(int, raw_input().strip().split(' '))

if b == d and d == s:
	print "0"
if b == d:
    if d > s:
        print d - 1 - s
    else:
        print (s-1)*3 + 1 - b - d - s

elif d == s:
    if b > d:
        print (b-1)*2 - d - s

    else:
        print s - 1 - b

elif b == s:
    if b > d:
        print b - 1 - d
    else:
        print (d-2)*3 + 4 - b - d - s
else:
    if b == max(b,d,s):
        print (b-1)*3 +1 -b- d- s
    elif d == max(b,d,s):
        print (d-2)*3 + 4 - b - d -s
    else:
        print 1 + (s-1)*3 - b - d-s


def twoch(b,d,f):
    if b==f and d==f:
        return 2*(s-1)-2*f
    return 0
b,d,s=map(int,raw_input().split())
x=b+d+s
if x==1:
    print 0
elif b==d==s:
    print 0
else:
    ans=0
    f=max(b,d,s)
    if b<f-1:
        ans+=f-1-b
    if d<f-1:
        ans+=f-1-d
    if s<f-1:
        ans+=f-1-s
    print ans


a = map(int, raw_input().split())
ans = max(a) * 3 - sum(a)
if max(a) == 1:
    ans = 0
for start in range(3):
    for end in range(3):
        total = [0 if ind < start and ind > end else (2 if ind >= start and ind <= end else 1) for ind in range(3)]
        k = 0
        for i in range(3):
            k = max(k, a[i] - total[i])
        for i in range(3):
            total[i] += k
        current = 0
        for i in range(3):
            current += total[i] - a[i]
        ans = min(ans, current)
print ans


a = map(int, raw_input().split())
ans = max(a) * 3 - sum(a)
for start in range(3):
    for end in range(3):
        total = [0 if ind < start and ind > end else (2 if ind >= start and ind <= end else 1) for ind in range(3)]
        k = 0
        for i in range(3):
            k = max(k, a[i] - total[i])
        for i in range(3):
            total[i] += k
        current = 0
        for i in range(3):
            current += total[i] - a[i]
        ans = min(ans, current)
print ans


a, b, c = map(long, raw_input().split())
mx = max(a, b, c)
mxa, mxb, mxc = mx, mx, mx
if a < mxa:
    mxa -= 1
    if b < mxb:
        mxb -= 1
if c < mxc:
    mxc -= 1
    if b < mxb:
        mxb -= 1
print mxa + mxb + mxc - a - b - c


def func(x,y,z):
    m = max(x,y,z)
    r = 3*m -x - y-z
    if(z<m):
        r-=1
    if(z<m and y<m):
        r-=1
    return r
x,y,z = map(int,raw_input().split())
r = func(x,y,z)
r = min(r,func(x+1,y,z))
r = min(r,func(x+1,y+1,z))
print r


inp = [int(x) for x in raw_input().split()]
ans = max(inp)*3
for i in xrange(3):
    num = inp[:]
    temp = (max(num)*3) - sum(num)
    ans = min(ans,temp)
    temp = (max(num)-num[0])+max((max(num)-1)-num[1],0)+max((max(num)-1)-num[2],0)
    ans = min(ans,temp)
    temp = (max(num)-num[0])+(max(num)-num[1])+max((max(num)-1)-num[2],0)
    inp = inp[1:]+[inp[0]]
print ans


a=map(int,raw_input().split(' '))
a=sorted(a)
count=0
if (a[2]==a[0]):
    count=0
elif (a[2]==a[1]):
    count=1
else:
    count=2
print (3*a[2]-count)-sum(a)


s=raw_input().split()
a=int(s[0])
b=int(s[1])
c=int(s[2])
m= max(a,b,c)
count=0
for i in (a,b,c):
	if (m-i-1)>0:
		count+=m-i-1
print count


z, o, u = [int(a) for a in raw_input().split()]
x = max (z, o, u)-1
print max(0,x-z)+max(0,x-o)+max(0,x-u)


import sys

meals = [int(i) for i in sys.stdin.readline().strip().split()]

maximum = max(meals)
answer = 0
for m in meals:
    if maximum - m > 1:
        answer += maximum - m - 1

print answer


a,b,c = raw_input().split(" ")
a,b,c = int(a),int(b),int(c)
m = min([a, b c])
a -= m
b -= m
c -= m
total = max([a-1, b-1, c-1])
ans = max(total - a, 0)	+ max(total - b, 0) + max(total	- c, 0)
print(ans)


a,b,c = raw_input().split(" ")
a,b,c = int(a),int(b),int(c)
ans = max([0,a-1-min([a,b,c]),b-1-min([a,b,c]),c-1-min([a,b,c])])
print(ans)


a,b,c = raw_input().split(" ")
a,b,c = int(a),int(b),int(c)
m = min([a, b, c])
a -= m
b -= m
c -= m
total = max([a-1,	b-1, c-1])
ans = max(total - a, 0)	+ max(total - b, 0) + max(total	- c, 0)
print(ans)


a,b,c = raw_input().split(" ")
a,b,c = int(a),int(b),int(c)
m = min([a, b c])
a -= m
b -= m
c -= m
total = max([a-1,	b-1, c-1])
ans = max(total - a, 0)	+ max(total - b, 0) + max(total	- c, 0)
print(ans)


def read(t=None):
	string = raw_input()
	return string if t is None else [t(x) for x in string.split()]

def solve(a, b):
	c = [x+y for x, y in zip(a, b)]
	f = [True if x >= 0 else False for x in c]
	if not all(f):
		4 * pow(10, 18)
	m = max(c)
	return sum([m-x for x in c])

if __name__ == "__main__":
	a = read(int)

	r = [0 for i in range(7)]
	r[0] = solve(a, [0, 0, 0])
	r[1] = solve(a, [-1, 0, 0])
	r[2] = solve(a, [0, -1, 0])
	r[3] = solve(a, [0, 0, -1])
	r[4] = solve(a, [-1, -1, 0])
	r[5] = solve(a, [0, -1, -1])
	r[6] = solve(a, [-1, 0, -1])

	print min(r)


a = []

def read(t=None):
	string = raw_input()
	return string if t is None else [t(x) for x in string.split()]

def compute(b):
	global a
	b = [x-y for x,y in zip(a,b)]
	#print "b: %s"%b

	if any([x < 0 for x in b]):
		return 2 * pow(10, 18)

	d = max(b)
	#print "d: %d"%d
	return sum([d-x for x in b])

if __name__ == "__main__":
	a = read(int)
	ans = []
	ans.append(compute([0, 0, 0]))
	ans.append(compute([1, 0, 0]))
	ans.append(compute([1, 1, 0]))
	ans.append(compute([0, 1, 0]))
	ans.append(compute([0, 1, 1]))
	ans.append(compute([0, 0, 1]))
	ans.append(compute([1, 0, 1]))

	print min(ans)


x=raw_input().split(' ')
a=int (x[0])
b=int (x[1])
c=int (x[2])
arr=[]
arr.append(a)
arr.append(b)
arr.append(c)

arr.sort()
arr.reverse()

sm=0
if arr[0]>arr[1]:
    sm=sm+(arr[0]-1-arr[1])+(arr[0]-1-arr[2])
elif arr[0]==arr[1]:
    if arr[1]>arr[2]:
        sm=sm+(arr[1]-1-arr[2])

print sm


(b, s, d) = map(int, raw_input().split())
days = max(b,s,d)
print max(days-b-1, 0) + max(days-s-1, 0) + max(days-d-1, 0)


b,d,s=map(int,raw_input().split())
t=max(b,d,s)
c=0
if t!=b:
    c+=t-b-1
if t!=d:
    c+=t-d-1
if t!=s:
    c+=t-s-1
print c


b, d, s = map(int, raw_input().split())

temp = min(b, d)
temp = min(temp, s)
b-=temp
d-=temp
s-=temp
temp = max(b, d)
temp = max(temp, s)
tempB = temp
tempD = temp
tempS = temp
ans = abs(b - tempB) + abs(s - tempS) + abs(d - tempD)
tempS-=1
ans = min(ans, abs(b - tempB) + abs(s - tempS) + abs(d - tempD))
tempB-=1
ans = min(ans, abs(b - tempB) + abs(s - tempS) + abs(d - tempD))
tempB+=1
tempD-=1
ans = min(ans, abs(b - tempB) + abs(s - tempS) + abs(d - tempD))
tempS+=1
ans = min(ans, abs(b - tempB) + abs(s - tempS) + abs(d - tempD))
tempB-=1
ans = min(ans, abs(b - tempB) + abs(s - tempS) + abs(d - tempD))
tempD+=1
ans = min(ans, abs(b - tempB) + abs(s - tempS) + abs(d - tempD))

print ans


a=map(int,raw_input().split())
print sum(max(0,max(a)-x-1) for x in a)


meals = [int(x) for x in raw_input().split()]
tot = max(meals)-1
miss = 0
for x in meals:
    if x < tot:
        miss += tot-x
print miss


#! /usr/bin/env python

remembered = [int(m) for m in raw_input().split(' ')]

largest = max(remembered) - 1
print sum([max(0,largest-m) for m in remembered])


def go():
    b,d,s = [int(i) for i in raw_input().split(" ")]
    m = max(b,d,s)
    t = 0
    t+= m-b
    t+= m-d
    t+= m-s
    if m==b and m==s and m==d:
        return t
    elif m==b and m==s:
        t-=1
    elif m==s and m==d:
        t-=1
    elif m==b and m==d:
        t-=1
    else:
        t-=2

    return t
print go()


meal = [int(x) for x in raw_input().split(' ')]
maxx = max(meal)
total = 0
for m in meal:
    if maxx > m:
        total += maxx - m - 1
print total


# Hello python

l = map(int, raw_input().split())

res = 0;

for i in l:
	if max(l) != i:
		res += max( max(l) - i - 1, 0)

print res


l = map(int, raw_input().split())
print sum(max(0, max(l) - i - 1) for i in l)


from math import *
a, b, c = map(int, raw_input().split())
ans = 10000000000000000000000000000000
mx = max(a,b,c)
tmp = 0
ans = mx - a + mx - b + mx - c
if mx == a and mx != b and mx != c:
	tmp = (mx-1-b) + (mx-1-c)
else:
	tmp = (mx+1-a) + mx - b + mx - c
ans = min(ans, tmp)
if (mx == a or mx == b) and mx != c:
	tmp = mx-a + mx-b + (mx-1) - c
else:
	tmp = mx+1-a + mx+1-b + mx-c
ans = min(ans, tmp)

if(mx == b and mx != a and mx != c):
	tmp = mx-1-a + mx-1-c
else:
	tmp = mx+1-b + mx -a + mx - c
ans = min(ans, tmp)

if((mx == b or mx == c) and mx != a):
	tmp = mx-b + mx-c + mx-1-a
else:
	tmp = mx+1-b + mx+1-c + mx-a
ans = min(ans, tmp)

if(mx == c and mx != b and mx != a):
	tmp = mx-1-b + mx-1-a
else:
	tmp = mx+1-c + mx-a + mx-b
ans = min(ans, tmp)

if((mx == a or mx == c) and mx != b):
	tmp = mx-a + mx-c + mx-1-b
else:
	tmp = mx+1-a + mx-b +mx+1-c
ans = min(ans, tmp)

print(ans)


from math import *
a, b, c = map(int, raw_input().split())
ans = 10000000000000000000000000000000
mx = max(a,b,c)
tmp = 0
ans = mx -a + mx-b + mx -c
if mx == a and mx != b and mx != c:
	tmp = (mx-1-b) + (mx-1-c)
else:
	tmp = (mx+1-a) + mx - b + mx - c
ans = min(ans, tmp)
if (mx == a or mx == b) and mx != c:
	tmp = mx-a + mx-b + (mx-1) - c
else:
	tmp = mx+1-a + mx+1-b + mx-c
ans = min(ans, tmp)

if(mx == b and mx != a and mx != c):
	tmp = mx-1-a + mx-1-c
else:
	tmp = mx+1-b + mx -a + mx - c
ans = min(ans, tmp)

if((mx == b or mx == c) and mx != a):
	tmp = mx-b + mx-c + mx-1-a
else:
	tmp = mx+1-b + mx+1-c + mx-a
ans = min(ans, tmp)

if(mx == c and mx != b and mx != a):
	tmp = mx-b + mx-a
else:
	tmp = mx+1-c + mx-a + mx-b
ans = min(ans, tmp)

if((mx == a or mx == c) and mx != b):
	tmp = mx-a + mx-c + mx-1-b
else:
	tmp = mx+1-a + mx-b +mx+1-c
ans = min(ans, tmp)

print(ans)


a = map(int, raw_input().split())
s = max(a) - 1
res = 0
for i in range(len(a)):
    if a[i] != max(a):
        res += s - a[i]
print(res)


b, d, s = map(int, raw_input().split())
x = b+d+s
minim = 3*max(b,d,s)
minim = min(minim, 1+3*max(b-1, d, s))
minim = min(minim, 2+3*max(b-1, d-1, s))
minim = min(minim, 1+3*max(b, d-1, s))
minim = min(minim, 2+3*max(b, d-1, s-1))
minim = min(minim, 1+3*max(b, d, s-1))
minim = min(minim, 2+3*max(b-1, d, s-1))
print minim - x


def foo():
    b, d, s = map(int, raw_input().split())
    max_day = max(b, d, s)
    ans = 0
    if b < max_day:
        ans += max_day - b - 1
    if d < max_day:
        ans += max_day - d - 1
    if s < max_day:
        ans += max_day - s - 1
    print ans

if __name__ == '__main__':
    foo()


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
long long int max(long long int a,long long int b)
{
	if(a>=b)
		return a;
	else
		return b;
}
int main()
{
	long long int b,d,s,ans,m,sum;
	scanf("%lld %lld %lld",&b,&d,&s);
	if((b==d)&&(d==s))
		ans=0;
	else
	{
		if(b==d)
		{
			if((s==(b-1))||(s==(b+1)))
				ans=0;
			else
				ans=b-1-s;
		}
		else if(d==s)
		{
			if((b==(d-1))||(b==(d+1)))
				ans=0;
			else
				ans=d-1-b;
		}
		else if(b==s)
		{
			if((d==(b-1))||(d==(b+1)))
				ans=0;
			else
				ans=b-1-d;
		}
		else
		{
			sum=b+s+d;
			m=max(b,max(s,d));
			ans=(3*m)-sum-2;
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
long long int max(long long int a,long long int b)
{
	if(a>=b)
		return a;
	else
		return b;
}
int main()
{
	long long int b,d,s,ans,m,sum;
	scanf("%lld %lld %lld",&b,&d,&s);
	if((b==d)&&(d==s))
		ans=0;
	else
	{
		if(b==d)
		{
			if((s==(b-1))||(s==(b+1)))
				ans=0;
			else
			{
				if((b-1)>s)
					ans=b-1-s;
				else
					ans=(2*(s-1))-b-d;
			}
		}
		else if(d==s)
		{
			if((b==(d-1))||(b==(d+1)))
				ans=0;
			else
			{
				if((d-1)>b)
					ans=d-1-b;
				else
					ans=(2*(b-1))-s-d;
			}
		}
		else if(b==s)
		{
			if((d==(b-1))||(d==(b+1)))
				ans=0;
			else
			{
				if((b-1)>d)
					ans=b-1-d;
				else
					ans=(2*(d-1))-b-s;
			}
		}
		else
		{
			sum=b+s+d;
			m=max(b,max(s,d));
			ans=(3*m)-sum-2;
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include <stdio.h>
#include <math.h>

int main()
{
    long long int a,b,x,y,c;
    scanf("%lld%lld%lld",&a,&b,&c);
    x=a;
    y=0;
    if(x<b)
        x=b;
    if(x<c)
        x=c;
    if(a<x-1)
        y=y+(x-a-1);
    if(b<x-1)
        y=y+(x-b-1);
    if(c<x-1)
        y=y+(x-c-1);
        printf("%lld",y);
    return 0;
}


#include<stdio.h>
int main()
{
  long long  int a,b,c;
    scanf("%lli%lli%lli",&a,&b,&c);

     if(a<b)
     {
         a=a+b;
         b=a-b;
         a=a-b;
     }
     if(c>a)
     {
         a=a+b+c;
         b=a-(b+c);
         c=a-(b+c);
         a=a-(b+c);
     }
    else if(c>b)
    {
        b=b+c;
        c=b-c;
        b=b-c;
    }

    if(a>b)
        a--;
       else if(a==b&&a!=c)
    c++;
        printf("%lli",a-b+a-c);

}


#include <stdio.h>
int main()
{
	long long int b,s,d,t,ans=0;
	scanf("%I64lld%I64lld%I64lld",&b,&s,&d);
	   if(b<d)
		{
			t=d;
			d=b;
			b=t;
		}
		if(b<s)
		{
			t=b;
			b=s;
			s=t;
		}

		if(s<d)
		{
			t=d;
			d=s;
			s=t;
		}
		printf("%lld%lld%lld\n",b,s,d);
		if(b==s&&s==d)
			ans=0;
		else if(b>s)
			ans=b-1-s+b-1-d;
		else if(b==s)
			ans=b-1-d;
		printf("%I64lld",ans);
		return 0;
}


#include <stdio.h>
int main()
{
	long long int b,s,d,t,ans=0;
	scanf("%I64lld%I64lld%I64lld",&b,&s,&d);
	   if(b<d)
		{
			t=d;
			d=b;
			b=t;
		}
		if(b<s)
		{
			t=b;
			b=s;
			s=t;
		}

		if(s<d)
		{
			t=d;
			d=s;
			s=t;
		}
		if(b==s&&s==d)
			ans=0;
		else if(b>s)
			ans=b-1-s+b-1-d;
		else if(b==s)
			ans=b-1-d;
		printf("%I64lld",ans);
		return 0;
}


#include <stdio.h>
int main()
{
	long long int b,s,d,t,ans;
	scanf("%I64lld%I64lld%I64lld",&b,&s,&d);
	if(b<s)
		{
			t=b;
			b=s;
			s=t;
		}
		if(s<d)
		{
			t=d;
			d=s;
			s=t;
		}
		if(b<d)
		{
			t=d;
			d=b;
			b=t;
		}
		if(b==s==d)
			ans=0;
		else if(b>s)
			ans=b-1-s+b-1-d;
		else if(b==s)
			ans=b-1-d;
		printf("%I64lld",ans);
		return 0;
}


#include <stdio.h>
int main()
{
	long long int b,s,d,t,ans;
	scanf("%I64lld%I64lld%I64lld",&b,&s,&d);
	if(b<s)
		{
			t=b;
			b=s;
			s=t;
		}
		if(s<d)
		{
			t=d;
			d=s;
			s=t;
		}
		if(b<d)
		{
			t=d;
			d=b;
			b=t;
		}
		if(b==s&&s==d)
			ans=0;
		else if(b>s)
			ans=b-1-s+b-1-d;
		else if(b==s)
			ans=b-1-d;
		printf("%I64lld",ans);
		return 0;
}


#include <stdio.h>
int main()
{
	long long int b,s,d,t,ans=0;
	scanf("%I64lld%I64lld%I64lld",&b,&s,&d);
	   if(s<d)
		{
			t=d;
			d=s;
			s=t;
		}
		if(b<s)
		{
			t=b;
			b=s;
			s=t;
		}

		if(b<d)
		{
			t=d;
			d=b;
			b=t;
		}
		if(b==s&&s==d)
			ans=0;
		else if(b>s)
			ans=b-1-s+b-1-d;
		else if(b==s)
			ans=b-1-d;
		printf("%I64lld",ans);
		return 0;
}


#include <stdio.h>

int main() {
	long long b, d, s, ans;

	scanf("%lld%lld%lld", &b, &d, &s);
	if (b == d && d == s) {
		ans = 0;
	} else {
		long long tmp;

		if (b > d)
			tmp = b, b = d, d = tmp;
		if (b > s)
			tmp = b, b = s, s = tmp;
		if (d > s)
			tmp = d, d = s, s = tmp;
		b--;
		ans = ((s - d) - 1) + ((d - b) - 1);
	}
	printf("%lld\n", ans);
	return 0;
}


#include <stdio.h>

int main() {
	long long b, d, s, tmp;

	scanf("%lld%lld%lld", &b, &d, &s);
	if (b > d)
		tmp = b, b = d, d = tmp;
	if (b > s)
		tmp = b, b = s, s = tmp;
	if (d > s)
		tmp = d, d = s, s = tmp;
	printf("%lld\n", (b < s - 1 ? s - 1 - b : 0) + (d < s - 1 ? s - 1 - d : 0));
	return 0;
}


#include <stdio.h>
int main()
{
    long long a,b,c;
    while(~scanf("%lld%lld%lld",&a,&b,&c))
    {
         long long cnt = 0;
        if(a>=b&&a>=c)
        {
            a--;
            if(a>b) cnt+=a-b;
            if(a>c) cnt+=a-c;
        }
        else if(b>=a&&b>=c)
        {
            b--;
            if(b>a) cnt+=b-a;
            if(b>c) cnt+=b-c;
        }
        else if(c>=a&&c>=b)
        {
            c--;
            if(c>a) cnt+=c-a;
            if(c>b) cnt+=c-b;
        }
        printf("%lld\n",cnt);
    }
    return 0;
}


#include <stdio.h>

int main(void)
{
    long long int b,d,s,days=0,i;
    scanf("%lld%lld%lld",&b,&d,&s);
    for(i=1;i<=4;i++)
    {
        if(i==1)
        days++;
        if(i==2)
        b--;
        if(i==3)
        d--;
        if(i==4){
        s--;i=1;}
        if((b||d||s)==0)
        break;
    }
    printf("%lld %lld %lld\n",b,d,s);
    return 0;
}


#include <stdio.h>

int main(void)
{
    long long int b,d,s,max=0,i,sum=0;
    scanf("%lld%lld%lld",&b,&d,&s);
    max=b;
    if(d>max)max=d;
    if(s>max)max=s;
    max--;
    if(b<max)sum=sum+max-b;
    if(d<max)sum=sum+max-d;
    if(s<max)sum=sum+max-s;
 printf("%lld\n",sum);
 return 0;
}


#include<stdio.h>
int main()
{
    long long int   b,d,s,x;
    scanf("%lld%lld%lld",&b,&d,&s);
    if(b>=d&&b>=s)
    {
        x=b-d-1+b-d;
    }
    if(d>=s&&d>=b)
    {
        x=d-b-1+d-s-1;
    }
    if(s>=d&&s>=b)
    {
        x=s-d-1+s-b-1;
    }
    printf("%lld",x);
    return 0;
}


#include <stdio.h>

int main()
{
    long long b, d, s, tot, max;
    scanf("%I64d %I64d %I64d", &b, &d, &s);
    max = (b>d) ? ((b>s) ? b : s) : ((d>s) ? d : s);
    if(max==b) b--;
    if(max==d) d--;
    if(max==s) s--;
    tot = 3 * (max-1) - (b + d + s);
    printf("%I64d\n", tot);
    return 0;
}


#include<stdio.h>
int main()
{
    long long int   b,d,s,x;
    scanf("%lld%lld%lld",&b,&d,&s);
    if(b>=d&&b>=s)
    {
        x=b-d-1+b-s-1;
    }
    if(d>=s&&d>=b)
    {
        x=d-b-1+d-s-1;
    }
    if(s>=d&&s>=b)
    {
        x=s-d-1+s-b-1;
    }
    printf("%lld",x);
    return 0;
}


#include<stdio.h>
int main()
{
    long long int   b,d,s,x;
    scanf("%lld%lld%lld",&b,&d,&s);
    x=0;
    if(b>d&&b>s)
    {
        x=b-d-1+b-s-1;
    }
    if(d>s&&d>b)
    {
        x=d-b-1+d-s-1;
    }
    if(s>d&&s>b)
    {
        x=s-d-1+s-b-1;
    }
    printf("%lld",x);
    return 0;
}


#include <stdio.h>
int main(void)
{
    long long int b,d,s,max=0,sum=0;
    scanf("%lld%lld%lld",&b,&d,&s);
    max=b;
    if(d>max)max=d;
    if(s>max)max=s;
    max--;
    if(b<max)sum=sum+max-b;
    if(d<max)sum=sum+max-d;
    if(s<max)sum=sum+max-s;
 printf("%lld\n",sum);
 return 0;
}


#include <stdio.h>
long long int Min(long long int a,long long int b,long long int c){
   long long int ans=(a<b)?((a<c)?a:c):((b<c)?b:c);
   return ans;
}

long long int Max(long long int a,long long int b,long long int c){
    long long int ans=(a>b)?((a>c)?a:c):((b>c)?b:c);
    return ans;
}
int main()
{   //jai mata di
    long long int a[3],m,ans=0,m1,m2,m3;
    scanf("%I64d%I64d%I64d",&a[0],&a[1],&a[2]);

    m=Max(a[0],a[1],a[2]);
    m1=((m-1)>a[0])?(m-1-a[0]):0;
    m2=((m-1)>a[1])?(m-1-a[1]):0;
    m3=((m-1)>a[2])?(m-1-a[2]):0;
    ans=m1+m2+m3;
    printf("%I64d",ans);

    return 0;
}


#include <stdio.h>
int max(int a,int b){
    return a>b?a:b;
}
int min(int a,int b){
    return a>b?b:a;
}
int main(){
    long int a,b,c;
    long int d,e,f;
    d=max(a,max(b,c));
    f=min(a,min(b,c));
    e=a+b+c-d-f;
    if(d==e&&e==f)
    printf("%d",0);
    else if(d==1&&e==1&&f==0)
    printf("%d",0);
    else
    printf("%ld",2*d-e-f-2);
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    char s[10001],c[100000];
    scanf("%s",s);
    strcpy(c,s);
    strrev(s);
    strcat(c,s);
    printf("%s",c);
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	int i;
	char a[1000];
	gets(a);
	for(i=0;i<strlen(a);i++)
	{
		printf("%c",a[i]);
	}
	for(i=strlen(a)-1;i>=0;i--)
	{
		printf("%c",a[i]);
	}
	return 0;
}


#pragma warning(disable:4996)
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include <stdlib.h>
int lol(const void *x1, const void *x2)
{
	return *(int*)x1 - *(int*)x2;
}
/*
int main(void)
{
	int n,x,y, flag =1;
	scanf("%i", &n);
	for (x = 4; x <= 999996 && flag; x+=2)
		for (y = 999996; y >= 4 && flag; y-=2)
			if (((n - y) / x == 1 || (n - x) / y == 1) && ((n - y) % x == 0 || (n - x) % y == 0))
				flag = 0;
	printf("%i %i", x,y);
	return 0;
}
*/
/*
int gcd(int a, int b)
{
	int c;
	while (b)
	{
		c = a % b;
		a = b;
		b = c;
	}
	return a;
}*/
int main(void)
{
	int i = 0;
	char c, c1[1001];
	do
	{
		scanf("%c", &c);
		if (c != '\n')
		{
			c1[i++] = c;
		}
	} while (c != '\n');
	c1[i] = '\0';
	for (i = 0; c1[i] != '\0'; i++)
		printf("%c", c1[i]);
	for (i = 0; c1[i+1] != '\0'; i++)
		printf("%c", c1[i]);
	return 0;
}


#pragma warning(disable:4996)
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include <stdlib.h>
int lol(const void *x1, const void *x2)
{
	return *(int*)x1 - *(int*)x2;
}
/*
int main(void)
{
	int n,x,y, flag =1;
	scanf("%i", &n);
	for (x = 4; x <= 999996 && flag; x+=2)
		for (y = 999996; y >= 4 && flag; y-=2)
			if (((n - y) / x == 1 || (n - x) / y == 1) && ((n - y) % x == 0 || (n - x) % y == 0))
				flag = 0;
	printf("%i %i", x,y);
	return 0;
}
*/
/*
int gcd(int a, int b)
{
	int c;
	while (b)
	{
		c = a % b;
		a = b;
		b = c;
	}
	return a;
}*/
int main(void)
{
	int i = 0;
	char c, c1[1001];
	do
	{
		scanf("%c", &c);
		if (c != '\n')
		{
			c1[i++] = c;
		}
	} while (c != '\n');
	c1[i] = '\0';
	for (i = 0; c1[i] != '\0'; i++)
		printf("%c", c1[i]);
	i -= 2;
	for (; i>=0; i--)
		printf("%c", c1[i]);
	return 0;
}


#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
long long cmpfunc (const void * a, const void * b) {
   return ( *(long long*)a - *(long long*)b );
}

int main(void){

long long int test,i,j,n,count,flag=0,o1=0,o2=0,b1,x,m,l,max,sum2,min,f,c,r,o,sum1,sum=0,y,a,b,count1=0,a2,b2;
char k[1000000];

scanf("%s",k);
printf("%s",k);
for(i=strlen(k)-1;i>=0;i--){
    printf("%c",k[i]);
}
return 0;
}


#include<stdio.h>
#include<string.h>
int main()
    {
        char str[100000],i;
        scanf("%s",str);
        int n=strlen(str);
        for(i=0;i<n;i++)
        {
            printf("%c",str[i]);
        }
        for(int j=i-2;j>=0;j--)
        {
            printf("%c",str[j]);
        }
    }


#include<stdio.h>
#include<string.h>
int main()
    {
        char str[100000],str2[100000],i;
        scanf("%s",str);
        strcpy(str2,str);
        strrev(str2);
        strcat(str,str2);
        printf("%s",str);

    }


#include<stdio.h>
#include<string.h>
int main()
{
	char x[1009];
	scanf("%s",&x);
	printf("%s",x);
	int len=strlen(x);
	int i=0;
	for(i=0;i<len;i++)
	{
	    printf("%c",x[len-i-1]);
	}
	printf("\n");
	return 0;
}


//problerm 932A
//https://codeforces.com/problemset/problem/932/A
/*A. Суперпалиндром
ограничение по времени на тест
2 секунды
ограничение по памяти на тест
256 мегабайт
ввод
стандартный ввод
вывод
стандартный вывод

Дана строка A. Найдите строку B такую, что B является палиндромом, и A входит в B как подпоследовательность.

Подпоследовательность строки — это строка, полученная удалением некоторых символов (не обязательно подряд идущих) этой строки без изменения порядка остальных символов. Например, «cotst» это подпоследовательность строки «contest».

Строка называется палиндромом, если она одинаково читается слева направо и справа налево.

Длина строки B должна быть не более 104. Гарантируется, что найдётся хотя бы одна подходящая строка.

Вам не требуется найти кратчайший ответ, единственное ограничение — длина B не должна превышать 104.
Входные данные

В первой строке содержится строка A (1 ≤ |A| ≤ 103), состоящая из строчных букв английского алфавита, где |A| — это длина строки A.
Выходные данные

Выведите строку B, состоящую только из строчных букв английского алфавита. Вам не требуется найти кратчайший ответ, единственное ограничение — длина B не должна превышать 104. Если ответов несколько, выведите любую подходящую строку B.
Примеры
Входные данные
Скопировать

aba

Выходные данные
Скопировать

aba

Входные данные
Скопировать

ab

Выходные данные
Скопировать

aabaa

Примечание

В первом тестовом примере «aba» является подпоследовательностью строки «aba», которая является палиндромом.

Во втором тестовом примере «ab» является подпоследовательностью строки «aabaa», которая является палиндромом.*/
#include <stdio.h>
#include <string.h>

const int MAX = 1e4;

int main (){
int i;
char s[MAX];
scanf("%s",s);
i = strlen(s) - 1;
do{
	printf("%c",s[i]);
	i--;
	} while (i>-1);
printf("%s",s);
printf("\n");
return 0-0-0;
}


#include<stdio.h>
#include<string.h>
int main()
{
             int i,j;
             char s[1005],ch[10005],rev[1004];
             scanf("%s",s);
             int len=strlen(s);
             for(i=0,j=len-1;i<len;i++)
             {

                         rev[i]=s[j];
                         j--;

             }

              printf("%s",(strcat(s,rev)));

return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
             int i,j;
             char s[1005],ch[10005],rev[1004];
             scanf("%s",s);
             int len=strlen(s);
             for(i=0,j=len-1;i<len;i++)
             {

                         rev[i]=s[j];
                         j--;
                }
                   int k=0;
                for(i=0;i<len;i++)
                {
                    ch[i]=s[i];

                }

                    printf("%s",ch);
                for(j=i,k=0;j<(len+len);j++)
                {
                    ch[j]=rev[k];
                    printf("%c",ch[j]);
                    k++;
                }
                ch[i]='\0';

                  printf("\n");

return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
             int i,j;
             char s[1005],ch[10005],rev[1004];
             scanf("%s",s);
             int len=strlen(s);
             for(i=0,j=len-1;i<len;i++)
             {

                         rev[i]=s[j];
                         j--;
                }

            printf("%s\n",(strcat(s,rev)));

return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void fun(char *);
void fun(char *s)
{
    char t[1001];
    int i;
    int len=strlen(s);
    for(i=0;i<len;i++)
    {
        t[i]=s[len-i-1];
    }
    t[i]='\0';
    printf("%s%s\n",s,t);
}
int main()
{
    char a[1001];
    scanf("%s",a);
    fun(a);
    return 0;
}


#include <stdio.h>
#include <string.h>
char a[1005], b[3005];
int main()
{
    int i;
    scanf("%s", a);
    for (i=0;i <strlen(a);i++)
        b[i]=a[strlen(a)-(i+1)];
    strcat(b, a);
    printf("%s", b);
    return 0;
}


#include<stdio.h>
#include<string.h>
int main(){
	char a[1000],b[1000];
	gets(a);
	printf("%s",a);
	printf("%s",strrev(a));
}


#include <stdio.h>
#include <string.h>

int main()
{
    char a[20002], b[20002];
    gets(a);
    strcpy(b, a);
    strrev(b);
    strcat(a,b);
    puts(a);

    return 0;
}


#include <stdio.h>
#include <string.h>

#define N 1000

int main() {
	static char s[N + N + 1];
	int n, i;

	scanf("%s", s);
	n = strlen(s);
	for (i = n; i < n + n; i++)
		s[i] = s[n + n - 1 - i];
	s[n + n] = 0;
	printf("%s\n", s);
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{

    char a[100000];
    int i,j,l;
    scanf("%s",&a);
    l=strlen(a);
    for(i=0; i<l; i++)
    {
        printf("%c",a[i]);
    }
    for(i=l-1; i>=0; i--)
    {
        printf("%c",a[i]);
    }

    return 0;

}


#include<stdio.h>
#include<string.h>
int main()
{
    int l,i;
    char s[1000001],m[1000];
    gets(s);
    strcpy(m,s);
    strrev(m);
    strcat(s,m);
    printf("%s",s);
}


#include<stdio.h>
#include<string.h>
int main()
{
    int l,i;
    char s[1000001],m[1000];
    gets(s);
    l=strlen(s);
    for(i=0;i<l;i++)
        m[i]=s[i];
    strrev(m);
    strcat(s,m);
    printf("%s",s);
}


#include<stdio.h>
#include<string.h>
int main()
{
    int l,i;
    char s[1000001],m[1000];
    gets(s);
    l=strlen(s);
    strcpy(m,s);
    strrev(m);
    strcat(s,m);
    printf("%s",s);
}


#include<stdio.h>
#include<string.h>
int main()
{
    int l,i;
    char s[100001],m[1000];
    gets(s);
    l=strlen(s);
    for(i=0;i<l;i++)
        m[i]=s[i];
    strrev(m);
    strcat(s,m);
    printf("%s",s);
}


#include<stdio.h>
#include<string.h>
int main()
{
    int l,i;
    char s[1000001],m[10000];
    gets(s);
    strcpy(m,s);
    strrev(m);
    strcat(s,m);
    printf("%s",s);
}


#include<stdio.h>
#include<string.h>

int main(){
    char st[200000],st2[200000];
    int len,i;
    scanf("%s",st);
    len = strlen(st);
    for(i=0;i<len;i++){
        st2[i] = st[len-1-i];
    }
    printf("%s%s",st,st2);
    return 0;
}


# include <stdio.h>
# include <string.h>
int main()
{
    char a[1001];
    scanf("%s",a);
    int l=strlen(a),i,j,m=0;
    i=0;
    j=l-1;
    while(1)
    {
        if(i>j)
        {
            break;
        }
        else
        {

        }
        if(a[i]==a[j])
        {
            i++;
            j--;
        }
        else
        {
            m=1;
            break;
        }
    }
    if(m==0)
    {
        printf("%s",a);
    }
    else
    {
        for(i=0;i<l;i++)
        {
            printf("%c",a[i]);
        }
        for(i=l-2;i>=0;i--)
        {
            printf("%c",a[i]);
        }
    }
    return 0;
}