#include<iostream>
using namespace std;
int main()
{
    int t; cin>>t;
    while(t--)
    {
        string a,b,c; cin>>a>>b>>c;
        int l=0;
        int n=a.length();
        for(int i=0;i<n;i++)
        {
            if(a[i]!=b[i]&&(a[i]==c[i]||b[i]==c[i])||(a[i]==b[i]&&b[i]==c[i])){
                l++;
            }
            else {cout<<"NO\n"; break;}
        }
        if(l==n) cout<<"YES\n";
    }

}


#include<iostream>
using namespace std;
int main()
{
    int t; cin>>t;
    while(t--)
    {
        int n; cin>>n;
        int l=1,a[n],mini=INT_MAX,maxi=-1;
        for(int i=0;i<n;i++) cin>>a[i];
        for(int i=0;i<n;i++){
            if(a[i]!=-1) {l=0; break;}
        }
        if(l==1){ cout<<0<<" "<<0<<"\n";}
        else{
            for(int i=0;i<n;i++){
                if(a[i]!=-1&&a[i]<mini) mini=a[i];
                if(a[i]>maxi) maxi=a[i];
            }
            cout<<abs(maxi-mini-(maxi-mini)/2)<<" "<<(mini+(maxi-mini)/2)<<"\n";
        }
    }

}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
using namespace std;
typedef double DO;
typedef long long INT;
typedef pair<int,int> pii;
typedef pair<pii,int> pi3;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

#define NN 110
int T;
char a[NN], b[NN], c[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	for (cin>>T; T--;) {
		cin>>a+1>>b+1>>c+1;
		int n=strlen(a+1);
		int flag=0;
		for (int i=1; i<=n; i++) {
			if (a[i]!=c[i] and c[i]!=b[i])  {flag=1; break;}
		}
		puts(flag ? "NO" : "YES");
	}
	return 0;
}


#include<bits/stdc++.h>

#define NN 110

using namespace std;

typedef long long INT;

char a[NN],b[NN],c[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int t;
	cin>>t;
	while(t--){
		int flag=0;
		cin>>a>>b>>c;
		int n=strlen(a);
		for(int i=0;i<n;i++){
			if(a[i]!=c[i] and b[i]!=c[i]){
				flag=1;
				break;
			}
		}
		if(flag)puts("NO");
		else puts("YES");
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
char a[100100],b[100100],c[100100];
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%s%s%s",a,b,c);
		int n=strlen(a);
		for(int i=0;i<n;i++){
			if(c[i]!=a[i] and c[i]!=b[i]) {puts("NO");break;}
			if(i==n-1) puts("YES");
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char a[111],b[111],c[111];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int t;
	cin>>t;
	while(t--){
		cin>>a>>b>>c;
		int n=strlen(a);
		int f=0;
		for(int i=0;i<n;i++){
			if(a[i]==c[i] or b[i]==c[i]) continue;
			else {f=1;break;}
		}
		if(f) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	}
	return 0;
}


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

typedef long long INT;
typedef pair<int, int> pii;

char a[NN], b[NN], c[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int test;
	scanf("%d", &test);
	while(test--){
		cin>>a>>b>>c;
		int L = strlen(a);
		int flag = 1;
		for(int i = 0; i < L; i++)
			if(c[i] != a[i] && c[i] != b[i]) {flag = 0; break;}
		if(flag) puts("YES");
		else puts("NO");
	}
	return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second

using namespace std;

typedef long long INT;
typedef pair<int,int> pii;

const int NN=1010;
const int inf=0x3f3f3f3f;

char a[NN],b[NN],c[NN];

main(){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
#endif
	int t;
	cin>>t;
	while(t--){
		cin>>a>>b>>c;
		int n=strlen(a),f=1;

		for(int i=0;i<n;i++){
			if(a[i]!=b[i] && b[i]!=c[i] &&c[i]!=a[i]){
				puts("NO");
				f=0;
				break;
			}
			if(a[i]==b[i] && a[i]!=c[i]){
				puts("NO");
				f=0;
				break;
			}
		}
		if(f)puts("YES");
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--){
        string a,b,c;
        cin>>a>>b>>c;
        int l=a.size(),d=0,e=0;
        for(int i=0;i<l;i++){
            if(a[i]==b[i]||c[i]==b[i]){
                    d++;
            }
            if(b[i]==a[i]||c[i]==a[i]){
                    e++;
            }

        }
        //cout<<e<<d;
        if((d+e)==l)cout<<"YES"<<endl;
        else if((d+e)==2*l)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--){
        string a,b,c;
        cin>>a>>b>>c;
        int l=a.size();
        for(int i=0;i<l;i++){
            if(a[i]==b[i]||c[i]==b[i]){
                    cout<<"YES"<<endl;
                    break;
            }
            else {
                    cout<<"NO"<<endl;
                    break;
            }
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--){
        string a,b,c;
        cin>>a>>b>>c;
        int l=a.size(),d=0,e=0;
        for(int i=0;i<l;i++){
            if(a[i]==b[i]||c[i]==b[i]){
                    d++;
            }
            if(b[i]==a[i]||c[i]==a[i]){
                    e++;
            }

        }
        if((d+e)==l)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--){
        string a,b,c;
        cin>>a>>b>>c;
        int l=a.size(),d=0,e=0;
        for(int i=0;i<l;i++){
           if(a[i]==c[i]){
            b[i]=c[i];
           }
           else if(a[i]!=c[i]){
            a[i]=c[i];
           }

        }
        if(a==b)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;

    }
}


#include <iostream>
#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <cmath>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define endl '\n'
#define spc " "
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define inf 1000000000000007

int main()

{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ll t;
	cin>>t;
	while(t--){
		string a,b,c;
		cin>>a;
		cin>>b;
		cin>>c;
		ll nahi=0;
		f(i,0,a.length()){
			if(c[i]!=b[i] && c[i]!=a[i]){
				nahi=1;
				break;
			}
		}
		if(nahi==0){
			cout<<"YES"<<endl;
		}
		else{
			cout<<"NO"<<endl;
		}
	}

}


#include <iostream>
#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <cmath>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define endl '\n'
#define spc " "
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define inf 1000000000000007

int main()

{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ll t;
	cin>>t;
	while(t--){
		string a,b,c;
		cin>>a>>b>>c;
		ll z=0;
		f(i,0,a.length()){
			if(a[i]!=b[i]){
				if(c[i]==b[i]){
					a[i]=c[i];
					z++;
				}
				else if(c[i]==a[i]){
					b[i]=c[i];
					z++;
				}
			}
		}
		ll nahi=0;
		f(i,0,a.length()){
			if(a[i]!=b[i]){
				nahi=1;
			}
		}
		if(nahi==0 && z==a.size()){
			cout<<"YES"<<endl;
		}
		else{
			cout<<"NO"<<endl;
		}
	}

}


#include <iostream>
#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <cmath>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define endl '\n'
#define spc " "
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define inf 1000000000000007

int main()

{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ll t;
	cin>>t;
	while(t--){
		string a,b,c;
		cin>>a>>b>>c;
		ll z=0,nahi=0;
		f(i,0,a.length()){
			if(a[i]!=b[i]){
				if(c[i]==b[i]){
					a[i]=c[i];
					z++;
				}
				else if(c[i]==a[i]){
					b[i]=c[i];
					z++;
				}
			}
			else{
				nahi=1;
			}
		}
		f(i,0,a.length()){
			if(a[i]!=b[i]){
				nahi=1;
			}
		}
		if(nahi==0 && z==a.size()){
			cout<<"YES"<<endl;
		}
		else{
			cout<<"NO"<<endl;
		}
	}

}


#include <iostream>
#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <cmath>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define endl '\n'
#define spc " "
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define inf 1000000000000007

int main()

{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ll t;
	cin>>t;
	while(t--){
		string a,b,c;
		cin>>a>>b>>c;
		f(i,0,a.length()){
			if(a[i]!=b[i]){
				if(c[i]==b[i]){
					a[i]=c[i];
				}
				else if(c[i]==a[i]){
					b[i]=c[i];
				}
			}
		}
		ll nahi=0;
		f(i,0,a.length()){
			if(a[i]!=b[i]){
				nahi=1;
			}
		}
		if(nahi==1){
			cout<<"NO"<<endl;
		}
		else{
			cout<<"YES"<<endl;
		}
	}

}


#include <iostream>
#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <cmath>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define endl '\n'
#define spc " "
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define inf 1000000000000007

int main()

{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ll t;
	cin>>t;
	while(t--){
		string a,b,c;
		cin>>a>>b>>c;
		ll z=0;
		f(i,0,a.length()){
			if(a[i]!=b[i]){
				if(c[i]==b[i]){
					a[i]=c[i];
					z++;
				}
				else if(c[i]==a[i]){
					b[i]=c[i];
					z++;
				}
			}
		}
		ll nahi=0;
		f(i,0,a.length()){
			if(a[i]!=b[i]){
				nahi=1;
			}
		}
		if(nahi==1 || z!=a.size()){
			cout<<"NO"<<endl;
		}
		else{
			cout<<"YES"<<endl;
		}
	}

}


#include <iostream>
#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <cmath>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define endl '\n'
#define spc " "
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define inf 1000000000000007

int main()

{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ll t;
	cin>>t;
	while(t--){
		string a,b,c;
		cin>>a>>b>>c;
		ll nahi=0;
		f(i,0,a.length()){
			if(a[i]==b[i]){
				nahi=1;
			}
			else if(c[i]!=b[i] && c[i]!=a[i]){
				nahi=1;
			}
		}
		if(nahi==0){
			cout<<"YES"<<endl;
		}
		else{
			cout<<"NO"<<endl;
		}
	}

}


#include <bits/stdc++.h>
#define ll long long int

using namespace std;

int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
      string a,b,c;
      cin>>a>>b>>c;
      ll l=a.length();
      ll cnt=0;
      for(ll i=0;i<l;i++)
      {
          if(a[i]!=b[i])
          {
              if(c[i]==b[i])
              {
                  swap(c[i],a[i]);
                  cnt++;
              }
              else if(c[i]==a[i])
              {
                  swap(c[i],b[i]);
                  cnt++;
              }
          }
      }
      cout<<a<< " "<<b<< " "<<c<<endl;
      ll f=0;
      for(ll i=0;i<l;i++)
      {
          if(a[i]!=b[i])
          {
              f=1;
              break;
          }
      }
      if(f || cnt<l)
      {
          cout<< "NO"<<endl;
      }
      else
      {
          cout<< "YES"<<endl;
      }

    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int

using namespace std;

int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
      string a,b,c;
      cin>>a>>b>>c;
      ll l=a.length();
      ll cnt=0;
      for(ll i=0;i<l;i++)
      {
        if((c[i]==b[i] || c[i]==a[i]))
          {
                  cnt++;

      }
      }

      if(cnt==l)
      {
          cout<< "YES"<<endl;
      }
      else
      {
          cout<< "NO"<<endl;
      }

    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int

using namespace std;

int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
      string a,b,c;
      cin>>a>>b>>c;
      ll l=a.length();
      ll cnt=0;
      for(ll i=0;i<l;i++)
      {
          if(a[i]!=b[i])
          {
              if(c[i]==b[i])
              {
                 // swap(c[i],a[i]);
                  cnt++;
              }
              else if(c[i]==a[i])
              {
                 // swap(c[i],b[i]);
                  cnt++;
              }
          }
      }
     // cout<<a<< " "<<b<< " "<<c<<endl;

      if(cnt==l)
      {
          cout<< "YES"<<endl;
      }
      else
      {
          cout<< "NO"<<endl;
      }

    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int

using namespace std;

int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
      string a,b,c;
      cin>>a>>b>>c;
      ll l=a.length();
      ll cnt=0;
      for(ll i=0;i<l;i++)
      {
          if(a[i]!=b[i])
          {
              if(c[i]==b[i])
              {
                  swap(c[i],a[i]);
                  cnt++;
              }
              else if(c[i]==a[i])
              {
                  swap(c[i],b[i]);
                  cnt++;
              }
          }
      }
     // cout<<a<< " "<<b<< " "<<c<<endl;
      ll f=0;
      for(ll i=0;i<l;i++)
      {
          if(a[i]!=b[i])
          {
              f=1;
              break;
          }
      }
      if(f || cnt<l)
      {
          cout<< "NO"<<endl;
      }
      else
      {
          cout<< "YES"<<endl;
      }

    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int

using namespace std;

int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
      string a,b,c;
      cin>>a>>b>>c;
      ll l=a.length();
      ll cnt=0;
      for(ll i=0;i<l;i++)
      {
          if(a[i]!=b[i] && (c[i]==b[i] || c[i]==a[i]))
          {

                 // swap(c[i],a[i]);
                  cnt++;

      } }
     // cout<<cnt<<endl;
     // cout<<a<< " "<<b<< " "<<c<<endl;

      if(cnt==l)
      {
          cout<< "YES"<<endl;
      }
      else
      {
          cout<< "NO"<<endl;
      }

    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int

using namespace std;

int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
      string a,b,c;
      cin>>a>>b>>c;
      ll l=a.length();
      ll cnt=0;
      for(ll i=0;i<l;i++)
      {
          if(a[i]!=b[i])
          {
              if(c[i]==b[i] || c[i]==a[i])
              {
                 // swap(c[i],a[i]);
                  cnt++;
              }

          }
      }
     // cout<<a<< " "<<b<< " "<<c<<endl;

      if(cnt==l)
      {
          cout<< "YES"<<endl;
      }
      else
      {
          cout<< "NO"<<endl;
      }

    }
    return 0;
}


#include<bits/stdc++.h>
#define forn(i,n)  for(int i=0;i<int(n);i++)
using namespace std;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int test;
string a,b,c;
cin>>test;
while(test--){
        int flag=1;
cin>>a>>b>>c;
forn(i,a.length()){
        if(!(a[i]==c[i] || b[i]==c[i])) flag=0;
}
if(flag==0) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;

}


#include<bits/stdc++.h>
#define forn(i,n)  for(int i=0;i<int(n);i++)
using namespace std;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int test;
string a,b,c;
cin>>test;
while(test--){
        int flag=0;
cin>>a>>b>>c;
forn(i,a.length()){
        if(!(a[i]==c[i] || b[i]==a[i])) flag=1;
}
if(flag==1) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;

}


#include<bits/stdc++.h>
#define forn(i,n)  for(int i=0;i<int(n);i++)
using namespace std;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int test;
string a,b,c;
cin>>test;
while(test--){
        int flag=0;
cin>>a>>b>>c;
forn(i,a.length()){
        if(a[i]==b[i]==c[i]) continue;
        else if(a[i]==c[i] && b[i]!=c[i]) continue;
        else if(a[i]!=c[i] && b[i]==c[i]) continue;
        else flag=1;
}
if(flag==1) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;

}


#include<bits/stdc++.h>
#define forn(i,n)  for(int i=0;i<int(n);i++)
using namespace std;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int test;
string a,b,c;
cin>>test;
while(test--){
        int flag=0;
cin>>a>>b>>c;
forn(i,a.length()){
        if(!a[i]==c[i] || !b[i]==a[i]) flag=1;
}
if(flag==1) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;

}


#include <iostream>
using namespace std;

void swap(char *p, char *q)
{
    char temp;
    temp = *p;
    *p = *q;
    *q = temp;
}

int main()
{
	int t;
	cin>>t;
	while(t>0)
	{
	    string a,b,c;
	    int counter = 0;
	    cin>>a>>b>>c;
	    for(int i=0;i<=c.length(); i++)
	    {
	        if(c[i] == b[i] && b[i] != a[i])
	        {
	            swap(&a[i], &c[i]);
	            counter++;
	        }
	        else if(c[i] == a[i] && a[i] != b[i])
	        {
	            swap(&b[i], &c[i]);
	            counter++;
	        }
	    }
	    if(counter == c.length())
	    {
	        cout<<"YES";
	    }
	    else{
	        cout<<"NO";
	    }
	    cout<<endl;
	    counter = 0;
	    t--;
	}
	return 0;
}


#include <iostream>
using namespace std;

void swap(char *p, char *q)
{
    char temp;
    temp = *p;
    *p = *q;
    *q = temp;
}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	    string a,b,c;
	    int counter = 0;
	    cin>>a>>b>>c;
	    for(int i=0;i<=c.length(); i++)
	    {
	        if(c[i] == b[i] && b[i] != a[i])
	        {
	            swap(&a[i], &c[i]);
	            counter++;
	        }
	        else if(c[i] == a[i] && a[i] != b[i])
	        {
	            swap(&b[i], &c[i]);
	            counter++;
	        }
	    }
	    if(counter == c.length())
	    {
	        cout<<"YES";
	    }
	    else{
	        cout<<"NO";
	    }
	    cout<<endl;
	    counter = 0;

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define FI first
#define SE second
#define PB push_back
#define MP make_pair
#define MOD 1000000007
#define INF 1152921504606846976LL
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<vector<ll> > matrix;

int main() {

    cin.sync_with_stdio(0);
    cin.tie(0);
    cin.exceptions(cin.failbit);

    ll t;
    cin >> t;
    while (t--) {

        string a, b, c;
        int flag = 1;
        cin >> a >> b >> c;
        rep(i, 0, a.size()) {
            if (a[i] == b[i]) {
                if (c[i] != a[i]) {
                    flag = 0;
                    break;
                }
            } else {
                if (c[i] == a[i])
                    swap(c[i], b[i]);
                else if (c[i] == b[i])
                    swap(c[i], a[i]);
                else {
                    flag = 0;
                    break;
                }
            }

        }
        if (flag)
            cout << "YES\n";
        else
            cout << "NO\n";
    }
    return 0;
}


#include <iostream>
using namespace std;

void swap(char *p, char *q)
{
    char temp;
    temp = *p;
    *p = *q;
    *q = temp;
}

int main() {
	int t;

	cin>>t;
	while(t>0)
	{
	    string a,b,c;
	    int counter = 0;
	    cin>>a>>b>>c;
	    for(int i=0;i<=c.length(); i++)
	    {
	        if(c[i] == b[i] && b[i] != a[i])
	        {
	            swap(&a[i], &c[i]);
	            counter++;
	        }
	        else if(c[i] == a[i] && a[i] != b[i])
	        {
	            swap(&b[i], &c[i]);
	            counter++;
	        }
	    }
	    if(counter == c.length())
	    {
	        cout<<"YES"<<endl;
	    }
	    else{
	        cout<<"NO"<<endl;
	    }
	    counter = 0;
	    t--;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a,b,c,d,i,j,k,l,n,m;
    string s,ss,sss;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>s>>ss>>sss;
        if(ss==sss&&sss!=s)
        {
            cout<<"YES"<<endl;
        }
        else
        {
            c=0;
            for(j=0; j<s.size(); j++)
            {
                if(s[j]!=ss[j])
                {
                    if((s[j]==sss[j])
                        ||(ss[j]==sss[j]))
                    {
                        c++;
                    }
                    else
                    {
                        break;
                    }
                }

            }
            if(c==s.size())cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a,b,c,d,i,j,k,l,n,m;
    string s,ss,sss;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>s>>ss>>sss;

            c=0;
            for(j=0; j<s.size(); j++)
            {
                if(s[j]!=ss[j])
                {
                    if((s[j]==sss[j])
                        ||(ss[j]==sss[j]))
                    {
                        c++;
                    }
                    else
                    {
                        break;
                    }
                }

            }
            if(c==s.size())cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a,b,c,d,i,j,k,l,n,m;
    string s,ss,sss;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>s>>ss>>sss;

            c=0;
            for(j=0; j<s.size(); j++)
            {
                if(s[j]!=ss[j])
                {
                    if((s[j]==sss[j])
                        ||(ss[j]==sss[j]))
                    {
                        c++;
                    }
                    else
                    {
                        break;
                    }
                }
                else if(s[j]==sss[j])c++;

            }
            if(c==s.size())cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a,b,c,d,i,j,k,l,n,m;
    string s,ss,sss;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>s>>ss>>sss;
        if(ss==sss)
        {
            cout<<"YES"<<endl;
        }
        else
        {
            c=0;
            for(j=0; j<s.size(); j++)
            {
                if(s[j]!=ss[j])
                {
                    if((s[j]==sss[j])
                        ||(ss[j]==sss[j]))
                    {
                        c++;
                    }
                    else
                    {
                        break;
                    }
                }

            }
            if(c==s.size())cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }

}


#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
	int t;
	cin>>t;

	for (int i = 0; i < t; ++i)
	{
		string a,b,c;
		cin>>a>>b>>c;
		int flag=0;
		for (int i = 0; i < a.length(); ++i)
		{

			if(a[i]!=b[i])
			{
				flag=0;
			 	if(a[i] == c[i] || b[i] == c[i])
			 	{
			 		flag=1;
			 	}
			}
		}
		if(flag)
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;
}


#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
	int t;
	cin>>t;

	for (int i = 0; i < t; ++i)
	{
		string a,b,c;
		cin>>a>>b>>c;
		int flag=0,cnt=0;
		for (int i = 0; i < a.length(); ++i)
		{
			for(int i=0;i<a.length();i++)
		    {
		        if(c[i]==a[i]||c[i]==b[i])
		        cnt++;
		    }
		}
		if(cnt==c.length())
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;
}


#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
	int t;
	cin>>t;

	for (int i = 0; i < t; ++i)
	{
		string a,b,c;
		cin>>a>>b>>c;
		int flag=0,cnt=0;
		for (int i = 0; i < a.length(); ++i)
		{
			  if(c[i]==a[i]||c[i]==b[i])
		        	cnt++;
		}
		if(cnt==a.length())
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;
}


#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
	int t;
	cin>>t;

	for (int i = 0; i < t; ++i)
	{
		string a,b,c;
		cin>>a>>b>>c;
		int flag=0;
		for (int i = 0; i < a.length(); ++i)
		{
			flag=0;
			if(a[i]!=b[i])
			{
			 	if(a[i] == c[i] || b[i] == c[i])
			 	{
			 		flag=1;
			 	}
			}
		}
		if(flag)
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;
}


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

#define ll long long
vector<ll>v;
int main()
{
    ll i,j,k;
    cin>>k;
    while(k--)
    {
        ll n,a;
        bool flag = false;
        cin>>n;
        for(i=0; i<n; i++)
        {
            cin>>a;
            v.push_back(a);
        }
        ll mi=INT_MAX,ma=0;
        for(i=0; i<n-1; i++)
        {
            if(v[i]==-1 and  v[i+1]!=-1)
            {
                mi=min(mi,v[i+1]);
                ma=max(ma,v[i+1]);
               flag = true ;
            }
        }
        reverse(v.begin(),v.end());
        for(i=0; i<n-1; i++)
        {
            if(v[i]==-1 and v[i+1]!=-1)
            {
                mi=min(mi,v[i+1]);
                ma=max(ma,v[i+1]);
               flag = true ;
            }
        }

       // cout << ma <<" -- "<<mi;

        ll best=0;
        if(flag)
            best=(ma+mi)/2;
        for(i=0; i<n; i++)
        {
            if(v[i]==-1)v[i]=best;
        }
        ll diff=0;
        for(i=0; i<n-1; i++)
        {
            diff=max(diff,abs(v[i]-v[i+1]));
        }
        cout<<diff<<" "<<best<<endl;
        v.clear();
    }
}


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

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

	int arr[n], tmp=n,counter=1;
	for (int i=0 ; i<n ; i++){
		cin >> arr[i];
		if (arr[i]==tmp-1){
			counter++;
			//cout << arr[i] << " " << counter << endl;
			tmp=arr[i];
		}
	}
//	cout << "ini counter " << counter << endl;
	if(counter==n && n!=2){
		cout << n-1 << endl;
		return 0;
	}
	vector <int> longest;

	for (int i=0 ; i<n ; i++){
		int L=arr[i], cnt=1;
		for (int j=i;j<n;j++){
			if (arr[j]==L+1){
				cnt++;
				L=arr[j];
			}
		}
		if (cnt == n){
			cout << 0 << endl;
			return 0;
		}
		else if(cnt==0){
			cout << n-1 << endl;
			return 0;
		}
		else if(cnt > n/2){
			cout << n-cnt << endl;
			return 0;
			}
		else {
			longest.push_back(cnt);
		}
	}

	sort(longest.begin(),longest.end());
	int x=longest[longest.size()-1];

	int res=n-x;
	cout << res << endl;
	return 0;
}


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

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

	int arr[n];
	for (int i=0 ; i<n ; i++){
		cin >> arr[i];
	}
	vector <int> longest;

	for (int i=0 ; i<n ; i++){
		int L=arr[i], cnt=1;
		for (int j=i;j<n;j++){
			if (arr[j]==L+1){
				cnt++;
				L=arr[j];
			}
		}
		if (cnt == n){
			cout << 0 << endl;
			return 0;
		}
		else if(cnt==0){
			cout << n-1 << endl;
			return 0;
		}
		else if(cnt > n/2){
			cout << n-cnt << endl;
			return 0;
			}
		else {
			longest.push_back(cnt);
		}
	}

	sort(longest.begin(),longest.end());
	int x=longest[longest.size()-1];

	int res=n-x;
	cout << res << endl;
	return 0;
}


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

int main(){
	int n,L=1;
	cin >> n;
	int R=n;

	int arr[n];
	for (int i=0 ; i<n ; i++){
		cin >> arr[i];
	}

	int cnt=1,hitung=1;

	for (int i=0 ; i<n ; i++){
		if (arr[i]==L+1){
			cnt++;
			L=arr[i];
			//cout << cnt << " ";
		}
	}
//	cout << endl;

	for (int i=n-1 ; i>=0 ; i--){
		if (arr[i]==R-1){
			hitung++;
			R=arr[i];
//			cout << hitung << " ";
		}
	}
//	cout << endl;

	int res=min(n-cnt,n-hitung);
	cout << res << endl;
	return 0;
}


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

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

	int arr[n];
	for (int i=0 ; i<n ; i++){
		cin >> arr[i];
	}

	vector <int> longest;

	for (int i=0 ; i<n ; i++){
		int L=arr[i], cnt=1;
		for (int j=i;j<n;j++){
			if (arr[j]==L+1){
				cnt++;
				L=arr[j];
			}
		}
		if (cnt == n){
			cout << 0 << endl;
			return 0;
		}
		else if(cnt==1){
			cout << n-1 << endl;
			return 0;
		}
		else if(cnt > n/2){
			cout << n-cnt << endl;
			return 0;
			}
		else {
			longest.push_back(cnt);
		}
	}

	sort(longest.begin(),longest.end());
	int x=longest[longest.size()-1];

	int res=n-x;
	cout << res << endl;
	return 0;
}


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

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

	int arr[n+1],x;

	for (int i=1;i<n+1;i++){
		cin >> x;
		arr[x]=i;
	}

	int cnt=1,ans=1;
	for (int j=1;j<n;j++){
		if (arr[j]<arr[j+1]){
			cnt++;
		}
		else{
			ans=max(ans,cnt);
			cnt=1;
		}
		ans=max(ans,cnt);
	}

	cout << n-ans<< endl;
	return 0;
}


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

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

	int arr[n];
	for (int i=0 ; i<n ; i++){
		cin >> arr[i];
	}
	vector <int> longest;

	for (int i=0 ; i<n ; i++){
		int L=arr[i], cnt=1;
		for (int j=i;j<n;j++){
			if (arr[j]==L+1){
				cnt++;
				L=arr[j];
			}
		}
		longest.push_back(cnt);
	}

	sort(longest.begin(),longest.end());
	int x=longest[longest.size()-1];

	int res=n-x;
	cout << res << endl;
	return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <map>
#include <string.h>
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>

using namespace std;
#define set_precision cout << fixed << setprecision(10)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define replb(i, a, b) for (ll i = a; i >= b; i--)
#define repl(i, a, b) for (ll i = a; i < b; i++)
#define repit(arr) for (auto it = arr.begin(); it != arr.end(); it++)
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define vpi vector<pair<int,int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<int>())
#define ssort(arr) stable_sort(arr.begin(), arr.end())
#define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end())
#define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(),, greater<int>())
#define search(arr, c) binary_search(arr.begin(), arr.end(), c)
#define pb push_back
#define ll long long
#define cl_bf cin.ignore(numeric_limits<streamsize>::max(), '\n');
#define castl static_cast<ll>
int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
int nCr(int n, int r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
int cel(int a,int b){return((a-1)/b+1);}
int gcd(int a, int b)
{
    if (a < b)
        swap(a, b);
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
int lcm(int a,int b){
    return (a*b)/gcd(a,b);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int a,b,ans=-1,n;
    cin >> n;vi v(n),chk(n+1);
    rep(i,0,n){cin>>v[i];chk[v[i]]=i;}
    a=1;b=1;
    while(b<n){
        a=1;
        rep(i,b,n){
            if(chk[i]>chk[i+1]){
                b=i+1;
                ans=max(a,ans);
                break;
            }
            a++;
        }
    }
    cout<<n-ans;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <map>
#include <string.h>
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>

using namespace std;
#define set_precision cout << fixed << setprecision(10)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define replb(i, a, b) for (ll i = a; i >= b; i--)
#define repl(i, a, b) for (ll i = a; i < b; i++)
#define repit(arr) for (auto it = arr.begin(); it != arr.end(); it++)
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define vpi vector<pair<int,int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<int>())
#define ssort(arr) stable_sort(arr.begin(), arr.end())
#define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end())
#define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(),, greater<int>())
#define search(arr, c) binary_search(arr.begin(), arr.end(), c)
#define pb push_back
#define ll long long
#define cl_bf cin.ignore(numeric_limits<streamsize>::max(), '\n');
#define castl static_cast<ll>
int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
int nCr(int n, int r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
int cel(int a,int b){return((a-1)/b+1);}
int gcd(int a, int b)
{
    if (a < b)
        swap(a, b);
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
int lcm(int a,int b){
    return (a*b)/gcd(a,b);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int a,b, n;
    cin >> n;vi v(n),chk(n+1);
    rep(i,0,n){cin>>v[i];chk[v[i]]=i;}
    a=0;b=0;
    repb(i,n-1,1){
        if(chk[i]>chk[i+1]){a=i;break;}
    }
    rep(i,1,n){
        if(chk[i]>chk[i+1]){b=n-i;break;}
    }
    cout<<min(a,b);
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <map>
#include <string.h>
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>

using namespace std;
#define set_precision cout << fixed << setprecision(10)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define replb(i, a, b) for (ll i = a; i >= b; i--)
#define repl(i, a, b) for (ll i = a; i < b; i++)
#define repit(arr) for (auto it = arr.begin(); it != arr.end(); it++)
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define vpi vector<pair<int,int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<int>())
#define ssort(arr) stable_sort(arr.begin(), arr.end())
#define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end())
#define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(),, greater<int>())
#define search(arr, c) binary_search(arr.begin(), arr.end(), c)
#define pb push_back
#define ll long long
#define cl_bf cin.ignore(numeric_limits<streamsize>::max(), '\n');
#define castl static_cast<ll>
int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
int nCr(int n, int r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
int cel(int a,int b){return((a-1)/b+1);}
int gcd(int a, int b)
{
    if (a < b)
        swap(a, b);
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
int lcm(int a,int b){
    return (a*b)/gcd(a,b);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int a,b,ans=-1,n,f=0;
    cin >> n;vi v(n),chk(n+1);
    rep(i,0,n){cin>>v[i];chk[v[i]]=i;}
    a=1;b=1;
    while(b<n){
        a=1;
        rep(i,b,n){
            if(chk[i]>chk[i+1]){
                b=i+1;f=1;
                break;
            }
            a++;
        }
        ans=max(a,ans);
        if(f==0)break;
    }
    cout<<n-ans;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <map>
#include <string.h>
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>

using namespace std;
#define set_precision cout << fixed << setprecision(10)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define replb(i, a, b) for (ll i = a; i >= b; i--)
#define repl(i, a, b) for (ll i = a; i < b; i++)
#define repit(arr) for (auto it = arr.begin(); it != arr.end(); it++)
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define vpi vector<pair<int,int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<int>())
#define ssort(arr) stable_sort(arr.begin(), arr.end())
#define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end())
#define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(),, greater<int>())
#define search(arr, c) binary_search(arr.begin(), arr.end(), c)
#define pb push_back
#define ll long long
#define cl_bf cin.ignore(numeric_limits<streamsize>::max(), '\n');
#define castl static_cast<ll>
int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
int nCr(int n, int r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
int cel(int a,int b){return((a-1)/b+1);}
int gcd(int a, int b)
{
    if (a < b)
        swap(a, b);
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
int lcm(int a,int b){
    return (a*b)/gcd(a,b);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int a,b,ans=-1,n,f=0;
    cin >> n;vi v(n),chk(n+1);
    rep(i,0,n){cin>>v[i];chk[v[i]]=i;}
    a=1;b=1;
    while(b<n){
        a=1;f=0;
        rep(i,b,n){
            if(chk[i]>chk[i+1]){
                b=i+1;f=1;
                break;
            }
            a++;
        }
        ans=max(a,ans);
        if(f==0)break;
    }
    cout<<n-ans;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <map>
#include <string.h>
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>

using namespace std;
#define set_precision cout << fixed << setprecision(10)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define replb(i, a, b) for (ll i = a; i >= b; i--)
#define repl(i, a, b) for (ll i = a; i < b; i++)
#define repit(arr) for (auto it = arr.begin(); it != arr.end(); it++)
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define vpi vector<pair<int,int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<int>())
#define ssort(arr) stable_sort(arr.begin(), arr.end())
#define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end())
#define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(),, greater<int>())
#define search(arr, c) binary_search(arr.begin(), arr.end(), c)
#define pb push_back
#define ll long long
#define cl_bf cin.ignore(numeric_limits<streamsize>::max(), '\n');
#define castl static_cast<ll>
int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
int nCr(int n, int r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
int cel(int a,int b){return((a-1)/b+1);}
int gcd(int a, int b)
{
    if (a < b)
        swap(a, b);
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
int lcm(int a,int b){
    return (a*b)/gcd(a,b);
}
// Binary search (note boundaries in the caller)
int CeilIndex(std::vector<int>& v, int l, int r, int key)
{
	while (r - l > 1) {
		int m = l + (r - l) / 2;
		if (v[m] >= key)
			r = m;
		else
			l = m;
	}

	return r;
}

int LongestIncreasingSubsequenceLength(std::vector<int>& v)
{
	if (v.size() == 0)
		return 0;

	std::vector<int> tail(v.size(), 0);
	int length = 1; // always points empty slot in tail

	tail[0] = v[0];
	for (size_t i = 1; i < v.size(); i++) {

		// new smallest value
		if (v[i] < tail[0])
			tail[0] = v[i];

		// v[i] extends largest subsequence
		else if (v[i] > tail[length - 1])
			tail[length++] = v[i];

		// v[i] will become end candidate of an existing
		// subsequence or Throw away larger elements in all
		// LIS, to make room for upcoming grater elements
		// than v[i] (and also, v[i] would have already
		// appeared in one of LIS, identify the location
		// and replace it)
		else
			tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i];
	}

	return length;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int a,b, n;
    cin >> n;vi v(n);
    rep(i,0,n){cin>>v[i];}
    cout<<n-LongestIncreasingSubsequenceLength(v);
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <map>
#include <string.h>
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>

using namespace std;
#define set_precision cout << fixed << setprecision(10)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define replb(i, a, b) for (ll i = a; i >= b; i--)
#define repl(i, a, b) for (ll i = a; i < b; i++)
#define repit(arr) for (auto it = arr.begin(); it != arr.end(); it++)
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define vpi vector<pair<int,int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<int>())
#define ssort(arr) stable_sort(arr.begin(), arr.end())
#define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end())
#define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(),, greater<int>())
#define search(arr, c) binary_search(arr.begin(), arr.end(), c)
#define pb push_back
#define ll long long
#define cl_bf cin.ignore(numeric_limits<streamsize>::max(), '\n');
#define castl static_cast<ll>
int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
int nCr(int n, int r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
int cel(int a,int b){return((a-1)/b+1);}
int gcd(int a, int b)
{
    if (a < b)
        swap(a, b);
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
int lcm(int a,int b){
    return (a*b)/gcd(a,b);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int a,b,ans=1,n,f=0;
    cin >> n;vi v(n),chk(n+1);
    rep(i,0,n){cin>>v[i];chk[v[i]]=i;}
    a=1;b=1;
    while(b<n){
        a=1;f=0;
        rep(i,b,n){
            if(chk[i]>chk[i+1]){
                b=i+1;f=1;
                break;
            }
            a++;
        }
        ans=max(a,ans);
        if(f==0)break;
    }
    cout<<n-ans;
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,x,cnt,c=1,cnt2,maxi,arr[100001];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>x;
        arr[x]=i;
        maxi=max(maxi,x);
    }
    for(int i=1;i<maxi;i++){
        if(arr[i]&&arr[i+1])
            if(arr[i]>arr[i+1])c++;
        else
            cnt=max(cnt,c),c=1;
    }
    cnt=max(cnt,c),c=1;
    for(int i=1;i<maxi;i++){
        if(arr[i]&&arr[i+1])
            if(arr[i+1]>arr[i])c++;
        else
            cnt2=max(cnt2,c),c=1;
    }
    cnt2=max(cnt2,c);
    cout<<min(n-cnt,n-cnt2)<<endl;
   	re 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,last,cnt,cnt2,cnt3,arr[100000];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin >> n;int m=n-1;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++){
        if(arr[i]-last==1)
            last=arr[i],cnt++;
    }
    cnt=n-cnt;
    if(!cnt){cout<<"0\n";re 0;}
    last=n;
    for(int i=n-1;i>=0;i--){
        if(last==arr[i])
            last--,cnt2++;
    }
    cnt2=n-cnt2;
    if(arr[n-1]==n)cnt++;
    //if(arr[0]==1)cnt2++;
    cnt3=n-arr[n-1];
    vector<int> v;
    for(int i=0;i<n;i++){
        if(arr[i]<arr[n-1])v.push_back(arr[i]);
    }
    n=v.size();
    //cout<<n<<endl;
    int mo=0;
    if(n){
        last=v[n-1];
        if(arr[m]-last>1)cnt3++;
        for(int i=n-1;i>=0;i--){
            if(last==v[i])
                last--,mo++;
        }
        mo=n-mo;
        cnt3+=mo;
    }
    //cout<<cnt<<" "<<cnt2<<" "<<cnt3<<endl;
    cout<<min(cnt,min(cnt2,cnt3))<<endl;
   	re 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,last,cnt,cnt2,arr[100000];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin >> n;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++){
        if(arr[i]-last==1)
            last=arr[i],cnt++;
    }
    cnt=n-cnt;
    if(!cnt){cout<<"0\n";re 0;}
    if(arr[n-1]==n)cnt++;
    cnt2=n-arr[n-1];
    vector<int> v;
    for(int i=0;i<n;i++){
        if(arr[i]<arr[n-1])v.push_back(arr[i]);
    }
    n=v.size();
    int mo=0;
    last=v[n-1];
    for(int i=n-1;i>=0;i--){
        if(last==v[i])
            last--,mo++;
    }
    mo=n-mo;
    if(v[0]==1)mo++;
    cnt2+=mo;
    cout<<min(cnt,cnt2)<<endl;
   	re 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,last,cnt,cnt2,arr[100000];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin >> n;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++){
        if(arr[i]-last==1)
            last=arr[i],cnt++;
    }
    cnt=n-cnt;
    if(!cnt){cout<<"0\n";re 0;}
    last=n;
    for(int i=n-1;i>=0;i--){
        if(last-arr[i]==1)
            last=arr[i],cnt2++;
    }
    cnt2=n-cnt2;
    if(arr[n-1]==n)cnt++;
    if(arr[0]==1)cnt2++;
    cout<<min(cnt,cnt2)<<endl;
   	re 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,last,cnt,cnt2,cnt3,arr[100000];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin >> n;int m=n-1;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++){
        if(arr[i]-last==1)
            last=arr[i],cnt++;
    }
    cnt=n-cnt;
    if(!cnt){cout<<"0\n";re 0;}
    last=n;
    for(int i=n-1;i>=0;i--){
        if(last==arr[i])
            last--,cnt2++;
    }
    cnt2=n-cnt2;
    //if(arr[n-1]==n)cnt++;
    //if(arr[0]==1)cnt2++;
    cnt3=n-arr[n-1];
    vector<int> v;
    for(int i=0;i<n;i++){
        if(arr[i]<arr[n-1])v.push_back(arr[i]);
    }
    n=v.size();
    //cout<<n<<endl;
    int mo=0;
    if(n){
        last=v[n-1];
        //if(arr[m]-last>1)cnt3++;
        for(int i=n-1;i>=0;i--){
            if(last==v[i])
                last--,mo++;
        }
        mo=n-mo;
        cnt3+=mo;
    }
    //cout<<cnt<<" "<<cnt2<<" "<<cnt3<<endl;
    cout<<min(cnt,min(cnt2,cnt3))<<endl;
   	re 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,last,cnt,cnt2,cnt3,arr[100000];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin >> n;int m=n-1;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++){
        if(arr[i]-last==1)
            last=arr[i],cnt++;
    }
    cnt=n-cnt;
    if(!cnt){cout<<"0\n";re 0;}
    last=n;
    for(int i=n-1;i>=0;i--){
        if(last==arr[i])
            last--,cnt2++;
    }
    cnt2=n-cnt2;
    if(arr[n-1]==n)cnt++;
    if(arr[0]==1)cnt2++;
    cnt3=n-arr[n-1];
    vector<int> v;
    for(int i=0;i<n;i++){
        if(arr[i]<arr[n-1])v.push_back(arr[i]);
    }
    n=v.size();
    //cout<<n<<endl;
    int mo=0;
    if(n){
        last=v[n-1];
        if(arr[m]-last>1)cnt3++;
        for(int i=n-1;i>=0;i--){
            if(last==v[i])
                last--,mo++;
        }
        mo=n-mo;
        cnt3+=mo;
    }
    //cout<<cnt<<" "<<cnt2<<" "<<cnt3<<endl;
    cout<<min(cnt,min(cnt2,cnt3))<<endl;
   	re 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,last,cnt,cnt2,arr[100000];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin >> n;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++){
        if(arr[i]-last==1)
            last=arr[i],cnt++;
    }
    cnt=n-cnt;
    if(!cnt){cout<<"0\n";re 0;}
    if(arr[n-1]==n)cnt++;
    cnt2=n-arr[n-1];
    vector<int> v;
    for(int i=0;i<n;i++){
        if(arr[i]<arr[n-1])v.push_back(arr[i]);
    }
    n=v.size();
    //cout<<n<<endl;
    int mo=0;
    if(n){
        last=v[n-1];
        for(int i=n-1;i>=0;i--){
            if(last==v[i])
                last--,mo++;
        }
        mo=n-mo;
        if(v[0]==1)mo++;
        cnt2+=mo;
    }
    cout<<min(cnt,cnt2)<<endl;
   	re 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,last,cnt,cnt2,cnt3,arr[100000];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin >> n;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++){
        if(arr[i]-last==1)
            last=arr[i],cnt++;
    }
    cnt=n-cnt;
    if(!cnt){cout<<"0\n";re 0;}
    last=n;
    for(int i=n-1;i>=0;i--){
        if(last-arr[i]==1)
            last=arr[i],cnt2++;
    }
    cnt2=n-cnt2;
    if(arr[n-1]==n)cnt++;
    if(arr[0]==1)cnt2++;
    cnt3=n-arr[n-1];
    vector<int> v;
    for(int i=0;i<n;i++){
        if(arr[i]<arr[n-1])v.push_back(arr[i]);
    }
    n=v.size();
    //cout<<n<<endl;
    int mo=0;
    if(n){
        last=v[n-1];
        for(int i=n-1;i>=0;i--){
            if(last==v[i])
                last--,mo++;
        }
        mo=n-mo;
        if(v[0]==1)mo++;
        cnt3+=mo;
    }
    cout<<min(cnt,min(cnt2,cnt3))<<endl;
   	re 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,last,cnt,cnt2,cnt3,arr[100000];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin >> n;int m=n-1;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++){
        if(arr[i]-last==1)
            last=arr[i],cnt++;
    }
    cnt=n-cnt;
    if(!cnt){cout<<"0\n";re 0;}
    last=n;
    for(int i=n-1;i>=0;i--){
        if(last==arr[i])
            last--,cnt2++;
    }
    cnt2=n-cnt2;
    //if(arr[n-1]==n)cnt++;
    //if(arr[0]==1)cnt2++;
    cnt3=n-arr[n-1];
    vector<int> v;
    for(int i=0;i<n;i++){
        if(arr[i]<arr[n-1])v.push_back(arr[i]);
    }
    n=v.size();
    //cout<<n<<endl;
    int mo=0;
    if(n){
        last=v[n-1];
        if(arr[m]-last>1)cnt3++;
        for(int i=n-1;i>=0;i--){
            if(last==v[i])
                last--,mo++;
        }
        mo=n-mo;
        cnt3+=mo;
    }
    //cout<<cnt<<" "<<cnt2<<" "<<cnt3<<endl;
    cout<<min(cnt,min(cnt2,cnt3))<<endl;
   	re 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,last,cnt,cnt2,cnt3,arr[100000];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin >> n;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++){
        if(arr[i]-last==1)
            last=arr[i],cnt++;
    }
    cnt=n-cnt;
    if(!cnt){cout<<"0\n";re 0;}
    last=n;
    for(int i=n-1;i>=0;i--){
        if(last==arr[i])
            last--,cnt2++;
    }
    cnt2=n-cnt2;
    if(arr[n-1]==n)cnt++;
    if(arr[0]==1)cnt2++;
    cnt3=n-arr[n-1];
    vector<int> v;
    for(int i=0;i<n;i++){
        if(arr[i]<arr[n-1])v.push_back(arr[i]);
    }
    n=v.size();
    //cout<<n<<endl;
    int mo=0;
    if(n){
        last=v[n-1];
        for(int i=n-1;i>=0;i--){
            if(last==v[i])
                last--,mo++;
        }
        mo=n-mo;
        if(v[0]==1)mo++;
        cnt3+=mo;
    }
    //cout<<cnt<<" "<<cnt2<<" "<<cnt3<<endl;
    cout<<min(cnt,min(cnt2,cnt3))<<endl;
   	re 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,last,cnt,cnt2,cnt3,arr[100000];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin >> n;int m=n-1;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++){
        if(arr[i]-last==1)
            last=arr[i],cnt++;
    }
    cnt=n-cnt;
    if(!cnt){cout<<"0\n";re 0;}
    last=n;
    for(int i=n-1;i>=0;i--){
        if(last==arr[i])
            last--,cnt2++;
    }
    cnt2=n-cnt2;
    if(arr[n-1]==n)cnt++;
    //if(arr[0]==1)cnt2++;
    cnt3=n-arr[n-1];
    vector<int> v;
    for(int i=0;i<n;i++){
        if(arr[i]<arr[n-1])v.push_back(arr[i]);
    }
    n=v.size();
    //cout<<n<<endl;
    int mo=0;
    if(n){
        last=v[n-1];
        //if(arr[m]-last>1)cnt3++;
        for(int i=n-1;i>=0;i--){
            if(last==v[i])
                last--,mo++;
        }
        mo=n-mo;
        cnt3+=mo;
    }
    //cout<<cnt<<" "<<cnt2<<" "<<cnt3<<endl;
    cout<<min(cnt,min(cnt2,cnt3))<<endl;
   	re 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,last,cnt,cnt2,arr[100000];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin >> n;
    for(itn i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++){
        if(arr[i]-last==1)
            last=arr[i],cnt++;
    }
    cnt=n-cnt;
    if(!cnt){cout<<"0\n";re 0;}
    last=n;
    for(int i=n-1;i>=0;i--){
        if(last-arr[i]==1)
            last=arr[i],cnt2++;
    }
    cnt2=n-cnt2;
    if(arr[n-1]==n)cnt++;
    if(arr[0]==1)cnt2++;
    cout<<min(cnt,cnt2)<<endl;
   	re 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,x,cnt,c=1,cnt2,maxi,arr[100001];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>x;
        arr[x]=i;
        maxi=max(maxi,x);
    }
    for(int i=1;i<maxi;i++){
        if(arr[i]&&arr[i+1])
            if(arr[i]>arr[i+1])c++;
        else
            cnt=max(cnt,c),c=1;
    }
    c=1;
    for(int i=1;i<maxi;i++){
        if(arr[i]&&arr[i+1])
            if(arr[i+1]>arr[i])c++;
        else
            cnt2=max(cnt2,c),c=1;
    }
    cout<<min(n-cnt,n-cnt2)<<endl;
   	re 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,last,cnt,cnt2,cnt3,arr[100000];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin >> n;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++){
        if(arr[i]-last==1)
            last=arr[i],cnt++;
    }
    cnt=n-cnt;
    if(!cnt){cout<<"0\n";re 0;}
    last=n;
    for(int i=n-1;i>=0;i--){
        if(last-arr[i]==1)
            last=arr[i],cnt2++;
    }
    cnt2=n-cnt2;
    //if(arr[n-1]==n)cnt++;
    //if(arr[0]==1)cnt2++;
    cnt3=n-arr[n-1];
    vector<int> v;
    for(int i=0;i<n;i++){
        if(arr[i]<arr[n-1])v.push_back(arr[i]);
    }
    n=v.size();
    //cout<<n<<endl;
    int mo=0;
    if(n){
        last=v[n-1];
        for(int i=n-1;i>=0;i--){
            if(last==v[i])
                last--,mo++;
        }
        mo=n-mo;
        if(v[0]==1)mo++;
        cnt3+=mo;
    }
    cout<<min(cnt,min(cnt2,cnt3))<<endl;
   	re 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,last,cnt,cnt2,cnt3,arr[100000];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin >> n;int m=n-1;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++){
        if(arr[i]-last==1)
            last=arr[i],cnt++;
    }
    cnt=n-cnt;
    if(!cnt){cout<<"0\n";re 0;}
    last=n;
    for(int i=n-1;i>=0;i--){
        if(last==arr[i])
            last--,cnt2++;
    }
    cnt2=n-cnt2;
    if(arr[n-1]==n)cnt++;
    if(arr[0]==1)cnt2++;
    cnt3=n-arr[n-1];
    vector<int> v;
    for(int i=0;i<n;i++){
        if(arr[i]<arr[n-1])v.push_back(arr[i]);
    }
    n=v.size();
    //cout<<n<<endl;
    int mo=0;
    if(n){
        last=v[n-1];
        if(v[0]==1||arr[m]-last>1)cnt3++;
        for(int i=n-1;i>=0;i--){
            if(last==v[i])
                last--,mo++;
        }
        mo=n-mo;
        cnt3+=mo;
    }
    //cout<<cnt<<" "<<cnt2<<" "<<cnt3<<endl;
    cout<<min(cnt,min(cnt2,cnt3))<<endl;
   	re 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <list>
#include <fstream>
#define ull unsigned long long
#define ll long long
#define ld long double
#define pi 3.1415926536
#define lp(i,n,a) for(ll i=a;i<=n;i++)
#define pl(i,n) for(ll i=n-1;i>=0;i--)
//#define w while
#define re return
using namespace std;
int n,x,cnt,c=1,cnt2,maxi,arr[100001];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>x;
        arr[x]=i;
        maxi=max(maxi,x);
    }
    for(int i=1;i<maxi;i++){
        if(arr[i]&&arr[i+1])
            if(arr[i]<arr[i+1])c++;
        else
            cnt=max(cnt,c),c=1;
    }
    cnt=max(cnt,c);
    cout<<n-cnt<<endl;
   	re 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int N = 100005;
int a[N], b[N];
int n;

int main() {
    cin >> n;
    for(int i = 1; i <= n; ++i) cin >> a[i];

    for(int i = 2; i <= n; ++i) {
        for(int j = i - 1; j >= 1; --j) {
            if(a[i] == a[j] + 1) { b[i] = b[j] + 1; break; }
        }
    }

    cout << n - *max_element(b, b + n + 1) - 1 << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int N = 100005;
int a[N], b[N];
int n;

int main() {
    cin >> n;
    for(int i = 1; i <= n; ++i) cin >> a[i];

    for(int i = 2; i <= n; ++i) {
        for(int j = i - 1; j >= 1; --j) {
            if(a[i] > a[j]) { b[i] = b[j] + 1; break; }
        }
    }

    cout << n - *max_element(b, b + n + 1) - 1 << endl;

    return 0;
}


#include <iostream>

using namespace std;

const int N = 100005;
int a[N], b[N];
int n;

int main() {
    cin >> n;
    for(int i = 1; i <= n; ++i) cin >> a[i];

    for(int i = 2; i <= n; ++i) {
        for(int j = i - 1; j >= 1; --j) {
            if(a[i] > a[j]) { b[i] = b[j] + 1; break; }
        }
    }

    cout << n - *max_element(b, b + n + 1) - 1 << endl;

    return 0;
}


#include <bits/stdc++.h>

#define FOR(i, s, e) for(int i = s; i < e; i++)
#define FORR(i, s, e) for(int i = s; i >= e; i--)
#define pb(i) push_back(i)
#define pf() pop_front()
#define initdx4 int dx[4] = {0,0,-1,1};
#define initdy4 int dy[4] = {1,-1,0,0};
#define initdx8 int dx[8] = {0,0,-1,-1,-1,1,1,1};
#define initdy8 int dy[8] = {1,-1,1,0,-1,1,0,-1};
#define initArr1(arr, e1, v) FOR(i, 0, e1) arr[i] = v;
#define initArr2(arr, e1, e2, v) FOR(i, 0, e1) FOR(j, 0, e2) arr[i][j] = v;
#define initArr3(arr, e1, e2, e3, v) FOR(i, 0, e1) FOR(j, 0, e2) FOR(k, 0, e3) arr[i][j][k] = v;
#define initArr4(arr, e1, e2, e3, e4, v) FOR(i, 0, e1) FOR(j, 0, e2) FOR(k, 0, e3) FOR(l, 0, e4) arr[i][j][k][l] = v;
#define ll long long

using namespace std;

int main() {
	ios::sync_with_stdio(0);cin.tie(0);
	int n, a[100000], pos[1000001];
	cin >> n;
	FOR(i, 0, n) {
		cin >> a[i];
		pos[a[i]] = i;
	}
	int ans = n;
	int c = 1;
	FOR(i, 1, n) {
		if(pos[i+1] > pos[i]) c++;
		else c = 1;
		ans = min(ans, n-c);
	}
	cout << min(ans, n-c) << endl;
	return 0;
}


#include <bits/stdc++.h>

#define FOR(i, s, e) for(int i = s; i < e; i++)
#define FORR(i, s, e) for(int i = s; i >= e; i--)
#define pb(i) push_back(i)
#define pf() pop_front()
#define initdx4 int dx[4] = {0,0,-1,1};
#define initdy4 int dy[4] = {1,-1,0,0};
#define initdx8 int dx[8] = {0,0,-1,-1,-1,1,1,1};
#define initdy8 int dy[8] = {1,-1,1,0,-1,1,0,-1};
#define initArr1(arr, e1, v) FOR(i, 0, e1) arr[i] = v;
#define initArr2(arr, e1, e2, v) FOR(i, 0, e1) FOR(j, 0, e2) arr[i][j] = v;
#define initArr3(arr, e1, e2, e3, v) FOR(i, 0, e1) FOR(j, 0, e2) FOR(k, 0, e3) arr[i][j][k] = v;
#define initArr4(arr, e1, e2, e3, e4, v) FOR(i, 0, e1) FOR(j, 0, e2) FOR(k, 0, e3) FOR(l, 0, e4) arr[i][j][k][l] = v;
#define ll long long

using namespace std;

int main() {
	ios::sync_with_stdio(0);cin.tie(0);
	int n, a[100000], pos[1000001];
	cin >> n;
	FOR(i, 0, n) {
		cin >> a[i];
		pos[a[i]] = i;
	}
	int ans = n;
	int c = 1;
	FOR(i, 1, n) {
		if(pos[i+1] > pos[i]) c++;
		else c = 1;
		ans = min(ans, n-c);
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

#define FOR(i, s, e) for(int i = s; i < e; i++)
#define FORR(i, s, e) for(int i = s; i >= e; i--)
#define pb(i) push_back(i)
#define pf() pop_front()
#define initdx4 int dx[4] = {0,0,-1,1};
#define initdy4 int dy[4] = {1,-1,0,0};
#define initdx8 int dx[8] = {0,0,-1,-1,-1,1,1,1};
#define initdy8 int dy[8] = {1,-1,1,0,-1,1,0,-1};
#define initArr1(arr, e1, v) FOR(i, 0, e1) arr[i] = v;
#define initArr2(arr, e1, e2, v) FOR(i, 0, e1) FOR(j, 0, e2) arr[i][j] = v;
#define initArr3(arr, e1, e2, e3, v) FOR(i, 0, e1) FOR(j, 0, e2) FOR(k, 0, e3) arr[i][j][k] = v;
#define initArr4(arr, e1, e2, e3, e4, v) FOR(i, 0, e1) FOR(j, 0, e2) FOR(k, 0, e3) FOR(l, 0, e4) arr[i][j][k][l] = v;
#define ll long long

using namespace std;

int main() {
	ios::sync_with_stdio(0);cin.tie(0);
	int n, a[100000], pos[1000001];
	cin >> n;
	FOR(i, 0, n) {
		cin >> a[i];
		pos[a[i]] = i;
	}
	int ans = n;
	int c = 1;
	FOR(i, 1, n) {
		if(pos[i+1] > pos[i]) c++;
		else c = 1;
		ans = min(ans, n-c);
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll a[100010];
int main()
{
	ll n;
    cin>>n;
    for(ll i=0;i<n;i++)
        cin>>a[i];
    if(a[0]==1)
        cout<<n-a[n-1]<<endl;
    else if(a[n-1]==n)
        cout<<a[0]-1<<endl;
    else
    cout<<min(a[0]-1,n-a[n-1])<<endl;
 return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll a[100010];
int main()
{
	ll n;
    cin>>n;
    for(ll i=0;i<n;i++)
        cin>>a[i];
    cout<<min(a[0]-1,n-a[n-1])<<endl;
 return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll a[100010];
int main()
{
	ll n;
    cin>>n;
    ll ans = 0,res=0;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        a[x]=i;
    }
    for(ll i=2;i<=n;i++)
    {
        if(a[i-1]<a[i])
            ans++;
        else
            ans=0;
       res = max(res,ans);
    }
    cout<<n-res-1<<endl;
 return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll a[100010];
int main()
{
	ll n;
    cin>>n;
    ll ans = 0,res=0;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        a[x]=i;
    }
    for(ll i=2;i<=n;i++)
    {
        if(a[i-1]<a[i])
            ans++;
        else
            ans=1;
       res = max(res,ans);
    }
    cout<<n-res-1<<endl;
 return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll a[100010];
int main()
{
	ll n;
    cin>>n;
    ll ans = 1,res=-1;
    for(ll i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        a[x]=i;
    }
    for(ll i=2;i<=n;i++)
    {
        if(a[i-1]<a[i])
            ans++;
        else
            ans=1;
       res = max(res,ans);
    }
    cout<<n-res<<endl;
 return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define int               long long
#define fr                first
#define sc                second
#define vc                vector
#define endl              '\n'
#define pii               pair<int,int>
#define msi               map<string,int>
#define mii               map<int,int>
#define psi               pair<string,int>
#define pis               pair<int,string>
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define LSB(i)            ((i)&-(i))

template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void solve()
{
    int n;
    cin >> n;
    int a[n];
    map<int, int>pos;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        pos[a[i]] = i;
    }
    int count = 1, longest = 1;
    for (int i = 2; i <= n; i++)
    {
        if (pos[i - 1] < pos[i])
            count++;
        else
        {
            longest = max(longest, count);
            count = 1;
        }
    }
    longes = max(longest, count);
    cout << n - longest << endl;
}
signed main() {
    ios_base::sync_with_stdio(false);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int t = 1;
    //cin>>t;
    while (t--) solve();
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define int               long long
#define fr                first
#define sc                second
#define vc                vector
#define endl              '\n'
#define pii               pair<int,int>
#define msi               map<string,int>
#define mii               map<int,int>
#define psi               pair<string,int>
#define pis               pair<int,string>
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define LSB(i)            ((i)&-(i))

template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void solve()
{
    int n;
    cin >> n;
    int a[n];
    map<int, int>pos;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        pos[a[i]] = i;
    }
    int count = 1, longest = 1;
    for (int i = 2; i <= n; i++)
    {
        if (pos[i - 1] < pos[i])
            count++;
        else
        {
            longest = max(longest, count);
            count = 1;
        }
    }
    longest = max(longest, count);
    cout << n - longest << endl;
}
signed main() {
    ios_base::sync_with_stdio(false);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int t = 1;
    //cin>>t;
    while (t--) solve();
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define int               long long
#define fr                first
#define sc                second
#define vc                vector
#define endl              '\n'
#define pii               pair<int,int>
#define msi               map<string,int>
#define mii               map<int,int>
#define psi               pair<string,int>
#define pis               pair<int,string>
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define LSB(i)            ((i)&-(i))

template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void solve()
{
    int n;
    cin >> n;
    int a[n];
    map<int, int>pos;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        pos[a[i]] = i;
    }
    int count = 1, longest = 1;
    for (int i = 2; i <= n; i++)
    {
        if (pos[i - 1] < pos[i])
            count++;
        else
        {
            longest = max(longest, count);
            count = 1;
        }
    }
    cout << n - longest << endl;
}
signed main() {
    ios_base::sync_with_stdio(false);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int t = 1;
    //cin>>t;
    while (t--) solve();
    return 0;
}


#include<iostream>
using namespace std;
int n;
int car[100001];
int main() {
    cin>>n;
    for (int i=0;i<n;i++) {
        int x;
        cin>>x;
        car[x]=i;
    }
    for (int i=1; i<=n; i++) {
    	// cout << car[i] << ' ';
    }
    // cout << endl;
    int cnt=0;
    int low,high=0;
    for (low=0;low<n;low++) {
        while (high < n && car[high+1]>car[high])
            high++;
        // printf("%d %d\n", low, high);
        cnt=max(cnt,high-low+1);
        low = high;
        high++;
    }
    // printf("cnt: %d\n", cnt);
    cout<<n-cnt;
}


#include<iostream>
using namespace std;
int n;
int car[100001];
int main() {
    cin>>n;
    for (int i=0;i<n;++) {
        int x;
        cin>>x;
        car[x]=i;
    }
    int cnt=0;
    int low,high=1;
    for (low=1,low<n;low++) {
        while (car[high+1]==car[high])
            high++;
        cnt+=high-low;
    }
    cout<<cnt-n;
}


#include<iostream>
using namespace std;
int n;
int car[100001];
int main() {
    cin>>n;
    for (int i=0;i<n;i++) {
        int x;
        cin>>x;
        car[x]=i;
    }
    for (int i=1; i<=n; i++) {
    	// cout << car[i] << ' ';
    }
    // cout << endl;
    int cnt=0;
    int low,high=1;
    for (low=1;low<=n;low++) {
        while (high + 1 <= n && car[high+1]>car[high]) {
        	// printf("high: %d, high+1: %d\n", car[high], car[high+1]);
            high++;
        }
        // printf("%d %d\n", low, high);
        cnt=max(cnt,high-low+1);
        low = high;
        high++;
    }
    // printf("cnt: %d\n", cnt);
    cout<<n-cnt;
}


#include<iostream>
using namespace std;
int n;
int car[100001];
int main() {
    cin>>n;
    for (int i=0;i<n;i++) {
        int x;
        cin>>x;
        car[x]=i;
    }
    for (int i=1; i<=n; i++) {
    	// cout << car[i] << ' ';
    }
    // cout << endl;
    int cnt=0;
    int low,high=1;
    for (low=1;low<n;low++) {
        while (car[high+1]>=car[high])
            high++;
        cnt=max(cnt,high-low+1);
        low = high;
        high++;
    }
    // printf("cnt: %d\n", cnt);
    cout<<n-cnt;
}


/*
#####################################################
# I will win.. maybe not immediately but definitely #
#####################################################
*/

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

//save time
typedef long long ll;

//for sorting
#define all(a) a.begin(),a.end()

//Constants
#define PI   3.141592653593
#define MOD  1000000007
#define EPS  0.000000001

//loops
#define REP(i,n) 	for(ll i=0;i<(n);++i)
#define FOR(i,a,b) 	for(ll i=(a);i<(b);++i)
#define DFOR(i,a,b) for(ll i=(a);i>=(b);--i)

//vectors
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int,int> >
#define pb 	push_back

//pairs
#define pi pair<int,int>
#define mp make_pair
#define F first
#define S second

//fast I/O
#ifndef _WIN32
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define gc getchar
#define pc putchar
#define scan getFoo

//If using cin and cout
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(NULL)

//queue
#define qi queue<int>
#define PQ priority_queue

//general
#define E empty()

//Input methods
template <typename T>
void getFoo(T &x){
    x=0;
    register char c=gc();
    for(;c<48 || c>57;c=gc());
    for(;c>47 && c<58;c=gc())
        x=(x<<1)+(x<<3)+c-48;
}

template <typename T1,typename T2>
void getFoo(T1 &x,T2 &y){
    getFoo(x);
    getFoo(y);
}

template <typename T1,typename T2,typename T3>
void getFoo(T1 &x,T2 &y,T3 &z){
    getFoo(x);
    getFoo(y);
    getFoo(z);
}
//Declare all variables and methods needed between this comment and the next one(OCD lol)
vi adj[1000010];
int lPath[1000010];

void dfs(int i){
    for(int x :  adj[i]){
        if(lPath[x]<lPath[i]+1){
            lPath[x]=lPath[i]+1;
            dfs(x);
        }
    }
}

//Code goes here !!
inline void solve(){
    IOS;
    TIE;
    int n;
    scan(n);
    int arr[n];
    REP(i,n)
        scan(arr[i]);
    //O(n^2)
    REP(i,n-1)
        FOR(j,i+1,n)
            if(arr[i]<arr[j])
                adj[i].pb(j);
    int maxPath=-1;
    REP(i,n){
        dfs(i);
        maxPath=max(maxPath,lPath[i]);
    }
    printf("%d",n-maxPath-1);
}

int main(){
    solve();
    return 0;
}


/*
#####################################################
# I will win.. maybe not immediately but definitely #
#####################################################
*/

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

//save time
typedef long long ll;

//for sorting
#define all(a) a.begin(),a.end()

//Constants
#define PI   3.141592653593
#define MOD  1000000007
#define EPS  0.000000001

//loops
#define REP(i,n) 	for(ll i=0;i<(n);++i)
#define FOR(i,a,b) 	for(ll i=(a);i<(b);++i)
#define DFOR(i,a,b) for(ll i=(a);i>=(b);--i)

//vectors
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int,int> >
#define pb 	push_back

//pairs
#define pi pair<int,int>
#define mp make_pair
#define F first
#define S second

//fast I/O
#ifndef _WIN32
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define gc getchar
#define pc putchar
#define scan getFoo

//If using cin and cout
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(NULL)

//queue
#define qi queue<int>
#define PQ priority_queue

//general
#define E empty()

//Input methods
template <typename T>
void getFoo(T &x){
    x=0;
    register char c=gc();
    for(;c<48 || c>57;c=gc());
    for(;c>47 && c<58;c=gc())
        x=(x<<1)+(x<<3)+c-48;
}

template <typename T1,typename T2>
void getFoo(T1 &x,T2 &y){
    getFoo(x);
    getFoo(y);
}

template <typename T1,typename T2,typename T3>
void getFoo(T1 &x,T2 &y,T3 &z){
    getFoo(x);
    getFoo(y);
    getFoo(z);
}
//Declare all variables and methods needed between this comment and the next one(OCD lol)
//Code goes here !!
inline void solve(){
    IOS;
    TIE;
    int n;
    scan(n);
    int arr[n+1];
    FOR(i,1,n+1){
        int temp;
        scan(temp);
        arr[temp]=i;
    }
    int cnt=0,foo=n+1;
    FOR(i,1,n){
        if(arr[i]>arr[i+1]){
            arr[i+1]=foo++;
            ++cnt;
        }
    }
    printf("%d",cnt);
}

int main(){
    solve();
    return 0;
}
/*
#####################################################
# I will win.. maybe not immediately but definitely #
#####################################################
*/

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

//save time
typedef long long ll;

//for sorting
#define all(a) a.begin(),a.end()

//Constants
#define PI   3.141592653593
#define MOD  1000000007
#define EPS  0.000000001

//loops
#define REP(i,n) 	for(ll i=0;i<(n);++i)
#define FOR(i,a,b) 	for(ll i=(a);i<(b);++i)
#define DFOR(i,a,b) for(ll i=(a);i>=(b);--i)

//vectors
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int,int> >
#define pb 	push_back

//pairs
#define pi pair<int,int>
#define mp make_pair
#define F first
#define S second

//fast I/O
#ifndef _WIN32
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define gc getchar
#define pc putchar
#define scan getFoo

//If using cin and cout
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(NULL)

//queue
#define qi queue<int>
#define PQ priority_queue

//general
#define E empty()

//Input methods
template <typename T>
void getFoo(T &x){
    x=0;
    register char c=gc();
    for(;c<48 || c>57;c=gc());
    for(;c>47 && c<58;c=gc())
        x=(x<<1)+(x<<3)+c-48;
}

template <typename T1,typename T2>
void getFoo(T1 &x,T2 &y){
    getFoo(x);
    getFoo(y);
}

template <typename T1,typename T2,typename T3>
void getFoo(T1 &x,T2 &y,T3 &z){
    getFoo(x);
    getFoo(y);
    getFoo(z);
}
//Declare all variables and methods needed between this comment and the next one(OCD lol)
//Code goes here !!
inline void solve(){
    IOS;
    TIE;
    int n;
    scan(n);
    int arr[n+1];
    FOR(i,1,n+1){
        int temp;
        scan(temp);
        arr[temp]=i;
    }
    int cnt=0,foo=n+1;
    FOR(i,1,n){
        if(arr[i]>arr[i+1]){
            arr[i+1]=foo++;
            ++cnt;
        }
    }
    printf("%d",cnt);
}

int main(){
    solve();
    return 0;
}


/*
#####################################################
# I will win.. maybe not immediately but definitely #
#####################################################
*/

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

//save time
typedef long long ll;

//for sorting
#define all(a) a.begin(),a.end()

//Constants
#define PI   3.141592653593
#define MOD  1000000007
#define EPS  0.000000001

//loops
#define REP(i,n) 	for(ll i=0;i<(n);++i)
#define FOR(i,a,b) 	for(ll i=(a);i<(b);++i)
#define DFOR(i,a,b) for(ll i=(a);i>=(b);--i)

//vectors
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int,int> >
#define pb 	push_back

//pairs
#define pi pair<int,int>
#define mp make_pair
#define F first
#define S second

//fast I/O
#ifndef _WIN32
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define gc getchar
#define pc putchar
#define scan getFoo

//If using cin and cout
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(NULL)

//queue
#define qi queue<int>
#define PQ priority_queue

//general
#define E empty()

//Input methods
template <typename T>
void getFoo(T &x){
    x=0;
    register char c=gc();
    for(;c<48 || c>57;c=gc());
    for(;c>47 && c<58;c=gc())
        x=(x<<1)+(x<<3)+c-48;
}

template <typename T1,typename T2>
void getFoo(T1 &x,T2 &y){
    getFoo(x);
    getFoo(y);
}

template <typename T1,typename T2,typename T3>
void getFoo(T1 &x,T2 &y,T3 &z){
    getFoo(x);
    getFoo(y);
    getFoo(z);
}
//Declare all variables and methods needed between this comment and the next one(OCD lol)
vi *adj;
int *lPath;

void dfs(int i){
    for(int x :  adj[i]){
        if(lPath[x]<lPath[i]+1){
            lPath[x]=lPath[i]+1;
            dfs(x);
        }
    }
}

//Code goes here !!
inline void solve(){
    IOS;
    TIE;
    int n;
    scan(n);
    lPath=new int[n];
    adj=new vi[n];
    REP(i,n)
        scan(lPath[i]);
    //O(n^2)
    REP(i,n-1)
        FOR(j,i+1,n)
            if(lPath[i]<lPath[j])
                adj[i].pb(j);
    fill(lPath,lPath+n,0);
    int maxPath=-1;
    REP(i,n){
        dfs(i);
        maxPath=max(maxPath,lPath[i]);
    }
    printf("%d",n-maxPath-1);
}

int main(){
    solve();
    return 0;
}


/*
#####################################################
# I will win.. maybe not immediately but definitely #
#####################################################
*/

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

//save time
typedef long long ll;

//for sorting
#define all(a) a.begin(),a.end()

//Constants
#define PI   3.141592653593
#define MOD  1000000007
#define EPS  0.000000001

//loops
#define REP(i,n) 	for(ll i=0;i<(n);++i)
#define FOR(i,a,b) 	for(ll i=(a);i<(b);++i)
#define DFOR(i,a,b) for(ll i=(a);i>=(b);--i)

//vectors
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int,int> >
#define pb 	push_back

//pairs
#define pi pair<int,int>
#define mp make_pair
#define F first
#define S second

//fast I/O
#ifndef _WIN32
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define gc getchar
#define pc putchar
#define scan getFoo

//If using cin and cout
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(NULL)

//queue
#define qi queue<int>
#define PQ priority_queue

//general
#define E empty()

//Input methods
template <typename T>
void getFoo(T &x){
    x=0;
    register char c=gc();
    for(;c<48 || c>57;c=gc());
    for(;c>47 && c<58;c=gc())
        x=(x<<1)+(x<<3)+c-48;
}

template <typename T1,typename T2>
void getFoo(T1 &x,T2 &y){
    getFoo(x);
    getFoo(y);
}

template <typename T1,typename T2,typename T3>
void getFoo(T1 &x,T2 &y,T3 &z){
    getFoo(x);
    getFoo(y);
    getFoo(z);
}
//Declare all variables and methods needed between this comment and the next one(OCD lol)
//Code goes here !!
inline void solve(){
    IOS;
    TIE;
    int n;
    scan(n);
    int arr[n+1];
    FOR(i,1,n+1){
        int temp;
        scan(temp);
        arr[temp]=i;
    }
    int cnt=0,foo=n+1;
    FOR(i,1,n){
        if(arr[i]>arr[i+1]){
            arr[i+1]=foo++;
            ++cnt;
        }
    }
    printf("%d",cnt);
}

int main(){
    solve();
    return 0;
}


/*
#####################################################
# I will win.. maybe not immediately but definitely #
#####################################################
*/

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

//save time
typedef long long ll;

//for sorting
#define all(a) a.begin(),a.end()

//Constants
#define PI   3.141592653593
#define MOD  1000000007
#define EPS  0.000000001

//loops
#define REP(i,n) 	for(ll i=0;i<(n);++i)
#define FOR(i,a,b) 	for(ll i=(a);i<(b);++i)
#define DFOR(i,a,b) for(ll i=(a);i>=(b);--i)

//vectors
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int,int> >
#define pb 	push_back

//pairs
#define pi pair<int,int>
#define mp make_pair
#define F first
#define S second

//fast I/O
#ifndef _WIN32
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define gc getchar
#define pc putchar
#define scan getFoo

//If using cin and cout
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(NULL)

//queue
#define qi queue<int>
#define PQ priority_queue

//general
#define E empty()

//Input methods
template <typename T>
void getFoo(T &x){
    x=0;
    register char c=gc();
    for(;c<48 || c>57;c=gc());
    for(;c>47 && c<58;c=gc())
        x=(x<<1)+(x<<3)+c-48;
}

template <typename T1,typename T2>
void getFoo(T1 &x,T2 &y){
    getFoo(x);
    getFoo(y);
}

template <typename T1,typename T2,typename T3>
void getFoo(T1 &x,T2 &y,T3 &z){
    getFoo(x);
    getFoo(y);
    getFoo(z);
}
//Declare all variables and methods needed between this comment and the next one(OCD lol)

//Code goes here !!
inline void solve(){
    IOS;
    TIE;
    ll n;
    scan(n);
    ll arr[n+10];
    FOR(i,1,n+1){
        ll temp;
        scan(temp);
        arr[temp]=i;
    }
    ll len=0,left=1,currMax=arr[1];
    FOR(i,1,n+1){
        if(arr[i]>=currMax)
            currMax=arr[i];
        else{
            left=i;
            currMax=arr[i];
        }
        len=max(len,i-left+1LL);
    }
    printf("%d",n-len);
}

int main(){
    solve();
    return 0;
}


/*
#####################################################
# I will win.. maybe not immediately but definitely #
#####################################################
*/

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

//save time
typedef long long ll;

//for sorting
#define all(a) a.begin(),a.end()

//Constants
#define PI   3.141592653593
#define MOD  1000000007
#define EPS  0.000000001

//loops
#define REP(i,n) 	for(ll i=0;i<(n);++i)
#define FOR(i,a,b) 	for(ll i=(a);i<(b);++i)
#define DFOR(i,a,b) for(ll i=(a);i>=(b);--i)

//vectors
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int,int> >
#define pb 	push_back

//pairs
#define pi pair<int,int>
#define mp make_pair
#define F first
#define S second

//fast I/O
#ifndef _WIN32
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define gc getchar
#define pc putchar
#define scan getFoo

//If using cin and cout
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(NULL)

//queue
#define qi queue<int>
#define PQ priority_queue

//general
#define E empty()

//Input methods
template <typename T>
void getFoo(T &x){
    x=0;
    register char c=gc();
    for(;c<48 || c>57;c=gc());
    for(;c>47 && c<58;c=gc())
        x=(x<<1)+(x<<3)+c-48;
}

template <typename T1,typename T2>
void getFoo(T1 &x,T2 &y){
    getFoo(x);
    getFoo(y);
}

template <typename T1,typename T2,typename T3>
void getFoo(T1 &x,T2 &y,T3 &z){
    getFoo(x);
    getFoo(y);
    getFoo(z);
}
//Declare all variables and methods needed between this comment and the next one(OCD lol)
//Code goes here !!
inline void solve(){
    IOS;
    TIE;
    int n;
    scan(n);
    int arr[n+1];
    FOR(i,1,n+1){
        int temp;
        scan(temp);
        arr[temp]=i;
    }
    int cnt=0,foo=n+1;
    FOR(i,1,n){
        if(arr[i]>arr[i+1]){
            arr[i+1]=foo++;
            ++cnt;
        }
    }
    printf("%d",min(cnt,n-cnt));
}

int main(){
    solve();
    return 0;
}


/*
#####################################################
# I will win.. maybe not immediately but definitely #
#####################################################
*/

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

//save time
typedef long long ll;

//for sorting
#define all(a) a.begin(),a.end()

//Constants
#define PI   3.141592653593
#define MOD  1000000007
#define EPS  0.000000001

//loops
#define REP(i,n) 	for(ll i=0;i<(n);++i)
#define FOR(i,a,b) 	for(ll i=(a);i<(b);++i)
#define DFOR(i,a,b) for(ll i=(a);i>=(b);--i)

//vectors
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int,int> >
#define pb 	push_back

//pairs
#define pi pair<int,int>
#define mp make_pair
#define F first
#define S second

//fast I/O
#ifndef _WIN32
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define gc getchar
#define pc putchar
#define scan getFoo

//If using cin and cout
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(NULL)

//queue
#define qi queue<int>
#define PQ priority_queue

//general
#define E empty()

//Input methods
template <typename T>
void getFoo(T &x){
    x=0;
    register char c=gc();
    for(;c<48 || c>57;c=gc());
    for(;c>47 && c<58;c=gc())
        x=(x<<1)+(x<<3)+c-48;
}

template <typename T1,typename T2>
void getFoo(T1 &x,T2 &y){
    getFoo(x);
    getFoo(y);
}

template <typename T1,typename T2,typename T3>
void getFoo(T1 &x,T2 &y,T3 &z){
    getFoo(x);
    getFoo(y);
    getFoo(z);
}
//Declare all variables and methods needed between this comment and the next one(OCD lol)
vi adj[100010];
int lPath[100010];

void dfs(int i){
    for(int x :  adj[i]){
        if(lPath[x]<lPath[i]+1){
            lPath[x]=lPath[i]+1;
            dfs(x);
        }
    }
}

//Code goes here !!
inline void solve(){
    IOS;
    TIE;
    int n;
    scan(n);
    int arr[n];
    REP(i,n)
        scan(arr[i]);
    //O(n^2)
    REP(i,n-1)
        FOR(j,i+1,n)
            if(arr[i]<arr[j])
                adj[i].pb(j);
    int maxPath=-1;
    REP(i,n){
        dfs(i);
        maxPath=max(maxPath,lPath[i]);
    }
    printf("%d",n-maxPath-1);
}

int main(){
    solve();
    return 0;
}


/*
#####################################################
# I will win.. maybe not immediately but definitely #
#####################################################
*/

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

//save time
typedef long long ll;

//for sorting
#define all(a) a.begin(),a.end()

//Constants
#define PI   3.141592653593
#define MOD  1000000007
#define EPS  0.000000001

//loops
#define REP(i,n) 	for(ll i=0;i<(n);++i)
#define FOR(i,a,b) 	for(ll i=(a);i<(b);++i)
#define DFOR(i,a,b) for(ll i=(a);i>=(b);--i)

//vectors
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int,int> >
#define pb 	push_back

//pairs
#define pi pair<int,int>
#define mp make_pair
#define F first
#define S second

//fast I/O
#ifndef _WIN32
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define gc getchar
#define pc putchar
#define scan getFoo

//If using cin and cout
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(NULL)

//queue
#define qi queue<int>
#define PQ priority_queue

//general
#define E empty()

//Input methods
template <typename T>
void getFoo(T &x){
    x=0;
    register char c=gc();
    for(;c<48 || c>57;c=gc());
    for(;c>47 && c<58;c=gc())
        x=(x<<1)+(x<<3)+c-48;
}

template <typename T1,typename T2>
void getFoo(T1 &x,T2 &y){
    getFoo(x);
    getFoo(y);
}

template <typename T1,typename T2,typename T3>
void getFoo(T1 &x,T2 &y,T3 &z){
    getFoo(x);
    getFoo(y);
    getFoo(z);
}
//Declare all variables and methods needed between this comment and the next one(OCD lol)

//Code goes here !!
inline void solve(){
    IOS;
    TIE;
    ll n;
    scan(n);
    ll arr[n+1];
    FOR(i,1,n+1){
        int temp;
        scan(temp);
        arr[temp]=i;
    }
    ll len=0,left=1,currMax=arr[1];
    FOR(i,1,n+1){
        if(arr[i]>=currMax)
            currMax=arr[i];
        else
            left=i;
        len=max(len,(int)i-left+1);
    }
    printf("%d",n-len);
}

int main(){
    solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int lis( vector<int> &arr )
{
    int n=arr.size();
    int *lis, i, j, max = 0;
    lis = (int*) malloc ( sizeof( int ) * n );

    for (i = 0; i < n; i++ )
        lis[i] = 1;

    for (i = 1; i < n; i++ )
        for (j = 0; j < i; j++ )
            if ( arr[i] > arr[j] && lis[i] < lis[j] + 1)
                lis[i] = lis[j] + 1;

    for (i = 0; i < n; i++ )
        if (max < lis[i])
            max = lis[i];

    free(lis);

    return max;
}
int main(){
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    cout<<n-lis(a)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int lis( vector<int> &a )
{
    int n=a.size();
    int *lis, i, j, max = 0;
    lis = (int*) malloc ( sizeof( int ) * n );

    for (i = 0; i < n; i++ )
        lis[i] = 1;

    for (i = 1; i < n; i++ )
        for (j = 0; j < i; j++ )
            if ( arr[i] > arr[j] && lis[i] < lis[j] + 1)
                lis[i] = lis[j] + 1;

    /* Pick maximum of all LIS values */
    for (i = 0; i < n; i++ )
        if (max < lis[i])
            max = lis[i];

    /* Free memory to avoid memory leak */
    free(lis);

    return max;
}
int main(){
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    cout<<n-lis(a)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int lis( vector<int> &arr )
{
    int n=arr.size();
    int *lis, i, j, max = 0;
    lis = (int*) malloc ( sizeof( int ) * n );

    for (i = 0; i < n; i++ )
        lis[i] = 1;

    for (i = 1; i < n; i++ )
        for (j = 0; j < i; j++ )
            if ( arr[i] > arr[j] && lis[i] < lis[j] + 1)
                lis[i] = lis[j] + 1;

    /* Pick maximum of all LIS values */
    for (i = 0; i < n; i++ )
        if (max < lis[i])
            max = lis[i];

    /* Free memory to avoid memory leak */
    free(lis);

    return max;
}
int main(){
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    cout<<n-lcs(a)<<endl;
}


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

#include <iostream>
#include <vector>

int CeilIndex(std::vector<int> &v, int l, int r, int key) {
    while (r-l > 1) {
    int m = l + (r-l)/2;
    if (v[m] >= key)
        r = m;
    else
        l = m;
    }

    return r;
}

int LongestIncreasingSubsequenceLength(std::vector<int> &v) {
    if (v.size() == 0)
        return 0;

    std::vector<int> tail(v.size(), 0);
    int length = 1;

    tail[0] = v[0];
    for (size_t i = 1; i < v.size(); i++) {
        if (v[i] < tail[0])

            tail[0] = v[i];
        else if (v[i] > tail[length-1])

            tail[length++] = v[i];
        else

            tail[CeilIndex(tail, -1, length-1, v[i])] = v[i];
    }

    return length;
}
int main(){
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    cout<<n-LongestIncreasingSubsequenceLength(a);
}