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

typedef long long ll;
#define pb push_back

void insert(vector<ll> &arr, ll cur, ll &ans)
{
    if (cur == 0 || cur == arr.size() - 1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans += arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans -= arr[cur];
}

void remove(vector<ll> &arr, ll cur, ll &ans)
{
    if (cur == 0 || cur == arr.size() - 1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans -= arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans += arr[cur];
}

int main()
{
    ll t;
    cin >> t;
    while (t-- > 0)
    {
        ll n, q;
        cin >> n >> q;
        vector<ll> arr(n + 2);
        arr[0] = arr[n + 1] = -1;
        ll low = 0, high = 0, temp;
        for (ll i = 1; i <= n; i++)
        {
            cin >> arr[i];
            temp = low;
            low = max(low, high - arr[i]);
            high = max(high, temp + arr[i]);
        }
        ll ans = max(low, high);
        cout << ans << endl;
        while (q-- > 0)
        {
            ll L, R;
            cin >> L >> R;
            remove(arr, L-1, ans);
            remove(arr, L, ans);
            remove(arr, L+1, ans);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) remove(arr, R-1, ans);
                if (R != L+1) remove(arr, R, ans);
                remove(arr, R+1, ans);
            }
            swap(arr[L], arr[R]);
            insert(arr, L-1, ans);
            insert(arr, L, ans);
            insert(arr, L+1, ans);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) insert(arr, R-1, ans);
                if (R != L+1) insert(arr, R, ans);
                insert(arr, R+1, ans);
            }
            cout << ans << endl;
        }
    }
    return 0;
}


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

typedef long long ll;
#define pb push_back

ll arr[1000000+5], ans=0, n;
void insert(int cur)
{
    if (cur == 0 || cur == n+1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans += arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans -= arr[cur];
}

void remove(int cur)
{
    if (cur == 0 || cur == n+1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans -= arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans += arr[cur];
}

int main()
{
    // ios_base::sync_with_stdio(false);
    // cin.tie(0);
    // cout.tie(0);
    int t;
    cin >> t;
    while (t-- > 0)
    {
        int q;
        cin >> n >> q;
        arr[0] = arr[n + 1] = -1;
        ll low = 0, high = 0, temp;
        for (int i = 1; i <= n; i++)
        {
            cin >> arr[i];
            temp = low;
            low = max(low, high - arr[i]);
            high = max(high, temp + arr[i]);
        }
        ans = max(low, high);
        cout << ans << endl;
        while (q-- > 0)
        {
            int L, R;
            cin >> L >> R;
            remove(L-1);
            remove(L);
            remove(L+1);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) remove(R-1);
                if (R != L+1) remove(R);
                remove(R+1);
            }
            swap(arr[L], arr[R]);
            insert(L-1);
            insert(L);
            insert(L+1);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) insert(R-1);
                if (R != L+1) insert(R);
                insert(R+1);
            }
            cout << ans << endl;
        }
    }
    return 0;
}


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

typedef long long ll;
#define pb push_back

ll ans=0;
void insert(ll *arr, int cur, int n)
{
    if (cur == 0 || cur == n+1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans += arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans -= arr[cur];
}

void remove(ll *arr, int cur, int n)
{
    if (cur == 0 || cur == n+1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans -= arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans += arr[cur];
}

int main()
{
    // ios_base::sync_with_stdio(false);
    // cin.tie(0);
    // cout.tie(0);
    int t;
    cin >> t;
    while (t-- > 0)
    {
        int n, q;
        cin >> n >> q;
        ll arr[n + 2];
        arr[0] = arr[n + 1] = -1;
        ll low = 0, high = 0, temp;
        for (int i = 1; i <= n; i++)
        {
            cin >> arr[i];
            temp = low;
            low = max(low, high - arr[i]);
            high = max(high, temp + arr[i]);
        }
        ans = max(low, high);
        cout << ans << endl;
        while (q-- > 0)
        {
            int L, R;
            cin >> L >> R;
            remove(arr, L-1, n);
            remove(arr, L, n);
            remove(arr, L+1, n);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) remove(arr, R-1, n);
                if (R != L+1) remove(arr, R, n);
                remove(arr, R+1, n);
            }
            swap(arr[L], arr[R]);
            insert(arr, L-1, n);
            insert(arr, L, n);
            insert(arr, L+1, n);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) insert(arr, R-1, n);
                if (R != L+1) insert(arr, R, n);
                insert(arr, R+1, n);
            }
            cout << ans << endl;
        }
    }
    return 0;
}


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

typedef long long ll;
#define pb push_back

void insert(vector<ll> &arr, ll cur, ll &ans)
{
    if (cur == 0 || cur == arr.size() - 1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans += arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans -= arr[cur];
}

void remove(vector<ll> &arr, ll cur, ll &ans)
{
    if (cur == 0 || cur == arr.size() - 1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans -= arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans += arr[cur];
}

ll main()
{
    ll t;
    cin >> t;
    while (t-- > 0)
    {
        ll n, q;
        cin >> n >> q;
        vector<ll> arr(n + 2);
        arr[0] = arr[n + 1] = -1;
        ll low = 0, high = 0, temp;
        for (ll i = 1; i <= n; i++)
        {
            cin >> arr[i];
            temp = low;
            low = max(low, high - arr[i]);
            high = max(high, temp + arr[i]);
        }
        ll ans = max(low, high);
        cout << ans << endl;
        while (q-- > 0)
        {
            ll L, R;
            cin >> L >> R;
            remove(arr, L-1, ans);
            remove(arr, L, ans);
            remove(arr, L+1, ans);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) remove(arr, R-1, ans);
                if (R != L+1) remove(arr, R, ans);
                remove(arr, R+1, ans);
            }
            swap(arr[L], arr[R]);
            insert(arr, L-1, ans);
            insert(arr, L, ans);
            insert(arr, L+1, ans);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) insert(arr, R-1, ans);
                if (R != L+1) insert(arr, R, ans);
                insert(arr, R+1, ans);
            }
            cout << ans << endl;
        }
    }
    return 0;
}


#include<iostream>

using namespace std;

#define int long long

int a[1000000+5];

int n;

int ans=0;

inline void insert(int i)
{
    if (i==0||i==n+1) return;
    if (a[i-1]<a[i]&&a[i]>a[i+1]) ans+=a[i];
    if (a[i-1]>a[i]&&a[i]<a[i+1]) ans-=a[i];
}

inline void erase(int i)
{
    if (i==0||i==n+1) return;
    if (a[i-1]<a[i]&&a[i]>a[i+1]) ans-=a[i];
    if (a[i-1]>a[i]&&a[i]<a[i+1]) ans+=a[i];
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while (t--)
    {
        int q;
        cin>>n>>q;
        for (int i=1; i<=n; i++)
        {
            cin>>a[i];
        }
        a[0]=-1;
        a[n+1]=-1;
        ans=0;
        for (int i=1; i<=n; i++)
        {
            if (a[i-1]<a[i]&&a[i]>a[i+1]) ans+=a[i];
            if (a[i-1]>a[i]&&a[i]<a[i+1]) ans-=a[i];
        }
        cout<<ans<<'\n';
        while (q--)
        {
            int l,r;
            cin>>l>>r;
            erase(l-1);
            erase(l);
            erase(l+1);
            if (l!=r)
            {
                if (r-1!=l+1&&r-1!=l) erase(r-1);
                if (r!=l+1) erase(r);
                erase(r+1);
            }
            swap(a[l],a[r]);
            insert(l-1);
            insert(l);
            insert(l+1);
            if (l!=r)
            {
                if (r-1!=l+1&&r-1!=l) insert(r-1);
                if (r!=l+1) insert(r);
                insert(r+1);
            }
            cout<<ans<<'\n';
        }
    }
}


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

typedef long long ll;
#define pb push_back

void insert(vector<ll> &arr, int cur, ll &ans)
{
    if (cur == 0 || cur == arr.size() - 1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans += arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans -= arr[cur];
}

void remove(vector<ll> &arr, int cur, ll &ans)
{
    if (cur == 0 || cur == arr.size() - 1)
        return;
    if (arr[cur - 1] < arr[cur] && arr[cur] > arr[cur + 1])
        ans -= arr[cur];
    if (arr[cur - 1] > arr[cur] && arr[cur] < arr[cur + 1])
        ans += arr[cur];
}

int main()
{
    int t;
    cin >> t;
    while (t-- > 0)
    {
        int n, q;
        cin >> n >> q;
        vector<ll> arr(n + 2);
        arr[0] = arr[n + 1] = -1;
        ll low = 0, high = 0, temp;
        arr[0] = INT_MIN;
        for (int i = 1; i <= n; i++)
        {
            cin >> arr[i];
            temp = low;
            low = max(low, high - arr[i]);
            high = max(high, temp + arr[i]);
        }
        ll ans = max(low, high);
        cout << ans << endl;
        while (q-- > 0)
        {
            int L, R;
            cin >> L >> R;
            remove(arr, L-1, ans);
            remove(arr, L, ans);
            remove(arr, L+1, ans);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) remove(arr, R-1, ans);
                if (R != L+1) remove(arr, R, ans);
                remove(arr, R+1, ans);
            }
            swap(arr[L], arr[R]);
            insert(arr, L-1, ans);
            insert(arr, L, ans);
            insert(arr, L+1, ans);
            if (L!=R) {
                if (R-1 != L+1 && R-1 != L) insert(arr, R-1, ans);
                if (R != L+1) insert(arr, R, ans);
                insert(arr, R+1, ans);
            }
            cout << ans << endl;
        }
    }
    return 0;
}


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

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
//    ios_base::sync_with_stdio(0); cin.tie(0);

    const ll inf = 1e12;
    struct SegmentTree {
        int n;
        vector<ll> omi, emi, oma, ema;
        SegmentTree(int n) : n(n), omi(4 * n), oma(4 * n), emi(4 * n), ema(4 * n) {}
        void merge(int tv) {
            int l = 2 * tv;
            int r = 2 * tv + 1;
            oma[tv] = max({oma[l], oma[r], oma[l] - emi[r], ema[l] + oma[r]});
            omi[tv] = min({omi[l], omi[r], omi[l] - ema[r], emi[l] + omi[r]});

            ema[tv] = max({ema[l], ema[r], ema[l] + ema[r], oma[l] - omi[r]});
            emi[tv] = min({emi[l], emi[r], emi[l] + emi[r], omi[l] - oma[r]});
        }
        void build(int n_, vector<int> &a) {
            n = n_;
            build(a, 1, 0, n - 1);
        }
        void build(vector<int> &a, int tv, int tl, int tr) {
            if (tl == tr) {
                oma[tv] = a[tr];
                omi[tv] = a[tr];
                ema[tv] = -inf;
                emi[tv] = inf;
            }
            else {
                int tm = (tl + tr) / 2;
                build(a, 2 * tv, tl, tm);
                build(a, 2 * tv + 1, tm + 1, tr);
                merge(tv);
            }
        }
        void setval(int idx, int val) {
            setval(idx, val, 1, 0, n - 1);
        }
        void setval(int idx, int val, int tv, int tl, int tr) {
            if (tl == tr) {
                oma[tv] = val;
                omi[tv] = val;
            }
            else {
                int tm = (tl + tr) / 2;
                if (idx <= tm) setval(idx, val, 2 * tv, tl, tm);
                else setval(idx, val, 2 * tv + 1, tm + 1, tr);
                merge(tv);
            }
        }
    };

    const int maxn = 3e5;
    SegmentTree tree(maxn);

    int t;
    cin >> t;
//    scanf("%d", &t);
    while (t--) {
        int n, q;
        cin >> n >> q;
//        scanf("%d%d", &n, &q);
        vector<int> a(n);
        for (auto &e : a) cin >> e; //scanf("%d", &e);

        tree.build(n, a);
        cout << max(tree.oma[1], tree.ema[1]) << "\n";
//        printf("%I64d\n", max(tree.oma[1], tree.ema[1]));
        for (int i = 0; i < q; ++i) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            tree.setval(l, a[r]);
            tree.setval(r, a[l]);
            swap(a[l], a[r]);
//            printf("%I64d\n", max(tree.oma[1], tree.ema[1]));
            cout << max(tree.oma[1], tree.ema[1]) << "\n";
        }
//        cout << "\n";
    }
}


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

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
//    ios_base::sync_with_stdio(0); cin.tie(0);

    const ll inf = 1e12;
    struct SegmentTree {
        int n;
        struct Node {
            ll omi, emi, oma, ema;
        };
        vector<Node> tree;
        SegmentTree(int n) : n(n), tree(4 * n) {}
        void merge(int tv) {
            auto &l = tree[2 * tv];
            auto &r = tree[2 * tv + 1];
            auto &we = tree[tv];

            we.oma = max({l.oma, r.oma, l.oma - r.emi, l.ema + r.oma});
            we.omi = min({l.omi, r.omi, l.omi - r.ema, l.emi + r.omi});

            we.ema = max({l.ema, r.ema, l.ema + r.ema, l.oma - r.omi});
            we.emi = min({l.emi, r.emi, l.emi + r.emi, l.omi - r.oma});
        }
        void build(int n_, vector<int> &a) {
            n = n_;
            build(a, 1, 0, n - 1);
        }
        void build(vector<int> &a, int tv, int tl, int tr) {
            if (tl == tr) {
                auto &we = tree[tv];
                we.oma = a[tr];
                we.omi = a[tr];
                we.ema = -inf;
                we.emi = inf;
            }
            else {
                int tm = (tl + tr) / 2;
                build(a, 2 * tv, tl, tm);
                build(a, 2 * tv + 1, tm + 1, tr);
                merge(tv);
            }
        }
        void setval(int idx, int val) {
            setval(idx, val, 1, 0, n - 1);
        }
        void setval(int idx, int val, int tv, int tl, int tr) {
            if (tl == tr) {
                tree[tv].oma = val;
                tree[tv].omi = val;
            }
            else {
                int tm = (tl + tr) / 2;
                if (idx <= tm) setval(idx, val, 2 * tv, tl, tm);
                else setval(idx, val, 2 * tv + 1, tm + 1, tr);
                merge(tv);
            }
        }
        ll getmax() {
            return max(tree[1].oma, tree[1].ema);
        }
    };

    const int maxn = 3e5;
    SegmentTree tree(maxn);

    int t;
//    cin >> t;
    scanf("%d", &t);
    while (t--) {
        int n, q;
//        cin >> n >> q;
        scanf("%d%d", &n, &q);
        vector<int> a(n);
//        for (auto &e : a) cin >> e;
        for (auto &e : a) scanf("%d", &e);

        tree.build(n, a);
//        cout << tree.getmax() << "\n";
        printf("%d\n", (int)tree.getmax());
        for (int i = 0; i < q; ++i) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            tree.setval(l, a[r]);
            tree.setval(r, a[l]);
            swap(a[l], a[r]);
            printf("%d\n", (int)tree.getmax());
//            cout << tree.getmax() << "\n";
        }
//        cout << "\n";
    }
}


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

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
//    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
//    cin >> t;
    scanf("%d", &t);
    while (t--) {
        int n, q;
//        cin >> n >> q;
        scanf("%d%d", &n, &q);
        vector<int> a(n);
        for (auto &e : a) scanf("%d", &e);//cin >> e;

        const ll inf = 1e12;
        struct SegmentTree {
            int n;
            vector<ll> omi, emi, oma, ema;
            SegmentTree(int n, vector<int> &a)
                : n(n), omi(4 * n), oma(4 * n), emi(4 * n), ema(4 * n) {
                build(a, 1, 0, n - 1);
            }
            void merge(int tv, int tl, int tr) {
                int l = 2 * tv;
                int r = 2 * tv + 1;
                oma[tv] = max({oma[l], oma[r], oma[l] - emi[r], ema[l] + oma[r]});
                omi[tv] = min({omi[l], omi[r], omi[l] - ema[r], emi[l] + omi[r]});

                ema[tv] = max({ema[l], ema[r], ema[l] + ema[r], oma[l] - omi[r]});
                emi[tv] = min({emi[l], emi[r], emi[l] + emi[r], omi[l] - oma[r]});
            }
            void build(vector<int> &a, int tv, int tl, int tr) {
                if (tl == tr) {
                    oma[tv] = a[tr];
                    omi[tv] = a[tr];
                    ema[tv] = -inf;
                    emi[tv] = inf;
                }
                else {
                    int tm = (tl + tr) / 2;
                    build(a, 2 * tv, tl, tm);
                    build(a, 2 * tv + 1, tm + 1, tr);
                    merge(tv, tl, tr);
                }
            }
            void setval(int idx, int val) {
                setval(idx, val, 1, 0, n - 1);
            }
            void setval(int idx, int val, int tv, int tl, int tr) {
                if (tl == tr) {
                    oma[tv] = val;
                    omi[tv] = val;
                }
                else {
                    int tm = (tl + tr) / 2;
                    if (idx <= tm) setval(idx, val, 2 * tv, tl, tm);
                    else setval(idx, val, 2 * tv + 1, tm + 1, tr);
                    merge(tv, tl, tr);
                }
            }
        };

        SegmentTree tree(n, a);
//        cout << max(tree.oma[1], tree.ema[1]) << "\n";
        printf("%lld\n", max(tree.oma[1], tree.ema[1]));
        for (int i = 0; i < q; ++i) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            tree.setval(l, a[r]);
            tree.setval(r, a[l]);
            swap(a[l], a[r]);
            printf("%lld\n", max(tree.oma[1], tree.ema[1]));
//            cout << max(tree.oma[1], tree.ema[1]) << "\n";
        }
//        cout << "\n";
    }
}


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

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
//    ios_base::sync_with_stdio(0); cin.tie(0);

    const ll inf = 1e12;
    struct SegmentTree {
        int n;
        struct Node {
            ll omi, emi, oma, ema;
        };
        vector<Node> tree;
        SegmentTree(int n) : n(n), tree(4 * n) {}
        void merge(int tv) {
            auto &l = tree[2 * tv];
            auto &r = tree[2 * tv + 1];
            auto &we = tree[tv];

            we.oma = max({l.oma, r.oma, l.oma - r.emi, l.ema + r.oma});
            we.omi = min({l.omi, r.omi, l.omi - r.ema, l.emi + r.omi});

            we.ema = max({l.ema, r.ema, l.ema + r.ema, l.oma - r.omi});
            we.emi = min({l.emi, r.emi, l.emi + r.emi, l.omi - r.oma});
        }
        void build(int n_, vector<int> &a) {
            n = n_;
            build(a, 1, 0, n - 1);
        }
        void build(vector<int> &a, int tv, int tl, int tr) {
            if (tl == tr) {
                auto &we = tree[tv];
                we.oma = a[tr];
                we.omi = a[tr];
                we.ema = -inf;
                we.emi = inf;
            }
            else {
                int tm = (tl + tr) / 2;
                build(a, 2 * tv, tl, tm);
                build(a, 2 * tv + 1, tm + 1, tr);
                merge(tv);
            }
        }
        void setval(int idx, int val) {
            setval(idx, val, 1, 0, n - 1);
        }
        void setval(int idx, int val, int tv, int tl, int tr) {
            if (tl == tr) {
                tree[tv].oma = val;
                tree[tv].omi = val;
            }
            else {
                int tm = (tl + tr) / 2;
                if (idx <= tm) setval(idx, val, 2 * tv, tl, tm);
                else setval(idx, val, 2 * tv + 1, tm + 1, tr);
                merge(tv);
            }
        }
        ll getmax() {
            return max(tree[1].oma, tree[1].ema);
        }
    };

    const int maxn = 3e5;
    SegmentTree tree(maxn);

    int t;
//    cin >> t;
    scanf("%d", &t);
    while (t--) {
        int n, q;
//        cin >> n >> q;
        scanf("%d%d", &n, &q);
        vector<int> a(n);
//        for (auto &e : a) cin >> e;
        for (auto &e : a) scanf("%d", &e);

        tree.build(n, a);
//        cout << tree.getmax() << "\n";
        printf("%I64d\n", tree.getmax());
        for (int i = 0; i < q; ++i) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            tree.setval(l, a[r]);
            tree.setval(r, a[l]);
            swap(a[l], a[r]);
            printf("%I64d\n", tree.getmax());
//            cout << tree.getmax() << "\n";
        }
//        cout << "\n";
    }
}


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

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
//    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
//    cin >> t;
    scanf("%d", &t);
    while (t--) {
        int n, q;
//        cin >> n >> q;
        scanf("%d%d", &n, &q);
        vector<int> a(n);
        for (auto &e : a) scanf("%d", &e);//cin >> e;

        const ll inf = 1e12;
        struct SegmentTree {
            int n;
            vector<ll> omi, emi, oma, ema;
            SegmentTree(int n, vector<int> &a)
                : n(n), omi(4 * n), oma(4 * n), emi(4 * n), ema(4 * n) {
                build(a, 1, 0, n - 1);
            }
            void merge(int tv) {
                int l = 2 * tv;
                int r = 2 * tv + 1;
                oma[tv] = max({oma[l], oma[r], oma[l] - emi[r], ema[l] + oma[r]});
                omi[tv] = min({omi[l], omi[r], omi[l] - ema[r], emi[l] + omi[r]});

                ema[tv] = max({ema[l], ema[r], ema[l] + ema[r], oma[l] - omi[r]});
                emi[tv] = min({emi[l], emi[r], emi[l] + emi[r], omi[l] - oma[r]});
            }
            void build(vector<int> &a, int tv, int tl, int tr) {
                if (tl == tr) {
                    oma[tv] = a[tr];
                    omi[tv] = a[tr];
                    ema[tv] = -inf;
                    emi[tv] = inf;
                }
                else {
                    int tm = (tl + tr) / 2;
                    build(a, 2 * tv, tl, tm);
                    build(a, 2 * tv + 1, tm + 1, tr);
                    merge(tv);
                }
            }
            void setval(int idx, int val) {
                setval(idx, val, 1, 0, n - 1);
            }
            void setval(int idx, int val, int tv, int tl, int tr) {
                if (tl == tr) {
                    oma[tv] = val;
                    omi[tv] = val;
                }
                else {
                    int tm = (tl + tr) / 2;
                    if (idx <= tm) setval(idx, val, 2 * tv, tl, tm);
                    else setval(idx, val, 2 * tv + 1, tm + 1, tr);
                    merge(tv);
                }
            }
        };

        SegmentTree tree(n, a);
//        cout << max(tree.oma[1], tree.ema[1]) << "\n";
        printf("%I64d\n", max(tree.oma[1], tree.ema[1]));
        for (int i = 0; i < q; ++i) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            tree.setval(l, a[r]);
            tree.setval(r, a[l]);
            swap(a[l], a[r]);
            printf("%I64d\n", max(tree.oma[1], tree.ema[1]));
//            cout << max(tree.oma[1], tree.ema[1]) << "\n";
        }
//        cout << "\n";
    }
}


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

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >> t;
//    scanf("%d", &t);
    while (t--) {
        int n, q;
        cin >> n >> q;
//        scanf("%d%d", &n, &q);
        vector<int> a(n);
        for (auto &e : a) cin >> e;
//        for (auto &e : a) scanf("%d", &e);

        const ll inf = 1e12;
        struct SegmentTree {
            int n;
            struct Node {
                ll omi, emi, oma, ema;
            };
            vector<Node> tree;
            SegmentTree(int n, vector<int> &a) : n(n), tree(4 * n) {
                build(a, 1, 0, n - 1);
            }
            void merge(int tv) {
                auto &l = tree[2 * tv];
                auto &r = tree[2 * tv + 1];
                auto &we = tree[tv];

                we.oma = max({l.oma, r.oma, l.oma - r.emi, l.ema + r.oma});
                we.omi = min({l.omi, r.omi, l.omi - r.ema, l.emi + r.omi});

                we.ema = max({l.ema, r.ema, l.ema + r.ema, l.oma - r.omi});
                we.emi = min({l.emi, r.emi, l.emi + r.emi, l.omi - r.oma});
            }
            void build(int n_, vector<int> &a) {
                n = n_;
                build(a, 1, 0, n - 1);
            }
            void build(vector<int> &a, int tv, int tl, int tr) {
                if (tl == tr) {
                    auto &we = tree[tv];
                    we.oma = a[tr];
                    we.omi = a[tr];
                    we.ema = -inf;
                    we.emi = inf;
                }
                else {
                    int tm = (tl + tr) / 2;
                    build(a, 2 * tv, tl, tm);
                    build(a, 2 * tv + 1, tm + 1, tr);
                    merge(tv);
                }
            }
            void setval(int idx, int val) {
                setval(idx, val, 1, 0, n - 1);
            }
            void setval(int idx, int val, int tv, int tl, int tr) {
                if (tl == tr) {
                    tree[tv].oma = val;
                    tree[tv].omi = val;
                }
                else {
                    int tm = (tl + tr) / 2;
                    if (idx <= tm) setval(idx, val, 2 * tv, tl, tm);
                    else setval(idx, val, 2 * tv + 1, tm + 1, tr);
                    merge(tv);
                }
            }
            ll getmax() {
                return max(tree[1].oma, tree[1].ema);
            }
        };

        SegmentTree tree(n, a);
        cout << tree.getmax() << "\n";
//        printf("%lld\n", tree.getmax());
        for (int i = 0; i < q; ++i) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            tree.setval(l, a[r]);
            tree.setval(r, a[l]);
            swap(a[l], a[r]);
//            printf("%d\n", tree.getmax());
            cout << tree.getmax() << "\n";
        }
//        cout << "\n";
    }
}


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

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);

    const ll inf = 1e12;
    struct SegmentTree {
        int n;
        struct Node {
            ll omi, emi, oma, ema;
        };
        vector<Node> tree;
        SegmentTree(int n) : n(n), tree(4 * n) {}
        void merge(int tv) {
            auto &l = tree[2 * tv];
            auto &r = tree[2 * tv + 1];
            auto &we = tree[tv];

            we.oma = max({l.oma, r.oma, l.oma - r.emi, l.ema + r.oma});
            we.omi = min({l.omi, r.omi, l.omi - r.ema, l.emi + r.omi});

            we.ema = max({l.ema, r.ema, l.ema + r.ema, l.oma - r.omi});
            we.emi = min({l.emi, r.emi, l.emi + r.emi, l.omi - r.oma});
        }
        void build(int n_, vector<int> &a) {
            n = n_;
            build(a, 1, 0, n - 1);
        }
        void build(vector<int> &a, int tv, int tl, int tr) {
            if (tl == tr) {
                auto &we = tree[tv];
                we.oma = a[tr];
                we.omi = a[tr];
                we.ema = -inf;
                we.emi = inf;
            }
            else {
                int tm = (tl + tr) / 2;
                build(a, 2 * tv, tl, tm);
                build(a, 2 * tv + 1, tm + 1, tr);
                merge(tv);
            }
        }
        void setval(int idx, int val) {
            setval(idx, val, 1, 0, n - 1);
        }
        void setval(int idx, int val, int tv, int tl, int tr) {
            if (tl == tr) {
                tree[tv].oma = val;
                tree[tv].omi = val;
            }
            else {
                int tm = (tl + tr) / 2;
                if (idx <= tm) setval(idx, val, 2 * tv, tl, tm);
                else setval(idx, val, 2 * tv + 1, tm + 1, tr);
                merge(tv);
            }
        }
        ll getmax() {
            return max(tree[1].oma, tree[1].ema);
        }
    };

    const int maxn = 3e5;
    SegmentTree tree(maxn);

    int t;
    cin >> t;
//    scanf("%d", &t);
    while (t--) {
        int n, q;
        cin >> n >> q;
//        scanf("%d%d", &n, &q);
        vector<int> a(n);
        for (auto &e : a) cin >> e;
//        for (auto &e : a) scanf("%d", &e);

        tree.build(n, a);
        cout << tree.getmax() << "\n";
//        printf("%lld\n", tree.getmax());
        for (int i = 0; i < q; ++i) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            tree.setval(l, a[r]);
            tree.setval(r, a[l]);
            swap(a[l], a[r]);
//            printf("%lld\n", tree.getmax());
            cout << tree.getmax() << "\n";
        }
//        cout << "\n";
    }
}


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

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);

    const ll inf = 1e12;
    struct SegmentTree {
        int n;
        vector<ll> omi, emi, oma, ema;
        SegmentTree(int n) : n(n), omi(4 * n), oma(4 * n), emi(4 * n), ema(4 * n) {}
        void merge(int tv) {
            int l = 2 * tv;
            int r = 2 * tv + 1;
            oma[tv] = max({oma[l], oma[r], oma[l] - emi[r], ema[l] + oma[r]});
            omi[tv] = min({omi[l], omi[r], omi[l] - ema[r], emi[l] + omi[r]});

            ema[tv] = max({ema[l], ema[r], ema[l] + ema[r], oma[l] - omi[r]});
            emi[tv] = min({emi[l], emi[r], emi[l] + emi[r], omi[l] - oma[r]});
        }
        void build(int n_, vector<int> &a) {
            n = n_;
            build(a, 1, 0, n - 1);
        }
        void build(vector<int> &a, int tv, int tl, int tr) {
            if (tl == tr) {
                oma[tv] = a[tr];
                omi[tv] = a[tr];
                ema[tv] = -inf;
                emi[tv] = inf;
            }
            else {
                int tm = (tl + tr) / 2;
                build(a, 2 * tv, tl, tm);
                build(a, 2 * tv + 1, tm + 1, tr);
                merge(tv);
            }
        }
        void setval(int idx, int val) {
            setval(idx, val, 1, 0, n - 1);
        }
        void setval(int idx, int val, int tv, int tl, int tr) {
            if (tl == tr) {
                oma[tv] = val;
                omi[tv] = val;
            }
            else {
                int tm = (tl + tr) / 2;
                if (idx <= tm) setval(idx, val, 2 * tv, tl, tm);
                else setval(idx, val, 2 * tv + 1, tm + 1, tr);
                merge(tv);
            }
        }
    };

    const int maxn = 3e5;
    SegmentTree tree(maxn);

    int t;
    cin >> t;
//    scanf("%d", &t);
    while (t--) {
        int n, q;
        cin >> n >> q;
//        scanf("%d%d", &n, &q);
        vector<int> a(n);
        for (auto &e : a) cin >> e; //scanf("%d", &e);

        tree.build(n, a);
        cout << max(tree.oma[1], tree.ema[1]) << "\n";
//        printf("%I64d\n", max(tree.oma[1], tree.ema[1]));
        for (int i = 0; i < q; ++i) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            tree.setval(l, a[r]);
            tree.setval(r, a[l]);
            swap(a[l], a[r]);
//            printf("%I64d\n", max(tree.oma[1], tree.ema[1]));
            cout << max(tree.oma[1], tree.ema[1]) << "\n";
        }
//        cout << "\n";
    }
}


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

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >> t;
    while (t--) {
        int n, q;
        cin >> n >> q;
        vector<int> a(n);
        for (auto &e : a) cin >> e;

        const ll inf = 1e12;
        struct SegmentTree {
            int n;
            vector<ll> omi, emi, oma, ema;
            SegmentTree(int n, vector<int> &a)
                : n(n), omi(4 * n), oma(4 * n), emi(4 * n), ema(4 * n) {
                build(a, 1, 0, n - 1);
            }
            void merge(int tv, int tl, int tr) {
                int l = 2 * tv;
                int r = 2 * tv + 1;
                oma[tv] = max({oma[l], oma[r], oma[l] - emi[r], ema[l] + oma[r]});
                omi[tv] = min({omi[l], omi[r], omi[l] - ema[r], emi[l] + omi[r]});

                ema[tv] = max({ema[l], ema[r], ema[l] + ema[r], oma[l] - omi[r]});
                emi[tv] = min({emi[l], emi[r], emi[l] + emi[r], omi[l] - oma[r]});
            }
            void build(vector<int> &a, int tv, int tl, int tr) {
                if (tl == tr) {
                    oma[tv] = a[tr];
                    omi[tv] = a[tr];
                    ema[tv] = -inf;
                    emi[tv] = inf;
                }
                else {
                    int tm = (tl + tr) / 2;
                    build(a, 2 * tv, tl, tm);
                    build(a, 2 * tv + 1, tm + 1, tr);
                    merge(tv, tl, tr);
                }
            }
            void setval(int idx, int val) {
                setval(idx, val, 1, 0, n - 1);
            }
            void setval(int idx, int val, int tv, int tl, int tr) {
                if (tl == tr) {
                    oma[tv] = val;
                    omi[tv] = val;
                }
                else {
                    int tm = (tl + tr) / 2;
                    if (idx <= tm) setval(idx, val, 2 * tv, tl, tm);
                    else setval(idx, val, 2 * tv + 1, tm + 1, tr);
                    merge(tv, tl, tr);
                }
            }
        };

        SegmentTree tree(n, a);
        cout << max(tree.oma[1], tree.ema[1]) << "\n";
        for (int i = 0; i < q; ++i) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            tree.setval(l, a[r]);
            tree.setval(r, a[l]);
            swap(a[l], a[r]);
            cout << max(tree.oma[1], tree.ema[1]) << "\n";
        }
//        cout << "\n";
    }
}


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

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
//    ios_base::sync_with_stdio(0); cin.tie(0);

    const ll inf = 1e12;
    struct SegmentTree {
        int n;
        struct Node {
            ll omi, emi, oma, ema;
        };
        vector<Node> tree;
        SegmentTree(int n) : n(n), tree(4 * n) {}
        void merge(int tv) {
            auto &l = tree[2 * tv];
            auto &r = tree[2 * tv + 1];
            auto &we = tree[tv];

            we.oma = max({l.oma, r.oma, l.oma - r.emi, l.ema + r.oma});
            we.omi = min({l.omi, r.omi, l.omi - r.ema, l.emi + r.omi});

            we.ema = max({l.ema, r.ema, l.ema + r.ema, l.oma - r.omi});
            we.emi = min({l.emi, r.emi, l.emi + r.emi, l.omi - r.oma});
        }
        void build(int n_, vector<int> &a) {
            n = n_;
            build(a, 1, 0, n - 1);
        }
        void build(vector<int> &a, int tv, int tl, int tr) {
            if (tl == tr) {
                auto &we = tree[tv];
                we.oma = a[tr];
                we.omi = a[tr];
                we.ema = -inf;
                we.emi = inf;
            }
            else {
                int tm = (tl + tr) / 2;
                build(a, 2 * tv, tl, tm);
                build(a, 2 * tv + 1, tm + 1, tr);
                merge(tv);
            }
        }
        void setval(int idx, int val) {
            setval(idx, val, 1, 0, n - 1);
        }
        void setval(int idx, int val, int tv, int tl, int tr) {
            if (tl == tr) {
                tree[tv].oma = val;
                tree[tv].omi = val;
            }
            else {
                int tm = (tl + tr) / 2;
                if (idx <= tm) setval(idx, val, 2 * tv, tl, tm);
                else setval(idx, val, 2 * tv + 1, tm + 1, tr);
                merge(tv);
            }
        }
        ll getmax() {
            return max(tree[1].oma, tree[1].ema);
        }
    };

    const int maxn = 3e5;
    SegmentTree tree(maxn);

    int t;
//    cin >> t;
    scanf("%d", &t);
    while (t--) {
        int n, q;
//        cin >> n >> q;
        scanf("%d%d", &n, &q);
        vector<int> a(n);
//        for (auto &e : a) cin >> e;
        for (auto &e : a) scanf("%d", &e);

        tree.build(n, a);
//        cout << tree.getmax() << "\n";
        printf("%lld\n", tree.getmax());
        for (int i = 0; i < q; ++i) {
            int l, r;
            cin >> l >> r;
            --l, --r;
            tree.setval(l, a[r]);
            tree.setval(r, a[l]);
            swap(a[l], a[r]);
            printf("%lld\n", tree.getmax());
//            cout << tree.getmax() << "\n";
        }
//        cout << "\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int m;
    int a[100050];
    int b[100050];
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        cin >> a[i];
        b[a[i]]=b[a[i]-1]+1;

    }

    sort(b+1,b+m+1);
    if(b[m]==m)
    printf("0\n");
    else
    printf("%d\n",m-b[m]);

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int m;
    int a[100010];
    int b[100010];
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {cin >> a[i];
    b[a[i]]=b[a[i]-1]+1;

    }

    sort(b+1,b+m+1);
    if(b[m]==m)
    printf("0\n");
    else
    printf("%d\n",m-b[m]);

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int m;
    ll a[100005];
    ll b[100005];
    cin >> m;
    for(int i=1;i<=m;i++)
    {cin >> a[i];
    b[a[i]]=b[a[i]-1]+1;
    }

    sort(b+1,b+m+1);
    if(b[m]==m)
    printf("0\n");
    else
    printf("%I64d\n",m-b[m]);

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int m;
    int a[100010];
    int b[100010];
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        cin >> a[i];
        b[a[i]]=b[a[i]-1]+1;

    }
    sort(b+2,b+m+2);
    if(b[m]==m)
    printf("0\n");
    else
    printf("%d\n",m-b[m]);

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int m;
    ll a[100005];
    ll b[100005];
    cin >> m;
    for(int i=1;i<=m;i++) cin >> a[i];
    for(int i=1;i<=m;i++)
    {
        b[a[i]]=b[a[i]-1]+1;
    }
    sort(b+1,b+m+1);
    if(b[m]>=m)
    printf("0\n");
    else
    printf("%I64d\n",m-b[m]);

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int m;
    int a[100010];
    int b[100010];
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        cin >> a[i];
        b[a[i]]=b[a[i]-1]+1;

    }
    sort(b+2,b+m+1);
    if(b[m]==m)
    printf("0\n");
    else
    printf("%d\n",m-b[m]);

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int m;
    ll a[100005];
    ll b[100005];
    cin >> m;
    for(int i=1;i<=m;i++) cin >> a[i];
    for(int i=1;i<=m;i++)
    {
        b[a[i]]=b[a[i]-1]+1;
    }
    sort(b+1,b+m+1);
    if(b[m]==m)
    printf("0\n");
    else
    printf("I64d\n",m-b[m]);

}


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

int main()
{
    int m;
    ll a[100005];
    ll b[100005];
    cin >> m;
    for(int i=1;i<=m;i++) cin >> a[i];
    for(int i=1;i<=m;i++)
    {
        b[a[i]]=b[a[i]-1]+1;
    }
    sort(b+1,b+m+1);
    if(b[m]==m)
    printf("0\n");
    else
    printf("%l64d\n",m-b[m]);

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int m;
    int a[100010];
    int b[100010];
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        cin >> a[i];
        b[a[i]]=b[a[i]-1]+1;

    }

    sort(b+1,b+m+2);
    if(b[m]==m)
    printf("0\n");
    else
    printf("%d\n",m-b[m]);

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int m;
    int a[100050];
    int b[100050];
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        cin >> a[i];
        b[a[i]]=b[a[i]-1]+1;

    }

    sort(b+2,b+m+2);
    if(b[m]==m)
    printf("0\n");
    else
    printf("%d\n",m-b[m+1]);

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int m;
    int a[100050];
    int b[100050];
    memset(b, 0, sizeof(b));
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&a[i]);
        b[a[i]]=b[a[i]-1]+1;

    }

    sort(b+1,b+m+1);
    printf("%d\n",m-b[m]);

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int m;
    int a[100010];
    int b[100010];
    cin >> m;
    for(int i=1;i<=m;i++)
    {cin >> a[i];
    b[a[i]]=b[a[i]-1]+1;

    }

    sort(b+1,b+m+1);
    if(b[m]==m)
    printf("0\n");
    else
    printf("%d\n",m-b[m]);

}


#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>
using namespace std;

#define LL long long int
#define uLL unsigned long long int

#define S(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SLL(a) scanf("%lld",&a)
#define SLL2(a,b) scanf("%lld%lld",&a,&b)
#define SLL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define SC(a) scanf("%c",&a)
#define P(a) printf("%d",a)
#define PS(a) printf("%s",a)
#define PLL(a) printf("%lld",a)
#define PCASE(kk) printf("Case %d: ",kk++)
#define PCASENL(kk) printf("Case %d:\n",kk++)
#define NL puts("")

#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pi (2.0*acos(0.0))
#define pii pair<int,int>

template<typename T>inline T POW(T B,T P)
{
    if(P==0) return 1;
    if(P&1) return B*POW(B,P-1);
    else return SQR(POW(B,P/2));
}
template <typename T>inline T ModInv (T b,T m)
{
    return BigMod(b,m-2,m);
}
template<typename T>inline T ABS(T a)
{
    if(a<0)return -a;
    else return a;
}
template<typename T>inline T gcd(T a,T b)
{
    if(a<0)return gcd(-a,b);
    if(b<0)return gcd(a,-b);
    return (b==0)?a:gcd(b,a%b);
}
template<typename T>inline T lcm(T a,T b)
{
    if(a<0)return lcm(-a,b);
    if(b<0)return lcm(a,-b);
    return a*(b/gcd(a,b));
}
template <class T> inline T BMOD(T p,T e,T m)
{
    T ret=1;
    while(e)
    {
        if(e&1) ret=(ret*p)%m;
        p=(p*p)%m;
        e>>=1;
    }
    return (T)ret;
}

//for(__typeof(pp.begin()) i=pp.begin(); i!=pp.end(); i++ )

//knight and king move....

//int Dx[]={-2,-1,1,2,1,2,-2,-1};
//int Dy[]={-1,-2,2,1,-2,-1,1,2};
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
//////////////////////////////////////////////////

int n,arr[100005],pos[100005];

int main()
{
    S(n);
    for(int i=0;i<n;i++)
    {
        S(arr[i]);
        pos[arr[i]]=i;
    }

    int cnt=0,exp=1;
    for(int i=1;i<=n;i++)
    {
        if(arr[i]==exp)
        {
            exp++;
            cnt++;
        }
    }

    int ans=n-cnt;
    P(ans),NL;

    return 0;
}


#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>
using namespace std;

#define LL long long int
#define uLL unsigned long long int

#define S(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SLL(a) scanf("%lld",&a)
#define SLL2(a,b) scanf("%lld%lld",&a,&b)
#define SLL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define SC(a) scanf("%c",&a)
#define P(a) printf("%d",a)
#define PS(a) printf("%s",a)
#define PLL(a) printf("%lld",a)
#define PCASE(kk) printf("Case %d: ",kk++)
#define PCASENL(kk) printf("Case %d:\n",kk++)
#define NL puts("")

#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pi (2.0*acos(0.0))
#define pii pair<int,int>

template<typename T>inline T POW(T B,T P)
{
    if(P==0) return 1;
    if(P&1) return B*POW(B,P-1);
    else return SQR(POW(B,P/2));
}
template <typename T>inline T ModInv (T b,T m)
{
    return BigMod(b,m-2,m);
}
template<typename T>inline T ABS(T a)
{
    if(a<0)return -a;
    else return a;
}
template<typename T>inline T gcd(T a,T b)
{
    if(a<0)return gcd(-a,b);
    if(b<0)return gcd(a,-b);
    return (b==0)?a:gcd(b,a%b);
}
template<typename T>inline T lcm(T a,T b)
{
    if(a<0)return lcm(-a,b);
    if(b<0)return lcm(a,-b);
    return a*(b/gcd(a,b));
}
template <class T> inline T BMOD(T p,T e,T m)
{
    T ret=1;
    while(e)
    {
        if(e&1) ret=(ret*p)%m;
        p=(p*p)%m;
        e>>=1;
    }
    return (T)ret;
}

//for(__typeof(pp.begin()) i=pp.begin(); i!=pp.end(); i++ )

//knight and king move....

//int Dx[]={-2,-1,1,2,1,2,-2,-1};
//int Dy[]={-1,-2,2,1,-2,-1,1,2};
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
//////////////////////////////////////////////////

int n,arr[100005],pos[100005];

int main()
{
    S(n);
    for(int i=1;i<=n;i++)
    {
        S(arr[i]);
    }

    int cnt=0,exp=1;
    for(int i=1;i<=n;i++)
    {
        pos[arr[i]]=1+pos[arr[i]-1];
        cnt=max(cnt,pos[arr[i]]);
    }
//    cout<<cnt<<endl;
    int ans=n-cnt;
    P(ans),NL;

    return 0;
}


#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>
using namespace std;

#define LL long long int
#define uLL unsigned long long int

#define S(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SLL(a) scanf("%lld",&a)
#define SLL2(a,b) scanf("%lld%lld",&a,&b)
#define SLL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define SC(a) scanf("%c",&a)
#define P(a) printf("%d",a)
#define PS(a) printf("%s",a)
#define PLL(a) printf("%lld",a)
#define PCASE(kk) printf("Case %d: ",kk++)
#define PCASENL(kk) printf("Case %d:\n",kk++)
#define NL puts("")

#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pi (2.0*acos(0.0))
#define pii pair<int,int>

template<typename T>inline T POW(T B,T P)
{
    if(P==0) return 1;
    if(P&1) return B*POW(B,P-1);
    else return SQR(POW(B,P/2));
}
template <typename T>inline T ModInv (T b,T m)
{
    return BigMod(b,m-2,m);
}
template<typename T>inline T ABS(T a)
{
    if(a<0)return -a;
    else return a;
}
template<typename T>inline T gcd(T a,T b)
{
    if(a<0)return gcd(-a,b);
    if(b<0)return gcd(a,-b);
    return (b==0)?a:gcd(b,a%b);
}
template<typename T>inline T lcm(T a,T b)
{
    if(a<0)return lcm(-a,b);
    if(b<0)return lcm(a,-b);
    return a*(b/gcd(a,b));
}
template <class T> inline T BMOD(T p,T e,T m)
{
    T ret=1;
    while(e)
    {
        if(e&1) ret=(ret*p)%m;
        p=(p*p)%m;
        e>>=1;
    }
    return (T)ret;
}

//for(__typeof(pp.begin()) i=pp.begin(); i!=pp.end(); i++ )

//knight and king move....

//int Dx[]={-2,-1,1,2,1,2,-2,-1};
//int Dy[]={-1,-2,2,1,-2,-1,1,2};
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
//////////////////////////////////////////////////

int n,arr[100005],pos[100005];

int main()
{
    S(n);
    for(int i=1;i<=n;i++)
    {
        S(arr[i]);
        pos[arr[i]]=i;

    }

    int cnt=0,exp=1;
    for(int i=1;i<=n;i++)
    {
        if(arr[i]==exp)
        {
//            exp++;
            while(pos[exp]<=i)exp++;
            cnt++;
        }

    }
//    cout<<cnt<<endl;
    int ans=n-cnt;
    P(ans),NL;

    return 0;
}


#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>
using namespace std;

#define LL long long int
#define uLL unsigned long long int

#define S(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SLL(a) scanf("%lld",&a)
#define SLL2(a,b) scanf("%lld%lld",&a,&b)
#define SLL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define SC(a) scanf("%c",&a)
#define P(a) printf("%d",a)
#define PS(a) printf("%s",a)
#define PLL(a) printf("%lld",a)
#define PCASE(kk) printf("Case %d: ",kk++)
#define PCASENL(kk) printf("Case %d:\n",kk++)
#define NL puts("")

#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pi (2.0*acos(0.0))
#define pii pair<int,int>

template<typename T>inline T POW(T B,T P)
{
    if(P==0) return 1;
    if(P&1) return B*POW(B,P-1);
    else return SQR(POW(B,P/2));
}
template <typename T>inline T ModInv (T b,T m)
{
    return BigMod(b,m-2,m);
}
template<typename T>inline T ABS(T a)
{
    if(a<0)return -a;
    else return a;
}
template<typename T>inline T gcd(T a,T b)
{
    if(a<0)return gcd(-a,b);
    if(b<0)return gcd(a,-b);
    return (b==0)?a:gcd(b,a%b);
}
template<typename T>inline T lcm(T a,T b)
{
    if(a<0)return lcm(-a,b);
    if(b<0)return lcm(a,-b);
    return a*(b/gcd(a,b));
}
template <class T> inline T BMOD(T p,T e,T m)
{
    T ret=1;
    while(e)
    {
        if(e&1) ret=(ret*p)%m;
        p=(p*p)%m;
        e>>=1;
    }
    return (T)ret;
}

//for(__typeof(pp.begin()) i=pp.begin(); i!=pp.end(); i++ )

//knight and king move....

//int Dx[]={-2,-1,1,2,1,2,-2,-1};
//int Dy[]={-1,-2,2,1,-2,-1,1,2};
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
//////////////////////////////////////////////////

int n,arr[100005],dp[100005],pos[100005];

int binery_search(int h,int l,int b)
{
    int m;
    while(l<h)
    {
        m=(l+h)/2;
        if(dp[m]<b)
        {
            l=m+1;
        }
        else h=m;
    }
    return h;
}

int LIS(int n)
{
    int len=1;
    dp[0]=arr[0];
    pos[0]=0;
    for(int i=1;i<n;i++)
    {
        if(arr[i]<dp[0])
        {
            dp[0]=arr[i];
            pos[i]=0;
        }
        else if(arr[i]>dp[len-1])
        {
            dp[len++]=arr[i];
            pos[i]=len-1;
        }
        else{
            int p=binery_search(len-1,0,arr[i]);
            dp[p]=arr[i];
            pos[i]=p;
        }
    }
    return len;
}

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

    int lis=LIS(n);
    int ans=n-lis;
    P(ans),NL;

    return 0;
}


#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>
using namespace std;

#define LL long long int
#define uLL unsigned long long int

#define S(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SLL(a) scanf("%lld",&a)
#define SLL2(a,b) scanf("%lld%lld",&a,&b)
#define SLL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define SC(a) scanf("%c",&a)
#define P(a) printf("%d",a)
#define PS(a) printf("%s",a)
#define PLL(a) printf("%lld",a)
#define PCASE(kk) printf("Case %d: ",kk++)
#define PCASENL(kk) printf("Case %d:\n",kk++)
#define NL puts("")

#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pi (2.0*acos(0.0))
#define pii pair<int,int>

template<typename T>inline T POW(T B,T P)
{
    if(P==0) return 1;
    if(P&1) return B*POW(B,P-1);
    else return SQR(POW(B,P/2));
}
template <typename T>inline T ModInv (T b,T m)
{
    return BigMod(b,m-2,m);
}
template<typename T>inline T ABS(T a)
{
    if(a<0)return -a;
    else return a;
}
template<typename T>inline T gcd(T a,T b)
{
    if(a<0)return gcd(-a,b);
    if(b<0)return gcd(a,-b);
    return (b==0)?a:gcd(b,a%b);
}
template<typename T>inline T lcm(T a,T b)
{
    if(a<0)return lcm(-a,b);
    if(b<0)return lcm(a,-b);
    return a*(b/gcd(a,b));
}
template <class T> inline T BMOD(T p,T e,T m)
{
    T ret=1;
    while(e)
    {
        if(e&1) ret=(ret*p)%m;
        p=(p*p)%m;
        e>>=1;
    }
    return (T)ret;
}

//for(__typeof(pp.begin()) i=pp.begin(); i!=pp.end(); i++ )

//knight and king move....

//int Dx[]={-2,-1,1,2,1,2,-2,-1};
//int Dy[]={-1,-2,2,1,-2,-1,1,2};
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
//////////////////////////////////////////////////

int n,arr[100005],dp[100005],pos[100005];

int binery_search(int h,int l,int b)
{
    int m;
    while(l<h)
    {
        m=(l+h)/2;
        if(dp[m]<b)
        {
            l=m+1;
        }
        else h=m;
    }
    return h;
}

int LIS(int n)
{
    int len=1;
    dp[0]=arr[0];
    pos[0]=0;
    for(int i=1;i<n;i++)
    {
        if(arr[i]<dp[0])
        {
            dp[0]=arr[i];
            pos[i]=0;
        }
        else if(arr[i]>dp[len-1])
        {
            dp[len++]=arr[i];
            pos[i]=len-1;
        }
        else{
            int p=binery_search(len-1,0,arr[i]);
            dp[p]=arr[i];
            pos[i]=p;
        }
    }
    return len;
}

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

    int lis=LIS(n);
    int ans=n-lis;
    cout<<ans<<endl;

    return 0;
}


#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>
using namespace std;

#define LL long long int
#define uLL unsigned long long int

#define S(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SLL(a) scanf("%lld",&a)
#define SLL2(a,b) scanf("%lld%lld",&a,&b)
#define SLL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define SC(a) scanf("%c",&a)
#define P(a) printf("%d",a)
#define PS(a) printf("%s",a)
#define PLL(a) printf("%lld",a)
#define PCASE(kk) printf("Case %d: ",kk++)
#define PCASENL(kk) printf("Case %d:\n",kk++)
#define NL puts("")

#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pi (2.0*acos(0.0))
#define pii pair<int,int>

template<typename T>inline T POW(T B,T P)
{
    if(P==0) return 1;
    if(P&1) return B*POW(B,P-1);
    else return SQR(POW(B,P/2));
}
template <typename T>inline T ModInv (T b,T m)
{
    return BigMod(b,m-2,m);
}
template<typename T>inline T ABS(T a)
{
    if(a<0)return -a;
    else return a;
}
template<typename T>inline T gcd(T a,T b)
{
    if(a<0)return gcd(-a,b);
    if(b<0)return gcd(a,-b);
    return (b==0)?a:gcd(b,a%b);
}
template<typename T>inline T lcm(T a,T b)
{
    if(a<0)return lcm(-a,b);
    if(b<0)return lcm(a,-b);
    return a*(b/gcd(a,b));
}
template <class T> inline T BMOD(T p,T e,T m)
{
    T ret=1;
    while(e)
    {
        if(e&1) ret=(ret*p)%m;
        p=(p*p)%m;
        e>>=1;
    }
    return (T)ret;
}

//for(__typeof(pp.begin()) i=pp.begin(); i!=pp.end(); i++ )

//knight and king move....

//int Dx[]={-2,-1,1,2,1,2,-2,-1};
//int Dy[]={-1,-2,2,1,-2,-1,1,2};
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
//////////////////////////////////////////////////

int n,arr[100005],pos[100005];

int main()
{
    S(n);
    for(int i=1;i<=n;i++)
    {
        S(arr[i]);

    }

    int cnt=0,exp=1;
    for(int i=1;i<=n;i++)
    {
        if(arr[i]==exp)
        {
            exp++;
            cnt++;
        }
    }
//    cout<<cnt<<endl;
    int ans=n-cnt;
    P(ans),NL;

    return 0;
}


#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>
using namespace std;

#define LL long long int
#define uLL unsigned long long int

#define S(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SLL(a) scanf("%lld",&a)
#define SLL2(a,b) scanf("%lld%lld",&a,&b)
#define SLL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define SC(a) scanf("%c",&a)
#define P(a) printf("%d",a)
#define PS(a) printf("%s",a)
#define PLL(a) printf("%lld",a)
#define PCASE(kk) printf("Case %d: ",kk++)
#define PCASENL(kk) printf("Case %d:\n",kk++)
#define NL puts("")

#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pi (2.0*acos(0.0))
#define pii pair<int,int>

template<typename T>inline T POW(T B,T P)
{
    if(P==0) return 1;
    if(P&1) return B*POW(B,P-1);
    else return SQR(POW(B,P/2));
}
template <typename T>inline T ModInv (T b,T m)
{
    return BigMod(b,m-2,m);
}
template<typename T>inline T ABS(T a)
{
    if(a<0)return -a;
    else return a;
}
template<typename T>inline T gcd(T a,T b)
{
    if(a<0)return gcd(-a,b);
    if(b<0)return gcd(a,-b);
    return (b==0)?a:gcd(b,a%b);
}
template<typename T>inline T lcm(T a,T b)
{
    if(a<0)return lcm(-a,b);
    if(b<0)return lcm(a,-b);
    return a*(b/gcd(a,b));
}
template <class T> inline T BMOD(T p,T e,T m)
{
    T ret=1;
    while(e)
    {
        if(e&1) ret=(ret*p)%m;
        p=(p*p)%m;
        e>>=1;
    }
    return (T)ret;
}

//for(__typeof(pp.begin()) i=pp.begin(); i!=pp.end(); i++ )

//knight and king move....

//int Dx[]={-2,-1,1,2,1,2,-2,-1};
//int Dy[]={-1,-2,2,1,-2,-1,1,2};
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
//////////////////////////////////////////////////

int n,arr[100005],pos[100005];

int main()
{
    S(n);
    for(int i=1;i<=n;i++)
    {
        S(arr[i]);

    }

    int cnt=0,exp=1;
    for(int i=1;i<=n;i++)
    {
        if(arr[i]==exp)
        {
            exp++;
            cnt++;
        }
    }
    cout<<cnt<<endl;
    int ans=n-cnt;
    P(ans),NL;

    return 0;
}


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

typedef long long ll;
typedef pair< int, int > pii;
typedef vector< int > vi;
typedef vector< vi > vvi;

const int INF = 0x3f3f3f3f;

int subSolve( const vi &a ){
    vi pos( a.size() + 1 );
    for( int i = 0; i < a.size(); ++i )
        pos[ a[ i ] ] = i;
    for( int i = 2; i <= a.size(); ++i )
        if( pos[ i ] < pos[ i - 1 ] )
            return a.size() - i + 1;
    return 0;
}

int ss( const vi &a ){
    vi pos( a.size() + 1 );
    for( int i = 0; i < a.size(); ++i )
        pos[ a[ i ] ] = i;
    for( int i = a.size() - 1; i >= 1; --i )
        if( pos[ i ] > pos[ i + 1 ] )
            return i;
    return 0;
}

void solve(){
    int N; cin >> N;
    vi A( N );
    for( int i = 0; i < N; ++i )
        cin >> A[ i ];
    int ans = subSolve( A );
    ans = min( ans, ss( A ) );
    cout << ans << "\n";
}

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


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

typedef long long ll;
typedef pair< int, int > pii;
typedef vector< int > vi;
typedef vector< vi > vvi;

const int INF = 0x3f3f3f3f;

int LIS( const vi &a ){
    vi t( a.size(), INF );
    for( int i = 0; i < a.size(); ++i ){
        int idx = lower_bound( t.begin(), t.end(), a[ i ] ) - t.begin();
        t[ idx ] = a[ i ];
    }
    for( int i = a.size() - 1; i >= 0; --i )
        if( t[ i ] != INF )
            return i + 1;
}

void solve(){
    int N; cin >> N;
    vi A( N );
    for( int i = 0; i < N; ++i )
        cin >> A[ i ];
    vi pos( N );
    for( int i = 0; i < N; ++i )
        pos[ A[ i ] - 1 ] = i;
    cout << N - LIS( pos ) << "\n";
}

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


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

typedef long long ll;
typedef pair< int, int > pii;
typedef vector< int > vi;
typedef vector< vi > vvi;

const int INF = 0x3f3f3f3f;

int subSolve( const vi &a ){
    vi pos( a.size() + 1 );
    for( int i = 0; i < a.size(); ++i )
        pos[ a[ i ] ] = i;
    for( int i = 2; i <= a.size(); ++i )
        if( pos[ i ] < pos[ i - 1 ] )
            return a.size() - i + 1;
    return 0;
}

int ss( const vi &a ){
    vi pos( a.size() + 1 );
    for( int i = 0; i < a.size(); ++i )
        pos[ a[ i ] ] = i;
    for( int i = a.size() - 1; i >= 1; --i )
        if( pos[ i ] < pos[ i + 1 ] )
            return a.size() - i + 1;
    return 0;
}

void solve(){
    int N; cin >> N;
    vi A( N );
    for( int i = 0; i < N; ++i )
        cin >> A[ i ];
    int ans = subSolve( A );
    ans = min( ans, ss( A ) );
    cout << ans << "\n";
}

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


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

typedef long long ll;
typedef pair< int, int > pii;
typedef vector< int > vi;
typedef vector< vi > vvi;

const int INF = 0x3f3f3f3f;

int LIS( const vi &a ){
    vi t( a.size(), INF );
    for( int i = 0; i < a.size(); ++i ){
        int idx = lower_bound( t.begin(), t.end(), a[ i ] ) - t.begin();
        t[ idx ] = a[ i ];
    }
    for( int i = a.size() - 1; i >= 0; --i )
        if( t[ i ] != INF )
            return i + 1;
}

void solve(){
    int N; cin >> N;
    vi A( N );
    for( int i = 0; i < N; ++i )
        cin >> A[ i ];
    cout << N - LIS( A ) << "\n";
}

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


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

typedef long long ll;
typedef pair< int, int > pii;
typedef vector< int > vi;
typedef vector< vi > vvi;

const int INF = 0x3f3f3f3f;

void solve(){
    int N; cin >> N;
    vi A( N );
    for( int i = 0; i < N; ++i )
        cin >> A[ i ];
    vi pos( N );
    for( int i = 0; i < N; ++i )
        pos[ A[ i ] - 1 ] = i;
    int ans = 0;
    for( int i = 2, cnt = 1; i <= N; ++i ){
        if( pos[ i ] < pos[ i - 1 ] )
            cnt = 1;
        else
            ++cnt;
        ans = max( ans, cnt );
    }
    cout << ans << "\n";
}

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


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

typedef long long ll;
typedef pair< int, int > pii;
typedef vector< int > vi;
typedef vector< vi > vvi;

const int INF = 0x3f3f3f3f;

int subSolve( const vi &a ){
    map< int, int > pos;
    for( int i = 0; i < a.size(); ++i )
        pos[ a[ i ] ] = i;
    for( int i = 2; i <= a.size(); ++i )
        if( pos[ i ] < pos[ i - 1 ] )
            return a.size() - i + 1;
    return 0;
}

int ss( const vi &a ){
    vi pos( a.size() + 1 );
    for( int i = 0; i < a.size(); ++i )
        pos[ a[ i ] ] = i;
    for( int i = a.size() - 1; i >= 1; --i )
        if( pos[ i ] < pos[ i - 1 ] )
            return a.size() - i + 1;
    return 0;
}

void solve(){
    int N; cin >> N;
    vi A( N );
    for( int i = 0; i < N; ++i )
        cin >> A[ i ];
    int ans = subSolve( A );
    ans = min( ans, ss( A ) );
    cout << ans << "\n";
}

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


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

typedef long long ll;
typedef pair< int, int > pii;
typedef vector< int > vi;
typedef vector< vi > vvi;

const int INF = 0x3f3f3f3f;

void solve(){
    int N; cin >> N;
    vi A( N + 1 );
    for( int i = 1; i <= N; ++i )
        cin >> A[ i ];
    vi pos( N + 1 );
    for( int i = 1; i <= N; ++i )
        pos[ A[ i ] ] = i;
    int ans = 1;
    for( int i = 2, cnt = 1; i <= N; ++i ){
        if( pos[ i ] < pos[ i - 1 ] )
            cnt = 1;
        else
            ++cnt;
        ans = max( ans, cnt );
    }
    cout << N - ans << "\n";
}

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


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

typedef long long ll;
typedef pair< int, int > pii;
typedef vector< int > vi;
typedef vector< vi > vvi;

const int INF = 0x3f3f3f3f;

int subSolve( const vi &a ){
    vi pos( a.size() + 1 );
    for( int i = 0; i < a.size(); ++i )
        pos[ a[ i ] ] = i;
    for( int i = 2; i <= a.size(); ++i )
        if( pos[ i ] < pos[ i - 1 ] )
            return a.size() - i + 1;
    return 0;
}

int ss( const vi &a ){
    vi pos( a.size() + 1 );
    for( int i = 0; i < a.size(); ++i )
        pos[ a[ i ] ] = i;
    for( int i = a.size() - 1; i >= 1; --i )
        if( pos[ i ] < pos[ i + 1 ] )
            return i;
    return 0;
}

void solve(){
    int N; cin >> N;
    vi A( N );
    for( int i = 0; i < N; ++i )
        cin >> A[ i ];
    int ans = subSolve( A );
    ans = min( ans, ss( A ) );
    cout << ans << "\n";
}

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


#include <bits/stdc++.h>

#define endl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define in insert
#define pii pair<int,int>
#define pll pair<long long,long long>
#define psi pair<string,int>
#define psl pair<string,long long>
#define fc(k) for(int i=0;i<k;i++)
#define fc1(k) for(int i=1;i<=k;i++)
#define fm(k) for(int i=k-1;i>=0;i--)
#define fm1(k) for(int i=k;i>=1;i--)

typedef long long ll;
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);
    int n,c=0;
    cin>>n;
    int*mas=new int[n];
    fc(n)cin>>mas[i];
    fc(n-1)if(mas[i]>mas[i+1])c++;
    cout<<c;
	return 0;
}


#include <bits/stdc++.h>

#define endl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define in insert
#define pii pair<int,int>
#define pll pair<long long,long long>
#define psi pair<string,int>
#define psl pair<string,long long>
#define fc(k) for(int i=0;i<k;i++)
#define fc1(k) for(int i=1;i<=k;i++)
#define fm(k) for(int i=k-1;i>=0;i--)
#define fm1(k) for(int i=k;i>=1;i--)

typedef long long ll;
using namespace std;
int move_count(int *a, int n) {
    int j = n-1;
    while(j > 0 && a[j-1] < a[j]) --j;
    if (j == 0) return 0;   // the array is already sorted
    // min_val holds the smallest element in the array after
    // the currently investigated position, v_1 holds the smallest
    // element after the current position that is larger than any later
    int min_val = a[j], v_1 = a[j-1];
    j -= 2;
    while(j >= 0) {
        if (a[j] < min_val) {
            min_val = a[j];
        } else if (a[j] < v_1) {
            v_1 = a[j];
        }
        --j;
    }
    int count_smaller = 0;
    for(j = 0; j < n; ++j) {
        if (a[j] < v_1) ++count_smaller;
    }
    return n - count_smaller;
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);
    int n,c=0;
    cin>>n;
    list<int>mas;
    fc(n){cin>>c;mas.push_back(c);}
    //fc(n-1)if(mas[i]>mas[i+1])c++;
    //cout<<move_count(mas,n)<<endl;
    int prev, next;c=0;
    list<int>::iterator it=mas.begin();
    prev=(*mas.begin());
    for(int i=1;i<n;i++){

     advance(it, i);
     next=*it;
     if(prev-next>1)
          //push next to start and print the move
          {
            mas.erase(it);mas.push_front(next);c++;
          }
     else if(prev-next<1)
          //push next to end print the move
          {
            mas.erase(it);mas.push_back(next);c++;
          }
          }
          cout<<c;
	return 0;
}


#include <bits/stdc++.h>

#define endl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define in insert
#define pii pair<int,int>
#define pll pair<long long,long long>
#define psi pair<string,int>
#define psl pair<string,long long>
#define fc(k) for(int i=0;i<k;i++)
#define fc1(k) for(int i=1;i<=k;i++)
#define fm(k) for(int i=k-1;i>=0;i--)
#define fm1(k) for(int i=k;i>=1;i--)

typedef long long ll;
using namespace std;
int move_count(int *a, int n) {
    int j = n-1;
    while(j > 0 && a[j-1] < a[j]) --j;
    if (j == 0) return 0;   // the array is already sorted
    // min_val holds the smallest element in the array after
    // the currently investigated position, v_1 holds the smallest
    // element after the current position that is larger than any later
    int min_val = a[j], v_1 = a[j-1];
    j -= 2;
    while(j >= 0) {
        if (a[j] < min_val) {
            min_val = a[j];
        } else if (a[j] < v_1) {
            v_1 = a[j];
        }
        --j;
    }
    int count_smaller = 0;
    for(j = 0; j < n; ++j) {
        if (a[j] < v_1) ++count_smaller;
    }
    return n - count_smaller;
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);
    int n,c=0;
    cin>>n;
    int*mas=new int[n];
    fc(n)cin>>mas[i];
    //fc(n-1)if(mas[i]>mas[i+1])c++;
    cout<<move_count(mas,n)<<endl;
	return 0;
}


#include <bits/stdc++.h>

#define endl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define in insert
#define pii pair<int,int>
#define pll pair<long long,long long>
#define psi pair<string,int>
#define psl pair<string,long long>
#define fc(k) for(int i=0;i<k;i++)
#define fc1(k) for(int i=1;i<=k;i++)
#define fm(k) for(int i=k-1;i>=0;i--)
#define fm1(k) for(int i=k;i>=1;i--)

typedef long long ll;
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);
    int n,c=-1,k=-1;
    cin>>n;
    int*mas=new int[n];
    fc(n)cin>>mas[i];
    int i=0,d=0;
    while(n)
    {
        if(i==n)break;
        if(mas[i]>k)d++;
        else d=1;
        c=max(c,d);
        k=mas[i];
        i++;
    }
    cout<<n-c;
	return 0;
}


#include <bits/stdc++.h>

#define endl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define in insert
#define pii pair<int,int>
#define pll pair<long long,long long>
#define psi pair<string,int>
#define psl pair<string,long long>
#define fc(k) for(int i=0;i<k;i++)
#define fc1(k) for(int i=1;i<=k;i++)
#define fm(k) for(int i=k-1;i>=0;i--)
#define fm1(k) for(int i=k;i>=1;i--)

typedef long long ll;
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);
    int n,c=-1,k=-1;
    cin>>n;
    int*mas=new int[n];
    fc(n)cin>>mas[i];
    int i=0,d=0;
    while(n)
    {
        if(i==n)break;
        if(mas[i]>k)d++;
        else d=0;
        c=max(c,d);
        k=mas[i];
        i++;
    }
    cout<<n-c;
	return 0;
}


#include <bits/stdc++.h>

#define endl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define in insert
#define pii pair<int,int>
#define pll pair<long long,long long>
#define psi pair<string,int>
#define psl pair<string,long long>
#define fc(k) for(int i=0;i<k;i++)
#define fc1(k) for(int i=1;i<=k;i++)
#define fm(k) for(int i=k-1;i>=0;i--)
#define fm1(k) for(int i=k;i>=1;i--)

typedef long long ll;
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);
    int DP[500000]={},Pos[500000]={};
    int n, cur;
    cin>>n;
    int s = 0;
    fc1(n) {
        cin>>cur;
        DP[i] = DP[Pos[cur-1]] + 1;
        Pos[cur] = i;
        s = max(s, DP[i]);
    }
    cout << n-s;
}


#include <iostream>

using namespace std;

int c[100005];

int CeilIndex( int l, int r, int key)
{
	while (r - l > 1)
	{
		int m = l + (r - l) / 2;
		if (c[m] >= key)
			r = m;
		else
			l = m;
	}
	return r;
}

int f(int A[], int n)
{
	int len;

	c[0] = A[0];

	len = 1;
	for (int i = 1; i < n; i++)
	{
		if (A[i] < c[0])
			c[0] = A[i];
		else
			if (A[i] - 1 == c[len - 1])
				c[len++] = A[i];
	}
	return len;
}

int main(){
	int n, v[100005];
	cin >> n;
	for (int i = 0; i < n; ++i)
		cin >> v[i];

	cout << n - f(v, n);

	return 0;
}


#include <iostream>

using namespace std;

int f(int v[], int n)
{
	int lungime;
	int inceput;
	int i, j;
	int *maxime = new int[n];
	int *urmatorul = new int[n];
	int *subsir = new int[n];

	maxime[n - 1] = 1;
	urmatorul[n - 1] = -1;
	for (i = n - 2; i >= 0; i--){
		maxime[i] = 1;
		urmatorul[i] = -1;
		for (j = i + 1; j < n; j++){
			if (v[i] + 1 == v[j] && maxime[i] <= maxime[j]){
				maxime[i] = maxime[j] + 1;
				urmatorul[i] = j;
			}
		}
	}
	lungime = maxime[0];
	inceput = 0;
	for (i = 1; i < n; i++){
			if (lungime < maxime[i]){
				lungime = maxime[i];
				inceput = i;
			}
	}
	subsir[0] = v[inceput];
	for (i = 1; i < lungime; i++){
		inceput = urmatorul[inceput];
		subsir[i] = v[inceput];
	}

	delete maxime;
	return lungime;
}

int main(){
	int n, v[100005];
	cin >> n;
	for (int i = 0; i < n; ++i)
		cin >> v[i];

	cout << n - f(v, n);

	return 0;
}


#include <iostream>
#define maxN 100005

using namespace std;

int c[maxN], v[maxN];
int n;

int f(){
	int l, lm;
	l = lm = 1;

	for (int i = 2; i <= n; ++i){
		if (c[i] > c[i - 1])
			l++;
		else
			l = 1;

		if (l > lm)
			lm = l;
	}

	return lm;
}

int main(){
	cin >> n;
	c[0] = 0;

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

	cout << n - f();

	return 0;
}


#include <iostream>
#define maxN 100005

using namespace std;

int c[maxN], v[maxN];
int n;

int f(){
	int l, lm;
	l = lm = 1;

	for (int i = 1; i < n; ++i){
		if (c[i] > c[i - 1])
			l++;
		else
			l = 1;

		if (l > lm)
			lm = l;
	}

	return lm;
}

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

	cout << n - f();

	return 0;
}


#include <iostream>
#define maxN 100005

using namespace std;

int c[maxN], v[maxN];
int n;

int f(){
	int l, lm;
	l = lm = 1;

	for (int i = 2; i <= n; ++i){
		if (c[i] > c[i - 1])
			l++;
		else
			l = 1;

		if (l > lm)
			lm = l;
	}

	return lm;
}

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

	cout << n - f();

	return 0;
}


#include <iostream>

using namespace std;

int c[100005];

int CeilIndex( int l, int r, int key)
{
	while (r - l > 1)
	{
		int m = l + (r - l) / 2;
		if (c[m] >= key)
			r = m;
		else
			l = m;
	}
	return r;
}

int f(int A[], int n)
{
	int len, lenm;

	c[0] = A[0];

	len = lenm = 1;
	for (int i = 1; i < n; i++)
	{
		if (A[i] < c[0]){
			c[0] = A[i];
			len = 1;
		}
		else
			if (A[i] - 1 == c[len - 1])
				c[len++] = A[i];
		if (len>lenm)lenm = len;
	}
	return lenm;
}

int main(){
	int n, v[100005];
	cin >> n;
	for (int i = 0; i < n; ++i)
		cin >> v[i];

	cout << n - f(v, n);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int arr[100000];
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    int count=0;
    int max=0;
    int min;
    bool flag;
    int seq=1;
    int stop=n;
    for(int i=0;i<n;i++)
    {
        if(arr[i]<stop)
            stop=arr[i];
        seq=arr[i];
        count =1;
        for(int j=i;j<n;j++)
           {
            if(stop==seq+1)
                break;
           if(arr[j]==seq+1)
            {
                seq++;
            count++;
            }
           }
        if(max<count)
        max=count;

    }
    cout<<n-max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int arr[100000];
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    int count=0;
    int max=0;
    int seq=1;
    for(int i=0;i<n;i++)
    {
        seq=arr[i];
        count =1;
        for(int j=i;j<n;j++)
           {

           if(arr[j]==seq+1)
            {
                seq++;
            count++;
            }
           }
        if(max<count)
        max=count;

    }
    cout<<n-max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int arr[100000];
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    int count=0;
    int max=0;
    int min;
    bool flag;
    int seq=1;
    int stop=n;
    for(int i=0;i<n;i++)
    {
        if(arr[i]<stop)
            stop=arr[i];
        arr2[arr[i]]=true;
        seq=arr[i];
        count =1;
        for(int j=i;j<n;j++)
           {
            if(stop==seq+1)
                break;
           if(arr[j]==seq+1)
            {
                seq++;
            count++;
            }
           }
        if(max<count)
        max=count;

    }
    cout<<n-max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int arr2[100001]={0};
    int arr3[100001];
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        arr3[arr[i]]=i;
    }
    int count=0;
    int max=0;
    int seq=1;
    arr3[n+1]=-1;
    for(int i=0;i<n;i++)
    {
        seq=arr[i];
        count =1;
       while(true)
           {
            if(arr3[seq+1]>arr3[seq])
               {

                   count++;
                   seq++;
               }
               else
                break;
           }

        if(max<count)
        max=count;

    }
    cout<<n-max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int arr[100001];
    int arr3[100001];
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
        arr3[arr[i]]=i;
    }
    int count=0;
    int max=0;
    int seq=1;
    arr3[n+1]=-1;
    for(int i=0;i<n;i++)
    {
        seq=arr[i];
        count =1;
       while(true)
           {
            if(arr3[seq+1]>arr3[seq])
               {

                   count++;
                   seq++;
               }
               else
                break;
           }

        if(max<count)
        max=count;

    }
    cout<<n-max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int arr[100001];
    int arr3[100001];
    int n;
    cin>>n;
int sol[100001]
for(int i=0;i<n;i++)
bool sol[100001]={0};
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        arr3[arr[i]]=i;
    }
    int count=0;
    int max=0;
    int seq=1;
    arr3[n+1]=-1;
    for(int i=0;i<n;i++)
    {
        seq=arr[i];
        count =1;
if(sol[seq]==0)
{
       while(true)
           {
            if(arr3[seq+1]>arr3[seq])
               {

                   count++;
                   seq++;
               }
               else
                break;
           }
for(int x=arr[i];x<arr[i]+count-1;x++)
sol[x]=1;
}
        if(max<count)
        max=count;

    }
    cout<<n-max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int arr2[100001]={0};
    int arr[100000];
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    int count=0;
    int max=0;
    int min;
    bool flag;
    int seq=1;
    int stop=n;
    for(int i=0;i<n;i++)
    {
        seq=arr[i];
        arr2[seq]=1;
        count =1;
        for(int j=i;j<n;j++)
           {
            if(arr2[seq+1]==true)
                break;
           if(arr[j]==seq+1)
            {
                seq++;
            count++;
            }
           }
        if(max<count)
        max=count;

    }
    cout<<n-max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int arr[100000];
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    int count=0;
    int max=0;
    int min;
    bool flag;
    int seq=1;
    int stop=-1;
    for(int i=0;i<n;i++)
    {
        stop=max(stop,arr[i]);
        arr2[arr[i]]=true;
        seq=arr[i];
        count =1;
        for(int j=i;j<n;j++)
           {
            if(stop=seq+1)
                break;
           if(arr[j]==seq+1)
            {
                seq++;
            count++;
            }
           }
        if(max<count)
        max=count;

    }
    cout<<n-max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    bool arr2[100001]={0};
    int arr[100000];
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    int count=0;
    int max=0;
    int seq=1;
    for(int i=0;i<n;i++)
    {
        arr2[arr[i]]=true;
        seq=arr[i];
        count =1;
        for(int j=i;j<n;j++)
           {
            if(arr2[seq+1])
                break;
           if(arr[j]==seq+1)
            {
                seq++;
            count++;
            }
           }
        if(max<count)
        max=count;

    }
    cout<<n-max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int arr[100001];
    int arr3[100001];
    int n;
    cin>>n;
bool sol[100001]={0};

    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        arr3[arr[i]]=i;
    }
    int count=0;
    int max=0;
    int seq=1;
    arr3[n+1]=-1;
    for(int i=0;i<n;i++)
    {
        seq=arr[i];
        count =1;
if(sol[seq]==0)
{
       while(true)
           {
            if(arr3[seq+1]>arr3[seq])
               {

                   count++;
                   seq++;
               }
               else
                break;
           }
for(int x=arr[i];x<arr[i]+count;x++)
sol[x]=1;
}
        if(max<count)
        max=count;

    }
    cout<<n-max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int arr[100001];
    int arr3[100001];
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        arr3[arr[i]]=i;
    }
    int count=0;
    int max=0;
    int seq=1;
    arr3[n+1]=-1;
    for(int i=0;i<n;i++)
    {
        seq=arr[i];
        count =1;
       while(true)
           {
            if(arr3[seq+1]>arr3[seq])
               {

                   count++;
                   seq++;
               }
               else
                break;
           }

        if(max<count)
        max=count;

    }
    cout<<n-max;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int arr2[100001]={0};
    int arr[100000];
    int arr3[100001];
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        arr3[arr[i]]=i;
    }
    int count=0;
    int max=0;
    int min;
    bool flag;
    int seq=1;
    int stop=n;
    for(int i=0;i<n;i++)
    {
        seq=arr[i];
        arr2[seq]=1;
        count =1;
        if(seq!=n)
        {

        for(int j=i;j<n;j++)
           {
            if(arr3[seq+1]>arr3[seq])
               {

                   count++;
                   seq++;
               }
               else
                break;
           }
        }
        if(max<count)
        max=count;

    }
    cout<<n-max;

    return 0;
}


#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <iomanip>
#include <numeric>
#include <memory>
#include <limits.h>
#include <set>
#include <cassert>
#include <cmath>
using namespace std;

typedef long long ll;

#define REP(i,n) for(ll i=0; i<(n); ++i)
#define TEN(x) ((ll)1e##x)
#define ALL(v) (v).begin(), (v).end()
#define INF 100002

int main() {
	ll n;
	cin >> n;
	vector<ll> p(n);
	for (auto&i : p) cin >> i;

	ll max_c = 0;
	REP(i,n) if (p[i] == max_c+1) max_c++;

	ll cur = n;
	for (ll i = n - 1; i >= 0; --i) if(p[i] == cur-1) cur--;

	cout << n - max(max_c, n - cur) << endl;

	return 0;
}


#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <iomanip>
#include <numeric>
#include <memory>
#include <limits.h>
#include <set>
#include <cassert>
#include <cmath>
using namespace std;

typedef long long ll;

#define REP(i,n) for(ll i=0; i<(n); ++i)
#define TEN(x) ((ll)1e##x)
#define ALL(v) (v).begin(), (v).end()
#define INF 10000000

int main() {
	ll n;
	cin >> n;
	vector<ll> p(n);
	for (auto&i : p) cin >> i;

	vector<ll> dp(n);

	fill(ALL(dp), INF);
	REP(i, n) {
		*lower_bound(ALL(dp), p[i]) = p[i];
	}

	cout << n - distance(dp.begin(), lower_bound(ALL(dp), INF)) << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <iomanip>
#include <numeric>
#include <memory>
#include <limits.h>
#include <set>
#include <cassert>
#include <cmath>
using namespace std;

typedef long long ll;

#define REP(i,n) for(ll i=0; i<(n); ++i)
#define TEN(x) ((ll)1e##x)
#define ALL(v) (v).begin(), (v).end()
#define INF 100002

int main() {
	ll n;
	cin >> n;
	vector<ll> p(n);
	for (auto&i : p) cin >> i;

	ll max_c = 0;
	REP(i,n) if (p[i] == max_c+1) max_c++;

	ll cur = n;
	for (ll i = n - 1; i >= 0; --i) if(p[i] == cur-1) cur++;

	cout << n - max(max_c, n - cur) << endl;

	return 0;
}


#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <iomanip>
#include <numeric>
#include <memory>
#include <limits.h>
#include <set>
#include <cassert>
#include <cmath>
using namespace std;

typedef long long ll;

#define REP(i,n) for(ll i=0; i<(n); ++i)
#define TEN(x) ((ll)1e##x)
#define ALL(v) (v).begin(), (v).end()

int main() {
	ll n;
	cin >> n;
	vector<ll> p(n);
	for (auto&i : p) cin >> i;

	vector<ll> dp(n);

	ll max_c = 0;
	REP(i, n) {
		dp[i] = 1;
		REP(j, i) if(p[j] < p[i]) {
			dp[i] = max(dp[i], dp[j] + 1);
		}
		max_c = max(max_c, dp[i]);
	}

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


#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <iomanip>
#include <numeric>
#include <memory>
#include <limits.h>
#include <set>
#include <cassert>
#include <cmath>
using namespace std;

typedef long long ll;

#define REP(i,n) for(ll i=0; i<(n); ++i)
#define TEN(x) ((ll)1e##x)
#define ALL(v) (v).begin(), (v).end()
#define INF 100002

int main() {
	ll n;
	cin >> n;
	vector<ll> p(n);
	for (auto&i : p) cin >> i;

	vector<ll> dp(n, 1);
	vector<int> exist(n, false);
	REP(i, n) {
		exist[p[i]] = true;
		if (p[i] != 1) if (exist[p[i] - 1]) {
			dp[p[i]] = 1 + dp[p[i] - 1];
		}
	}

	ll max_c = 0;
	REP(i, n) {
		max_c = max(max_c, dp[i]);
	}

	cout << n - max_c << endl;

	return 0;
}


#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <iomanip>
#include <numeric>
#include <memory>
#include <limits.h>
#include <set>
#include <cassert>
#include <cmath>
using namespace std;

typedef long long ll;

#define REP(i,n) for(ll i=0; i<(n); ++i)
#define TEN(x) ((ll)1e##x)
#define ALL(v) (v).begin(), (v).end()

int main() {
	ll n;
	cin >> n;
	vector<ll> p(n);
	for (auto&i : p) cin >> i;

	ll max_c = 0;
	ll count = 0;
	REP(i,n){
		if (i == 0 || p[i - 1] < p[i]) count++;
		else {
			max_c = max(max_c, count);
			count = 1;
		}
	}

	std::cout << n - max_c << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <iomanip>
#include <numeric>
#include <memory>
#include <limits.h>
#include <set>
#include <cassert>
#include <cmath>
using namespace std;

typedef long long ll;

#define REP(i,n) for(ll i=0; i<(n); ++i)
#define TEN(x) ((ll)1e##x)
#define ALL(v) (v).begin(), (v).end()
#define INF 100002

int main() {
	ll n;
	cin >> n;
	vector<ll> p(n);
	for (auto&i : p) cin >> i;

	vector<ll> dp(n, 1);
	vector<int> exist(n, false);
	REP(i, n) {
		exist[p[i]-1] = true;
		if (p[i] != 1) if (exist[p[i] - 2]) {
			dp[p[i]-1] = 1 + dp[p[i] - 2];
		}
	}

	ll max_c = 0;
	REP(i, n) max_c = max(max_c, dp[i]);

	cout << n - max_c << endl;

	return 0;
}


#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <iomanip>
#include <numeric>
#include <memory>
#include <limits.h>
#include <set>
#include <cassert>
#include <cmath>
using namespace std;

typedef long long ll;

#define REP(i,n) for(ll i=0; i<(n); ++i)
#define TEN(x) ((ll)1e##x)
#define ALL(v) (v).begin(), (v).end()

int main() {
	ll n;
	cin >> n;
	vector<ll> p(n);
	for (auto&i : p) cin >> i;

	bool in_range = false;
	ll count = 1;
	REP(i,n){
		if (in_range) {
			if (p[i - 1] < p[i]) count++;
			else in_range = false;
		}
		if (p[i] == 1) in_range = true;
	}

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


#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <iomanip>
#include <numeric>
#include <memory>
#include <limits.h>
#include <set>
#include <cassert>
#include <cmath>
using namespace std;

typedef long long ll;

#define REP(i,n) for(ll i=0; i<(n); ++i)
#define TEN(x) ((ll)1e##x)
#define ALL(v) (v).begin(), (v).end()

int main() {
	ll n;
	cin >> n;
	vector<ll> p(n);
	for (auto&i : p) cin >> i;

	ll max_c = 0;
	ll count = 0;
	REP(i,n){
		if (i == 0 || p[i - 1] < p[i]) count++;
		else {
			max_c = max(max_c, count);
			count = 1;
		}
	}

	std::cout << n - max(count,max_c) << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <iomanip>
#include <numeric>
#include <memory>
#include <limits.h>
#include <set>
#include <cassert>
#include <cmath>
using namespace std;

typedef long long ll;

#define REP(i,n) for(ll i=0; i<(n); ++i)
#define TEN(x) ((ll)1e##x)
#define ALL(v) (v).begin(), (v).end()

int main() {
	ll n;
	cin >> n;
	vector<ll> p(n);
	for (auto&i : p) cin >> i;

	ll max_c = 1;
	ll c = 0;
	REP(i,n){
		if (i == 0 || p[i - 1] < p[i]) {
			c++;
		} else {
			max_c = max(max_c, c);
			c = 1;
		}
	}

	std::cout << n - max(max_c, c) << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	scanf("%d",&n);
	int a[n];
	int lis= 1;
	for(int i = 0 ; i < n;i++)
	{
	scanf("%d",&a[i]);
	}
	int cur =  1 ;
	for(int i = 0 ; i < n-1 ;i++)
		{
			if(a[i] <a[i+1] )
				{
					cur++;
					lis = max(lis , cur);
				}
			else
				{
					lis = max(lis , cur );
					cur = 1 ;
				}
		}
	printf("%d\n",lis);
	return 0;
}


#include <iostream>
#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];
    }
    int dp[n];
    int mx=1;
    for(int i=0;i<n;i++){
        int j=i-1;
        while (j>=0 && arr[j]>arr[i]){
            j--;
        }
        if(j<0){
            dp[i]=1;
        }
        else {
            dp[i] = dp[j]+1;
            mx = max(mx,dp[i]);
        }
    }
    int res= n-mx;

c   cput << res << endl;
    return 0;
}


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

int main() {
    int n;
    cin >> n;
    int pos[n+1];
    pos[0]=-1;
    for(int i=0;i<n;i++){
        int temp;
        cin >> temp;
        pos[temp] = i;
    }
    int dp[n+1];
    dp[0]=0;
    int mx=-1;
    for(int i=1;i<=n;i++){
        if(pos[i]>pos[i-1]){
            dp[i] = dp[i-1]+1;
            mx = max(mx,dp[i]);
        }
        else{
            dp[i] = 1;
        }
    }
    cout << n - mx << endl;
    return 0;
}


#include <iostream>
#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];
    }
    int dp[n];
    int mx=1;
    for(int i=0;i<n;i++){
        int j=i-1;
        while (j>=0 && arr[j]>arr[i]){
            j--;
        }
        if(j<0){
            dp[i]=1;
        }
        else {
            dp[i] = dp[j]+1;
            mx = max(mx,dp[i]);
        }
    }
    printf("%d\n",n-mx);
    return 0;
}


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

int main() {
    int n;
    cin>>n;
    int arr[n],arr1[n],ans=0,j=0;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        arr1[i]=arr[i];
    }
    sort(arr,arr+n);
    for(int i=0;i<n;i++)
    {
        if(arr1[i]!=arr[j])
            ans++;
        else
            j++;
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#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];
    }
    int dp[n];
    int mx=1;
    for(int i=0;i<n;i++){
        int j=i-1;
        while (j>=0 && arr[j]>arr[i]){
            j--;
        }
        if(j<0){
            dp[i]=1;
        }
        else {
            dp[i] = dp[j]+1;
            mx = max(mx,dp[i]);
        }
    }
    cout << n-mx << endl;
    return 0;
}


#include<iostream>
#include<map>
using namespace std;
typedef long long l1;
int main()
{
    l1 n,maximum=0,minimum=250306;
    cin>>n;
    map<l1,l1>array;
    for(l1 i=1;i<=n;i++){
        l1 x;
        cin>>x;
        array[x]=i;
        maximum=max(maximum,x);
        minimum=min(minimum,x);
    }
    l1 ans=1;
    l1 count=1;
    l1 t=array[minimum];
    for(l1 j=minimum+1;j<=maximum;j++){
        l1 l=array[j];
        if(l>t){
            count++;
            t=l;
        }
        if(l<t){
            ans=max(ans,count);
            count=1;
        }
    }
    ans=max(ans,count);
    cout<<n-ans<<endl;
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int array[n+1];
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        array[x]=i;
    }
    int ans=1;
    int count=1;
    int t=array[1];
    for(int j=2;j<=n;j++){
        int l=array[j];
        if(l>t){
            count++;
            t=l;
        }
        if(l<t){
            ans=max(ans,count);
            count=1;
        }
    }
    ans=max(ans,count);
    cout<<n-ans<<endl;
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int array[n+1];
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        array[x]=i;
    }
    int ans=0;
    for(int i=1;i<n;i++){
        int count=1;
        int t=array[i];
        for(int j=i+1;j<=n;j++){
            int l=array[j];
            if(l>t){
                count++;
                t=l;
            }
            if(l<t){
                break;
            }
        }
        ans=max(ans,count);
    }
    cout<<n-ans<<endl;
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int array[n+1];
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        array[x]=i;
    }
    int ans=1;
    for(int i=1;i<n;i++){
        int count=1;
        int t=array[i];
        for(int j=i+1;j<=n;j++){
            int l=array[j];
            if(l>t){
                count++;
                t=l;
            }
            if(l<t){
                break;
            }
        }
        ans=max(ans,count);
    }
    cout<<n-ans<<endl;
    return 0;
}


#include<iostream>
#include<map>
using namespace std;
typedef long long l1;
int main()
{
    l1 n,maximum=0,minimum=1000000000;
    cin>>n;
    map<l1,l1>array;
    for(l1 i=1;i<=n;i++){
        l1 x;
        cin>>x;
        array[x]=i;
        maximum=max(maximum,x);
        minimum=min(minimum,x);
    }
    l1 ans=1;
    l1 count=1;
    l1 t=array[minimum];
  //  cout<<t<<endl;
    for(l1 j=minimum+1;j<=maximum;j++){
        l1 l=array[j];
      //  cout<<"l: "<<l<<" t:"<<t<<" j:"<<j<<endl;
        if(l>t){
            count++;
        //    cout<<"count:"<<count<<endl;
            t=l;
        }
        else{
            t=l;
            ans=max(ans,count);
            count=1;
        }
        if(l<t){
            ans=max(ans,count);
            count=1;
        }
    }
   // cout<<count<<" "<<ans<<endl;
    ans=max(ans,count);
    cout<<n-ans<<endl;
    return 0;
}


#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <deque>
#include <queue>
#include <list>
using namespace std;

template <typename T, typename C = vector<T>>
C ReadVector()
{
  string line;
  C res;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  while (stream >> tmp)
    res.push_back(tmp);

  return res;
}

template <typename T>
void Fill(vector<T>* cont, T val)
{
  for (size_t i = 0; i < cont->size(); ++i)
  {
    (*cont)[i] = val;
  }
}

int main(int argc, char** argv)
{
  vector<int> tmp = ReadVector<int>();
  int num = tmp[0];
  vector<int> order = ReadVector<int>();

  vector<int> minValues(num);
  int mm = order.back();
  for (int i = num - 1; i >= 0; --i)
  {
    mm = min(order[i], mm);
    minValues[i] = mm;
  }

  vector<int> moved(num+1);
  Fill(&moved, 0);

  int res = 0;
  for (int i = 0; i < num; ++i)
  {
    int cur = order[i];
    if (cur != minValues[i])
    {
      res += 1;
    }
    continue;

    if (cur == minValues[i] || moved[cur])
    {
      // sorted
      continue;
    }

    // check cost of moving up to end, vs down to start
    int upCount = 1;
    for (int j = cur + 1; j <= num; ++j)
      upCount += moved[j] ? 0 : 1;

    int downCount = 1;
    for (int j = cur - 1; j > 0; --j)
      downCount += moved[j] ? 0 : 1;

    moved[cur] = 1;
    int tmp = 0;

    if (upCount < downCount)
    {
      for (int j = cur + 1; j <= num; ++j)
      {

        if (!moved[j])
        {
          tmp += 1;
          moved[j] = 1;
        }
      }
    }
    else
    {
      for (int j = cur - 1; j > 0; --j)
      {
        if (!moved[j])
        {
          tmp += 1;
          moved[j] = 1;
        }
      }
    }

    res += 1 + tmp;
  }

  printf("%d\n", res);
  return 0;
}


#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <deque>
#include <queue>
#include <list>
using namespace std;

template <typename T, typename C = vector<T>>
C ReadVector()
{
  string line;
  C res;
  T tmp;
  getline(cin, line);
  istringstream stream(line);
  while (stream >> tmp)
    res.push_back(tmp);

  return res;
}

template <typename T>
void Fill(vector<T>* cont, T val)
{
  for (size_t i = 0; i < cont->size(); ++i)
  {
    (*cont)[i] = val;
  }
}

int main(int argc, char** argv)
{
  vector<int> tmp = ReadVector<int>();
  int num = tmp[0];
  vector<int> order = ReadVector<int>();

  vector<int> minValues(num);
  int mm = order.back();
  for (int i = num - 1; i >= 0; --i)
  {
    mm = min(order[i], mm);
    minValues[i] = mm;
  }

  vector<int> moved(num+1);
  Fill(&moved, 0);

  int res = 0;
  for (int i = 0; i < num; ++i)
  {
    int cur = order[i];
    if (cur == minValues[i] || moved[cur])
    {
      // sorted
      continue;
    }

    // check cost of moving up to end, vs down to start
    int upCount = 1;
    for (int j = cur + 1; j <= num; ++j)
      upCount += moved[j] ? 0 : 1;

    int downCount = 1;
    for (int j = cur - 1; j > 0; --j)
      downCount += moved[j] ? 0 : 1;

    moved[cur] = 1;
    int tmp = 0;

    if (upCount < downCount)
    {
      for (int j = cur + 1; j <= num; ++j)
      {
        if (!moved[j])
        {
          tmp += 1;
          moved[j] = 1;
        }
      }
    }
    else
    {
      for (int j = cur - 1; j > 0; --j)
      {
        if (!moved[j])
        {
          tmp += 1;
          moved[j] = 1;
        }
      }
    }

    if (tmp)
      res += 1 + tmp;

  }

  printf("%d\n", res);
  return 0;
}