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

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair < int , pii > piii;
typedef pair<double, int > pdi;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef pair < ll , pll > plll;
typedef pair < plll , pll > pllll;

#define PB push_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'
#define mx 100005
#define mx1 100005
#define mx2 200005
#define mx3 300005
#define WHITE 0
#define RED 1
#define BLACK 2

#define bp __builtin_popcountll
#define fraction() cout.unsetf(ios::floatfield); cout.precision(14); cout.setf(ios::fixed,ios::floatfield);
#define hi cout<<"Dhukse " << endl;
#define infLL (ll)2e12
#define si(x) scanf("%d",&x)
#define sii(x,y) scanf("%d %d",&x,&y)
#define siii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define sl(x) scanf("%lld",&x)
#define sll(x,y) scanf("%lld %lld",&x,&y)
#define slll(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)

const double PI = acos(-1);
const double eps = 1e-12;
const int inf = 2000000000;
ll MOD = 1000000007;
int MOD1 = 1000000007;
int MOD2 = 1000000009;
#define harmonic(n) 0.57721566490153286l(Eulers-Mascharoni constant)+log(n) ( 1/1 + 1/2 + .... + 1/n );

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a) * (a))
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, ll i) { return n&(1LL<<i); }
inline ll setBit(ll n, ll i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, ll i) { return n&(~(1LL<<i)); }
#define bp __builtin_popcount

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
//int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
//int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1LL; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1LL; } return r; }
inline ll modDiv(ll a, ll b) { return modMul(a, modPow(b, MOD-2)); }

bool comp( const pair < ll , pll > &p1 , const pair < ll , pll > &p2 ){ return p1.F > p2.F ;}
bool comp1( const pll &p1 , const pll &p2 ){
          if( p1.F == p2.F ){
                 return p1.S > p2.S;
          }
          return p1.F < p2.F ;
}
ll converter( string a ){
    ll i , mul = 1 , r , t ,ans = 0LL;if( a.length() == 0 )return 0;for( i = a.length() - 1 ; i >= 0 ; i-- ){
        t = a[i] - '0';r = t%10;ans += (mul*r);mul = mul*10;
    }
    return ans;
}
int msb(unsigned x) {
	union { double a; int b[2]; };
	a = x;
	return (b[1] >> 20) - 1023;
}

//
//debug
#ifdef RIP
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
            return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
            os << "{";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const multiset< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << it -> first << " = " << it -> second ;
                                            }
                    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

void faltu () {
            cerr << endl;
}

template <typename T>
void faltu( T a[], int n ) {
            for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
                cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) {
            cerr << arg << ' ';
                faltu(rest...);
}
#else
#define dbg(args...)
#endif  //RIP
const int MAX = 5005;
int dp[MAX][MAX] , a[MAX] , n , m , i;

int solve( int idx , int lst ){
       if( idx > n ) return 0;
       if( dp[idx][lst]!= -1 ) return dp[idx][lst];
       int ret1 = inf , ret2 = inf;
       if( a[idx] >= lst ) ret1 = solve( idx + 1 , a[idx]  );
       ret2 = 1 + solve( idx + 1 , lst );
       return dp[idx][lst] = min( ret1 , ret2 );
}

int main() {
    optimize();
    //fraction();
    //freopen("input.txt","r",stdin);

    double x;

    cin >> n >> m;
    mem( dp, -1 );
    for( i = 1 ; i <= n; ++i ){
               cin >> a[i] >> x;
    }
    cout << solve( 1 , 0 );

    return 0;
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author ocelopilli
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskD {
    int[] s = new int[ 5005 ];
    double[] x = new double[ 5005 ];
    int[][] dp = new int[5005][5005];
    boolean[][] done = new boolean[5005][5005];

    int run(int n, int m)
    {
        if ( done[n][m] ) return dp[n][m];
        done[n][m] = true;
        if ( n == 0 || m == 0 ) return dp[n][m] = 0;
        if ( s[n] > m ) return dp[n][m] = run( n-1, m );
        return dp[n][m] = Math.max( run( n-1, m ), 1 + run( n-1, s[n] ) );
    }

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int m = in.nextInt();

        for (int i=1; i<=n; i++)
        {
            s[i] = in.nextInt();
            x[i] = in.nextDouble();
        }

        out.println( n - run( n, m ) );
    }
}

class InputReader {
    BufferedReader br;
    StringTokenizer st;

    public InputReader(InputStream in)
    {
        br = new BufferedReader(new InputStreamReader(in));
        st = null;
    }

    public String next()
    {
        while (st==null || !st.hasMoreTokens())
        {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return st.nextToken();
    }

    public int nextInt()
    {
        return Integer.parseInt(next());
    }

    public double nextDouble()
    {
        return Double.parseDouble(next());
    }

    }


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class D2 {
      static int []a;
      static int dp[][];
    public static void main(String[] args) throws IOException {
        int n=in();
        in();
        a=new int [n];
        dp=new int [n+1][n+1];
        for (int i=0;i<n;i++){
            a[i]=in();
            dub();
        }
        System.out.println(n-solve(0,-1));

    }
    static int solve (int i,int j){
        if (i>=a.length)return 0;
        if (j!=-1 && dp[i][j]!=0) return dp[i][j];
        if ( j==-1||a[i]>=a[j]){
            int c1=1+solve(i+1,i);
            int c2=solve(i+1,j);
            if (j!=-1)
            return dp[i][j]=Math.max(c1,c2);
            else
                return Math.max(c1,c2);
        }
        int c1=solve(i+1,j);
        return dp[i][j]=c1;
    }
    static BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static int in() throws IOException{
        if (st==null || !st.hasMoreTokens()){
            st=new StringTokenizer(buf.readLine());
        }
        return Integer.parseInt(st.nextToken());
    }
    static long ll() throws IOException{
        if (st==null || !st.hasMoreTokens()){
            st=new StringTokenizer(buf.readLine());
        }
        return Long.parseLong(st.nextToken());
    }
    static String str() throws IOException{
        if (st==null || !st.hasMoreTokens()){
            st=new StringTokenizer(buf.readLine());
        }
        return (st.nextToken());
    }
        static double dub() throws IOException{
            if (st==null || !st.hasMoreTokens()){
                st=new StringTokenizer(buf.readLine());
            }
            return Double.parseDouble(st.nextToken());
    }
}


import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

/**
 * User: Oleksiy Pylypenko
 * Date: 1/6/13
 * Time: 1:46 PM
 */
public class D {
    static boolean debug = !"true".equalsIgnoreCase(System.getProperty("ONLINE_JUDGE"));
    private Scanner in;
    private PrintWriter out;

    private void solve() throws IOException {
        int n = in.nextInt();
        int m = in.nextInt();

        int cats[] = new int[m];
        int []order = new int[n];
        int []ro = new int[n];
        for (int i = 0; i < n; i++) {
            order[i] = in.nextInt() - 1;
            cats[order[i]]++;
            in.nextDouble();
        }
        int k = 0;
        for (int i = 0; i < n; i++) {
            while (cats[k] == 0) {
                k++;
            }
            cats[k]--;
            ro[i] = k;
        }
        int [][]r = new int[n + 1][n + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (ro[i-1] == order[j-1]) {
                    r[i][j] = r[i-1][j-1] + 1;
                } else {
                    r[i][j] = Math.max(r[i-1][j], r[i][j-1]);
                }
            }
        }
        out.println(n - r[n][n]);
    }

    public static void main(String[] args) throws IOException {
        new D().exec();
    }

    private void exec() {
        if (debug) {
            try {
                System.setIn(new FileInputStream(getClass().getName() + ".in"));
            } catch (FileNotFoundException e) {
                System.out.println(e);
                System.exit(1);
            }
        }
        in = new Scanner(new InputStreamReader(System.in));
        out = new PrintWriter(System.out, true);

        try {
            if (debug) {
                while (in.hasNext()) {
                    solve();
                }
            } else {
                solve();
            }
        } catch (IOException e) {
            if (debug) {
                System.out.println(e);
                System.exit(1);
            }
        } finally {
            out.close();
            in.close();
        }
    }

    static class Brute {
        public static final int N_SIDES = 4;
        private final int n;

        public Brute(int n) {
            this.n = n;
        }

        public static void main(String[] args) {
            Brute brute = new Brute(2);
            System.out.println(brute.drillDown(brute.checker(2)));
        }

        private long drillDown(Checker checker) {
            int []steps = new int[n];
            return rec(steps, n, checker);
        }

        private long rec(int[] steps, int n, Checker checker) {
            if (n == 0) {
                return checker.check(steps) ? 1 : 0;
            }
            long ret = 0;
            for (int i = 0; i < N_SIDES; i++) {
                steps[n - 1] = i;
                ret += rec(steps, n - 1, checker);
            }
            return ret;
        }

        private Checker checker(int h) {
            return new Checker(h);
        }

        private class Checker {
            private final int h;

            public Checker(int h) {
                this.h = h;
            }

            public boolean check(int[] steps) {
                nextSide: for (int d = 0; d < N_SIDES; d++) {
                    int ld = 0;
                    for (int i = 1; i <= n; i++) {
                        if (steps[i - 1] == d) {
                            if (i - ld > h) {
                                continue nextSide;
                            } else {
                                ld = i;
                            }
                        }
                        if (ld >= n - h + 1) {
                            return true;
                        }
                    }
                }
                return false;
            }
        }
    }
}


import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

/**
 * User: Oleksiy Pylypenko
 * Date: 1/6/13
 * Time: 1:46 PM
 */
public class D {
    static boolean debug = !"true".equalsIgnoreCase(System.getProperty("ONLINE_JUDGE"));
    private Scanner in;
    private PrintWriter out;

    private void solve() throws IOException {
        int n = in.nextInt();
        int m = in.nextInt();

        int cats[] = new int[m];
        int []order = new int[n];
        int []ro = new int[n];
        for (int i = 0; i < n; i++) {
            order[i] = in.nextInt() - 1;
            cats[order[i]]++;
            in.nextDouble();
        }
        int k = 0;
        for (int i = 0; i < n; i++) {
            while (cats[k] == 0) {
                k++;
            }
            cats[k]--;
            ro[i] = k;
        }
        int [][]r = new int[n + 1][n + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (ro[i-1] == order[j-1]) {
                    r[i][j] = r[i - 1][j - 1];
                } else {
                    r[i][j] = Math.min(r[i-1][j], r[j][i -1]) + 1;
                }
            }
        }
        out.println(r[n][n]);
    }

    public static void main(String[] args) throws IOException {
        new D().exec();
    }

    private void exec() {
        if (debug) {
            try {
                System.setIn(new FileInputStream(getClass().getName() + ".in"));
            } catch (FileNotFoundException e) {
                System.out.println(e);
                System.exit(1);
            }
        }
        in = new Scanner(new InputStreamReader(System.in));
        out = new PrintWriter(System.out, true);

        try {
            if (debug) {
                while (in.hasNext()) {
                    solve();
                }
            } else {
                solve();
            }
        } catch (IOException e) {
            if (debug) {
                System.out.println(e);
                System.exit(1);
            }
        } finally {
            out.close();
            in.close();
        }
    }

    static class Brute {
        public static final int N_SIDES = 4;
        private final int n;

        public Brute(int n) {
            this.n = n;
        }

        public static void main(String[] args) {
            Brute brute = new Brute(2);
            System.out.println(brute.drillDown(brute.checker(2)));
        }

        private long drillDown(Checker checker) {
            int []steps = new int[n];
            return rec(steps, n, checker);
        }

        private long rec(int[] steps, int n, Checker checker) {
            if (n == 0) {
                return checker.check(steps) ? 1 : 0;
            }
            long ret = 0;
            for (int i = 0; i < N_SIDES; i++) {
                steps[n - 1] = i;
                ret += rec(steps, n - 1, checker);
            }
            return ret;
        }

        private Checker checker(int h) {
            return new Checker(h);
        }

        private class Checker {
            private final int h;

            public Checker(int h) {
                this.h = h;
            }

            public boolean check(int[] steps) {
                nextSide: for (int d = 0; d < N_SIDES; d++) {
                    int ld = 0;
                    for (int i = 1; i <= n; i++) {
                        if (steps[i - 1] == d) {
                            if (i - ld > h) {
                                continue nextSide;
                            } else {
                                ld = i;
                            }
                        }
                        if (ld >= n - h + 1) {
                            return true;
                        }
                    }
                }
                return false;
            }
        }
    }
}


import java.util.*;

public class D {
	public static void main(String [] args){
		try (Scanner s = new Scanner(System.in)){
			final int n = s.nextInt();
			final int m = s.nextInt();
			final int [] x = new int[n];
			for (int i = 0; i < n; ++i){
				x[i] = s.nextInt();
				s.nextDouble();
			}
			final int [] dp = new int[n];
			dp[0] = 1;
			int max = 1;
			for (int i = 1; i < n; ++i){
				dp[i] = 1;
				for (int j = 0; j < i; ++j){
					if (x[i] >= x[j]){
						dp[i] = Math.max(dp[i], dp[j] + 1);
					}
				}
				max = Math.max(max, dp[i]);
			}
			System.out.println(n - max);
		}
	}
}


import java.io.*;
import java.util.StringTokenizer;

public class D
{
	public static void main(String [] args) throws IOException
	{
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

		StringTokenizer st = new StringTokenizer(reader.readLine());
		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());

		Flower [] flowers = new Flower[n];

		for(int i = 0 ; i < n ; i++)
		{
			st = new StringTokenizer(reader.readLine());
			int type = Integer.parseInt(st.nextToken());
			double pos = Double.parseDouble(st.nextToken());

			flowers[i] = new Flower(type, pos);
		}

		int [] dp = new int[n];
		int res = 0;
		for(int i = 0 ; i < n ; i++)
		{
			dp[i] = 1;
			for(int j = 0 ; j < i ; j++)
				if(flowers[i].type >= flowers[j].type)
					dp[i] = Math.max(dp[i], dp[j]+1);

			res = Math.max(res, dp[i]);

		}

		System.out.println(n-res);
	}

	static class Flower
	{
		int type;
		double pos;

		public Flower(int type, double pos)
		{
			this.type = type;
			this.pos = pos;
		}
	}
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Lokesh Khandelwal
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskD {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
        int n=in.nextInt(),m=in.nextInt();
        int k[]=new int[n],i;
        double x[]=new double[n];
        for(i=0;i<n;i++)
        {
            k[i]=in.nextInt();
            x[i]=in.nextDouble();
        }
        int j;
        int z[]=new int[n],ans=1;
        Arrays.fill(z,1);
        for(i=0;i<n;i++)
        {
            for(j=0;j<i;j++)
            {
                if(k[i]>=k[j]&&z[i]<z[j]+1)
                    z[i]=z[j]+1;
            }
            ans=Math.max(z[i],ans);
        }
        //int ans=DynamicProgramming.LongestIncreasingSubsequence(k);
        out.printLine(n-ans);
    }
}

class InputReader
{
    BufferedReader in;
    StringTokenizer tokenizer=null;

    public InputReader(InputStream inputStream)
    {
        in=new BufferedReader(new InputStreamReader(inputStream));
    }
    public String next()
    {
        try{
            while (tokenizer==null||!tokenizer.hasMoreTokens())
            {
                tokenizer=new StringTokenizer(in.readLine());
            }
            return tokenizer.nextToken();
        }
        catch (IOException e)
        {
            return null;
        }
    }
    public int nextInt()
    {
        return Integer.parseInt(next());
    }
    public double nextDouble()
    {
        return Double.parseDouble(next());
    }
    }

class OutputWriter {
	private final PrintWriter writer;

	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
	}

	public OutputWriter(Writer writer) {
		this.writer = new PrintWriter(writer);
	}

	public void print(Object...objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(Object...objects) {
		print(objects);
		writer.println();
	}

	public void close() {
		writer.close();
	}
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Lokesh Khandelwal
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskD {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
        int n=in.nextInt(),m=in.nextInt();
        int k[]=new int[n],i;
        double x[]=new double[n];
        for(i=0;i<n;i++)
        {
            k[i]=in.nextInt();
            x[i]=in.nextDouble();
        }
        int j;
        int z[]=new int[n],ans=1;
        Arrays.fill(z,1);
        for(i=0;i<n;i++)
        {
            for(j=0;j<i;j++)
            {
                if(k[i]>=k[j]&&z[i]<z[j]+1)
                    z[i]=z[j]+1;
            }
            ans=Math.max(z[i],ans);
        }
        //int ans=DynamicProgramming.LongestIncreasingSubsequence(k);
        out.printLine(Math.min(ans,n-ans));
    }
}

class InputReader
{
    BufferedReader in;
    StringTokenizer tokenizer=null;

    public InputReader(InputStream inputStream)
    {
        in=new BufferedReader(new InputStreamReader(inputStream));
    }
    public String next()
    {
        try{
            while (tokenizer==null||!tokenizer.hasMoreTokens())
            {
                tokenizer=new StringTokenizer(in.readLine());
            }
            return tokenizer.nextToken();
        }
        catch (IOException e)
        {
            return null;
        }
    }
    public int nextInt()
    {
        return Integer.parseInt(next());
    }
    public double nextDouble()
    {
        return Double.parseDouble(next());
    }
    }

class OutputWriter {
	private final PrintWriter writer;

	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
	}

	public OutputWriter(Writer writer) {
		this.writer = new PrintWriter(writer);
	}

	public void print(Object...objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(Object...objects) {
		print(objects);
		writer.println();
	}

	public void close() {
		writer.close();
	}
}


// @author Sanzhar
import java.awt.Point;
import java.io.*;
import java.util.*;

public class Template {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer st;

    String next() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(in.readLine());
            } catch (Exception e) {
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    public void run() throws Exception {
        //in = new BufferedReader(new FileReader("input.txt"));
        //out = new PrintWriter(new FileWriter("output.txt"));
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.flush();
        out.close();
        in.close();
    }

    public void solve() throws Exception {
        int n = nextInt();
        int m = nextInt();
        int[] r = new int[n];
        for (int i = 0; i < n; i++) {
            r[i] = nextInt();
            next();
        }
        int[] dp = new int[n];
        int ans = 0;
        for (int i = 0; i < n; i++) {
            int max = 0;
            for (int j = 0; j < i; j++) {
                if (r[j] <= r[i]) {
                    max = Math.max(max, dp[j]);
                }
            }
            dp[i] = max + 1;
            ans = Math.max(ans, dp[i]);
        }
        out.println(n - ans);
    }

    public static void main(String[] args) throws Exception {
        new Template().run();
    }
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class C165P4 {

    private static BufferedReader in;
    private static PrintWriter out;
    private static StringTokenizer input;

    public static void main(String[] args) throws IOException {
        //-------------------------------------------------------------------------
        //Initializing...
        new C165P4();
        //-------------------------------------------------------------------------
        //put your code here... :)
        int n = nextInt();
        int m = nextInt();

        int[] p = new int[n];
        for (int i = 0; i < p.length; i++) {
            p[i] = nextInt();
            nextDouble();
        }

        int [] dp = new int[n];
        dp[0] = 1;
        for( int i = 1; i < dp.length; i++ ) {
           dp[i] = 1;
           for( int j = 0; j < i; j++ ) {
              if( p[i] >= p[j] ) {
                 if( dp[i] < dp[j]+1 ) {
                    dp[i] = dp[j]+1;
                 }
              }
           }
        }
        writeln(""+(n-dp[dp.length-1]));

        //-------------------------------------------------------------------------
        //closing up
        end();
        //--------------------------------------------------------------------------

    }

    public C165P4() throws IOException {
        //Input Output for Console to be used for trying the test samples of the problem
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        //-------------------------------------------------------------------------
        //Initalize Stringtokenizer input
        input = new StringTokenizer(in.readLine());
    }

    private static int nextInt() throws IOException {
        if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
        return Integer.parseInt(input.nextToken());
    }
    private static long nextLong() throws IOException {
        if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
        return Long.parseLong(input.nextToken());
    }
    private static double nextDouble() throws IOException {
        if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
        return Double.parseDouble(input.nextToken());
    }
    private static String nextString() throws IOException {
        if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
        return input.nextToken();
    }
    private static void write(String output){
        out.write(output);
        out.flush();
    }
    private static void writeln(String output){
        out.write(output+"\n");
        out.flush();
    }
    private static void end() throws IOException{
        in.close();
        out.close();
        System.exit(0);
    }

}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class C165P4 {

    private static BufferedReader in;
    private static PrintWriter out;
    private static StringTokenizer input;

    public static void main(String[] args) throws IOException {
        //-------------------------------------------------------------------------
        //Initializing...
        new C165P4();
        //-------------------------------------------------------------------------
        //put your code here... :)
        int n = nextInt();
        int m = nextInt();

        int[] p = new int[n];
        for (int i = 0; i < p.length; i++) {
            p[i] = nextInt();
            nextDouble();
        }

        int [] dp = new int[n];
        dp[0] = 1;
        for( int i = 1; i < dp.length; i++ ) {
           dp[i] = 1;
           for( int j = 0; j < i; j++ ) {
              if( p[i] >= p[j] ) {
                 if( dp[i] < dp[j]+1 ) {
                    dp[i] = dp[j]+1;
                 }
              }
           }
        }
        int max = -1;
        for (int i = 0; i < dp.length; i++) {
            max =Math.max(max, dp[i]);
        }
        writeln(""+(n-max));

        //-------------------------------------------------------------------------
        //closing up
        end();
        //--------------------------------------------------------------------------

    }

    public C165P4() throws IOException {
        //Input Output for Console to be used for trying the test samples of the problem
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        //-------------------------------------------------------------------------
        //Initalize Stringtokenizer input
        input = new StringTokenizer(in.readLine());
    }

    private static int nextInt() throws IOException {
        if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
        return Integer.parseInt(input.nextToken());
    }
    private static long nextLong() throws IOException {
        if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
        return Long.parseLong(input.nextToken());
    }
    private static double nextDouble() throws IOException {
        if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
        return Double.parseDouble(input.nextToken());
    }
    private static String nextString() throws IOException {
        if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
        return input.nextToken();
    }
    private static void write(String output){
        out.write(output);
        out.flush();
    }
    private static void writeln(String output){
        out.write(output+"\n");
        out.flush();
    }
    private static void end() throws IOException{
        in.close();
        out.close();
        System.exit(0);
    }

}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class C165P4 {

    private static BufferedReader in;
    private static PrintWriter out;
    private static StringTokenizer input;

    public static void main(String[] args) throws IOException {
        //-------------------------------------------------------------------------
        //Initializing...
        new C165P4();
        //-------------------------------------------------------------------------
        //put your code here... :)
        int n = nextInt();
        int m = nextInt();

        int[] p = new int[n];
        for (int i = 0; i < p.length; i++) {
            p[i] = nextInt();
            nextDouble();
        }
        int ans = 0;
        int solve = 0;
        for (int i = 1; i <= m; i++) {
            boolean move = false;
            int is =0;
            int xs = 0;
            int last = 0;
            for (int j = solve; j < p.length; j++) {
                if (move && p[j] == i) {is++;xs += last; last = 0;}
                if (p[j] > i){move = true; last++;}
            }
            if (is > xs){solve = p.length-last;}
            ans += Math.min(is, xs);
        }

        writeln(""+ans);
        //-------------------------------------------------------------------------
        //closing up
        end();
        //--------------------------------------------------------------------------

    }

    public C165P4() throws IOException {
        //Input Output for Console to be used for trying the test samples of the problem
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        //-------------------------------------------------------------------------
        //Initalize Stringtokenizer input
        input = new StringTokenizer(in.readLine());
    }

    private static int nextInt() throws IOException {
        if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
        return Integer.parseInt(input.nextToken());
    }
    private static long nextLong() throws IOException {
        if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
        return Long.parseLong(input.nextToken());
    }
    private static double nextDouble() throws IOException {
        if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
        return Double.parseDouble(input.nextToken());
    }
    private static String nextString() throws IOException {
        if (!input.hasMoreTokens())input=new StringTokenizer(in.readLine());
        return input.nextToken();
    }
    private static void write(String output){
        out.write(output);
        out.flush();
    }
    private static void writeln(String output){
        out.write(output+"\n");
        out.flush();
    }
    private static void end() throws IOException{
        in.close();
        out.close();
        System.exit(0);
    }

}


import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		Round165DivProb4 r = new Round165DivProb4();

		String a = sc.nextLine();
		String[] temp = a.split(" ");
		int[] side  = new int[Integer.parseInt(temp[0])];

		int i=0;
		for(i=0;i<side.length;i++) {
			String b = sc.nextLine();
			temp = b.split(" ");
			side[i]=Integer.parseInt(temp[0]);
		}

		System.out.println(r.getNumPlantsToChangePlace(side));

	}

}

class Round165DivProb4{

	public int getNumPlantsToChangePlace(int[] x) {

		int[] dp = new int[x.length];
		int i=0,j=0;;
		for(i=0;i<dp.length;i++)dp[i]=1;
		int max = 1;

		for(i=1;i<dp.length;i++) {
			for(j=0;j<i;j++) {
				if(x[i]>=x[j]) {
					dp[i]=Math.max(dp[i],dp[j]+1);
				}
			}

			if(max < dp[i]) {
				max = dp[i];
			}
		}

		return x.length-max;

	}

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Igors
 */
public class CF165D {
    public static void main(String[] args){
        Scanner s= new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int n=s.nextInt();
        long m=s.nextLong(),res=0;
        s.useLocale(Locale.ENGLISH);
        long[] nums=new long[n];
        long[] d=new long[n];
        double m2;
        for(int i=0;i<n;i++){
            nums[i]=s.nextLong();
            m2=s.nextDouble();
        }
        for(int i=0;i<n;i++){
            d[i]=1;
            for(int j=0;j<i;j++){
                if(nums[j]<=nums[i]){
                    d[i]=Math.max(d[i],d[j]+1);
                }
            }
        }
        res=d[0];
        for(int i=0;i<n;i++){
            res=res<d[i] ? d[i]:res;
        }
        System.out.println(n-res);
        s.close();
    }
}


import java.util.*;
import java.io.*;

public class FancyFence {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] a = new int[n];
        int c = 0;
        while (sc.hasNextInt()){
            int s = sc.nextInt();
            float x = sc.nextFloat();
            a[c] = s;
            c++;
        }
        System.out.println(getR(a));
    }
    static int getR(int[] a){
        int s = a.length-1;
        int[] b = Arrays.copyOfRange(a,s,a.length);
        int r = 0;
        while (b.length!=a.length){
            s--;
            int f = a[s];
            Arrays.sort(b);
            if (f>b[0])r++;
            b = Arrays.copyOfRange(a,s,a.length);
        }
        return r;
    }
}


import java.util.*;
import java.io.*;

public class FancyFence {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] a = new int[n];
        int c = 0;
        while (sc.hasNextInt()){
            int s = sc.nextInt();
            float x = sc.nextFloat();
            a[c] = s;
            c++;
        }
        int[] b = Arrays.copyOf(a, a.length);
        Arrays.sort(b);
        System.out.println(a.length-getR(a, b));
    }
    static int getR(int[] a, int[] b){
      if (a.length == 0 || b.length == 0) return 0;
      if (a[a.length-1]==b[b.length-1])
        return 1+getR(Arrays.copyOf(a,a.length-1),Arrays.copyOf(b,b.length-1));
      else {
        int c = getR(Arrays.copyOf(a,a.length-1),b);
        int d = getR(a,Arrays.copyOf(b,b.length-1));
        return Math.max(c, d);
      }
    }
}


import java.util.*;
import java.io.*;

public class FancyFence {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] a = new int[n];
        int c = 0;
        while (sc.hasNextInt()){
            int s = sc.nextInt();
            float x = sc.nextFloat();
            a[c] = s;
            c++;
        }
        int[] b = Arrays.copyOf(a, a.length);
        Arrays.sort(b);
        System.out.println(a.length-getR(a, b));
    }
    static int getR(int[] a, int[] b){
      int n = a.length;
      int m = b.length;
      int[][] s = new int[n+1][m+1];
      s[0][0]=0;s[0][1]=0;s[1][0]=0;
      for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++) {
          if (a[i-1]==b[j-1]) s[i][j]=1+s[i-1][j-1];
          else s[i][j]=Math.max(s[i-1][j],s[i][j-1]);
        }
      }
      return s[n][m];
    }
}


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.InputMismatchException;

public class CFFF {
	static PrintWriter out;

	public static void main(String[] args) {
		MScanner sc = new MScanner();
		out = new PrintWriter(System.out);

		int N = sc.nextInt();
		int M = sc.nextInt();
		Pair[] array=  new Pair[N];
		for(int a=0;a<N;a++)
			array[a]=new Pair(sc.nextInt(),sc.nextDouble());

		Arrays.sort(array);
		int[] temp= new int[N];
		for(int a=0;a<N;a++)temp[a]=array[a].m;
		int ans = longestIncreasingSubsequence(temp);
		out.println(N-ans);
		out.close();
	}

	static int longestIncreasingSubsequence(int[] x) {
		int[] z = new int[x.length];
		Arrays.fill(z, Integer.MAX_VALUE);
		z[0] = x[0];
		for (int i = 1; i < x.length; i++)
			z[binSearch(z, x[i])] = x[i];
		return binSearch(z, Integer.MAX_VALUE / 2);
	}

	static int binSearch(int[] x, int val) {
		int l = 0, r = x.length - 1, ret = x.length;
		while (l <= r) {
			int m = (l + r) / 2;
			if (x[m] > val)
				r = (ret = m) - 1;
			else
				l = m + 1;
		}
		return ret;
	}
	static class Pair implements Comparable<Pair>{
		int m;
		double p;
		Pair(int a, double b){
			m=a;
			p=b;
		}
		@Override
		public int compareTo(Pair o){
			return Double.compare(this.p, o.p);
		}
	}

	static class MScanner {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public MScanner() {
			stream = System.in;
			// stream = new FileInputStream(new File("dec.in"));

		}

		int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		int[] nextInt(int N) {
			int[] ret = new int[N];
			for (int a = 0; a < N; a++)
				ret[a] = nextInt();
			return ret;
		}

		int[][] nextInt(int N, int M) {
			int[][] ret = new int[N][M];
			for (int a = 0; a < N; a++)
				ret[a] = nextInt(M);
			return ret;
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		long[] nextLong(int N) {
			long[] ret = new long[N];
			for (int a = 0; a < N; a++)
				ret[a] = nextLong();
			return ret;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		double[] nextDouble(int N) {
			double[] ret = new double[N];
			for (int a = 0; a < N; a++)
				ret[a] = nextDouble();
			return ret;
		}

		String next() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		String[] next(int N) {
			String[] ret = new String[N];
			for (int a = 0; a < N; a++)
				ret[a] = next();
			return ret;
		}

		String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}

		String[] nextLine(int N) {
			String[] ret = new String[N];
			for (int a = 0; a < N; a++)
				ret[a] = nextLine();
			return ret;
		}

	}
}


import java.util.*;
import java.io.*;

public class D {
	String s = null;

	int[] d = null;
	int n = 0;
	public void run() throws Exception{

		BufferedReader br = null;
		File file = new File("input.txt");
		if(file.exists()){
			br = new BufferedReader(new FileReader("input.txt"));
		}
		else{
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		s = br.readLine();

		String[] ss = s.split(" ");
		n = Integer.parseInt(ss[0]);
		int m = Integer.parseInt(ss[1]);
		d = new int[n];
		List<Integer> list = new ArrayList<Integer>();
		for(int i = 0; i < n; i++){
			s = br.readLine();
			ss = s.split(" ");
			d[i] = Integer.parseInt(ss[0]);
			if(i == 0){
				list.add(d[i]);
			}
			if(i > 0 && d[i] != d[i-1] ){
				list.add(d[i]);
			}
		}
		n = list.size();
		d = new int[n];
		for(int i = 0; i < list.size(); i++){
			d[i] = list.get(i);
		}

		int[] ret = check(0);
		int[] ret2 = check2(n - 1);
		int ans = Math.min(ret[0], ret2[0]);

		System.out.println(ans);
		br.close();
	}

	int[] check(int pos){
		if(pos == n-1){
			int[] ret = new int[2];
			ret[0] = 0;
			ret[1] = d[n-1];
			return ret;
		}
		int num = d[pos];
		int[] ret = check(pos + 1);
		if(ret[1] < num){
			ret[0]++;
		}
		else{
			ret[1] = num;
		}
		return ret;
	}

	int[] check2(int pos){
		if(pos == 0){
			int[] ret = new int[2];
			ret[0] = 0;
			ret[1] = d[0];
			return ret;
		}
		int num = d[pos];
		int[] ret = check2(pos -1);
		if(ret[1] > num){
			ret[0]++;
		}
		else{
			ret[1] = num;
		}
		return ret;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		D t = new D();
		t.run();

	}

}


import java.util.*;
import java.io.*;

public class D2 {
	String s = null;

	int[] d = null;
	int n = 0;
	int[][] dp = null;
	public void run() throws Exception{

		BufferedReader br = null;
		File file = new File("input.txt");
		if(file.exists()){
			br = new BufferedReader(new FileReader("input.txt"));
		}
		else{
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		s = br.readLine();

		String[] ss = s.split(" ");
		n = Integer.parseInt(ss[0]);
		int m = Integer.parseInt(ss[1]);
		d = new int[n];

		for(int i = 0; i < n; i++){
			s = br.readLine();
			ss = s.split(" ");
			d[i] = Integer.parseInt(ss[0]);

		}
		dp = new int[n][m+1];

		for(int i = 0; i < n; i++){
			for(int j = 0; j < m+1; j++){
				dp[i][j] = -1;
			}
		}

		int ans = check(0, 0);
		System.out.println(ans);
		br.close();
	}

	int check(int max, int pos){
		if(dp[pos][max] != -1){
			return dp[pos][max];
		}

		if(pos == n-1){
			if(d[pos] < max){
				return 1;
			}
			else{
				return 0;
			}
		}
		int ret = 0;
		if(max == d[pos]){
			ret =  check(max, pos+1);
		}
		else if(max < d[pos]){
			ret =  Math.min(check(max, pos + 1) + 1, check(d[pos], pos + 1));
		}
		else{
			ret = check(max, pos + 1) + 1;
		}
		dp[pos][max] = ret;

		return ret;
	}

	int[] check2(int pos){
		if(pos == 0){
			int[] ret = new int[2];
			ret[0] = 0;
			ret[1] = d[0];
			return ret;
		}
		int num = d[pos];
		int[] ret = check2(pos -1);
		if(ret[1] > num){
			ret[0]++;
		}
		else{
			ret[1] = num;
		}
		return ret;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		D2 t = new D2();
		t.run();

	}

}


import java.util.*;
import java.io.*;

public class D {
	String s = null;

	int[] d = null;
	int n = 0;
	public void run() throws Exception{

		BufferedReader br = null;
		File file = new File("input.txt");
		if(file.exists()){
			br = new BufferedReader(new FileReader("input.txt"));
		}
		else{
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		s = br.readLine();

		String[] ss = s.split(" ");
		n = Integer.parseInt(ss[0]);
		int m = Integer.parseInt(ss[1]);
		d = new int[n];
		for(int i = 0; i < n; i++){
			s = br.readLine();
			ss = s.split(" ");
			d[i] = Integer.parseInt(ss[0]);
		}

		int[] ret = check(0);

		System.out.println(ret[0]);
		br.close();
	}

	int[] check(int pos){
		if(pos == n-1){
			int[] ret = new int[2];
			ret[0] = 0;
			ret[1] = d[n-1];
			return ret;
		}
		int num = d[pos];
		int[] ret = check(pos + 1);
		if(ret[1] < num){
			ret[0]++;
		}
		else{
			ret[1] = num;
		}
		return ret;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		D t = new D();
		t.run();

	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Round165_D {

	int N, M;
	ob[] a;

	public void solve() throws Exception {

		InputReader in = new InputReader();

		N = in.nextInt();
		M = in.nextInt();

		a = new ob[N];
		for (int i = 0; i < N; i++) {
			int z = in.nextInt() - 1;
			double x = Double.parseDouble(in.next());
			a[i] = new ob(x, z);
		}
		Arrays.sort(a);

		int[][] dp = new int[N + 1][M + 1];
		for(int i =0 ; i < N;i++)
			dp[i][M] = 1<<25;

		for (int i = N - 1; i > -1; i--)
			for (int j = M - 1; j > -1; j--) {
				int ans = 0;
				if (a[i].type != j)
					ans++;
				dp[i][j] = ans + Math.min(dp[i + 1][j], dp[i + 1][j + 1]);
			}
//		for(int i =0 ; i < N+1;i++)
//			System.out.println(Arrays.toString(dp[i]));
		int ans = 1 << 25;
		for (int i = 0; i < M; i++)
			ans = Math.min(dp[0][i], ans);
		System.out.println(ans);

	}

	static class ob implements Comparable<ob> {
		int type;
		double pos;

		public ob(double pos, int type) {
			this.type = type;
			this.pos = pos;
		}

		public int compareTo(ob o) {
			return new Double(pos).compareTo(o.pos);
		}
	}

	public static void main(String[] args) throws Exception {
		new Round165_D().solve();
	}

	static class InputReader {
		BufferedReader in;
		StringTokenizer st;

		public InputReader() throws IOException {
			in = new BufferedReader(new InputStreamReader(System.in));
			st = new StringTokenizer(in.readLine());

		}

		public String next() throws IOException {

			while (!st.hasMoreElements())
				st = new StringTokenizer(in.readLine());
			return st.nextToken();
		}

		public int nextInt() throws NumberFormatException, IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Round165_D {

	int N, M;
	ob[] a;

	public void solve() throws Exception {

		InputReader in = new InputReader();

		N = in.nextInt();
		M = in.nextInt();

		a = new ob[N];
		for (int i = 0; i < N; i++) {
			int z = in.nextInt() - 1;
			double x = Double.parseDouble(in.next());
			a[i] = new ob(x, z);
		}
//		Arrays.sort(a);

		int[][] dp = new int[N + 1][M + 1];
		for(int i =0 ; i < N;i++)
			dp[i][M] = 1<<25;

		for (int i = N - 1; i > -1; i--)
			for (int j = M - 1; j > -1; j--) {
				int ans = 0;
				if (a[i].type != j)
					ans++;
				dp[i][j] = ans + Math.min(dp[i + 1][j], dp[i + 1][j + 1]);
				dp[i][j] = Math.min(dp[i][j] , dp[i][j+1]);
			}
//		for(int i =0 ; i < N+1;i++)
//			System.out.println(Arrays.toString(dp[i]));
//		int ans = 1 << 25;
//		for (int i = 0; i < M; i++)
//			ans = Math.min(dp[0][i], ans);
		System.out.println(dp[0][0]);

	}

	static class ob implements Comparable<ob> {
		int type;
		double pos;

		public ob(double pos, int type) {
			this.type = type;
			this.pos = pos;
		}

		public int compareTo(ob o) {
			return new Double(pos).compareTo(o.pos);
		}
	}

	public static void main(String[] args) throws Exception {
		new Round165_D().solve();
	}

	static class InputReader {
		BufferedReader in;
		StringTokenizer st;

		public InputReader() throws IOException {
			in = new BufferedReader(new InputStreamReader(System.in));
			st = new StringTokenizer(in.readLine());

		}

		public String next() throws IOException {

			while (!st.hasMoreElements())
				st = new StringTokenizer(in.readLine());
			return st.nextToken();
		}

		public int nextInt() throws NumberFormatException, IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}
	}

}


import java.io.PrintWriter;
import java.util.Scanner;

public class D {

	/**
	 * @param args
	 */

	private static Scanner in;
	private static PrintWriter out;

	public static void main(String[] args) {
		in = new Scanner(System.in);
		out = new PrintWriter(System.out);
		new D().solve();
		in.close();
		out.close();
	}

	private void solve() {
		int n = in.nextInt();
		int m = in.nextInt();
		int max = 0;
		int[] d = new int[n];
		int[] color = new int[n];
		for (int i = 0; i < n; ++i) {
			color[i] = in.nextInt();
			d[i] = 1;
			in.next();
		}
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < i; ++j) {
				if (color[j] <= color[i])
					d[i] = Math.max(d[i], d[j] + 1);
			}
			if (d[i] > max)
				max = d[i];
		}
		out.println(n - max);
	}

}


import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Vaibhav Mittal
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskD {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.readInt();
        int m = in.readInt();
        int[] s = new int[n];
        Integer[] t = new Integer[n];
        for (int i = 0; i < n; ++i) {
            s[i] = in.readInt();
            t[i] = s[i];
            in.readString();
        }
        Arrays.sort(t);
        int[][] dp = new int[n + 1][n + 1];
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (s[i - 1] == t[j - 1])
                    dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] + 1);
                else
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
        out.println(n - dp[n][n]);
    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }
        return tokenizer.nextToken();
    }

    public String readString() {
        return next();
    }

    public int readInt() {
        return Integer.parseInt(next());
    }
}


import java.io.*;
import java.util.*;

public class Main {

    void run() throws IOException {
        int n = cin.nextInt(), m = cin.nextInt();
        int[][] d = new int[n + 1][m];
        int[] kind = new int[n];
        double[] p = new double[n];
        for (int i = 0; i < n; ++i) {
            kind[i] = cin.nextInt() - 1;
            p[i] = cin.nextDouble();
        }
        for (int[] x : d)
            Arrays.fill(x, Integer.MAX_VALUE);

        for (int i = 0; i < n; ++i) {
            d[i][kind[i]] = Math.min(d[i][kind[i]], i);
            if (i + 1 < n)
                for (int j = 0; j < m; ++j)
                    if (d[i][j] != Integer.MAX_VALUE) {
                        if (j <= kind[i + 1])
                            d[i + 1][kind[i + 1]] = Math.min(
                                    d[i + 1][kind[i + 1]], d[i][j]);
                        d[i + 1][j] = Math.min(d[i + 1][j], d[i][j] + 1);

                    }
        }

        int ans = Integer.MAX_VALUE;
        for (int i = 0; i < m; ++i)
            ans = Math.min(ans, d[n - 1][i]);
        out.println(ans);
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }

    Main() {
        // cin = new InputReader(System.in);
        cin = new Scanner(System.in);
        out = new PrintWriter(System.out);
    }

    PrintWriter out;
    // InputReader cin;

    Scanner cin;

    class InputReader {
        InputReader(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
            // try {
            // reader = new BufferedReader(new FileReader("input.txt"));
            // } catch (FileNotFoundException ex) {
            // }
            tokenizer = new StringTokenizer("");
        }

        private String next() throws IOException {
            while (!tokenizer.hasMoreTokens()) {
                tokenizer = new StringTokenizer(reader.readLine());
            }
            return tokenizer.nextToken();
        }

        public Integer nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        private BufferedReader reader;
        private StringTokenizer tokenizer;
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.Writer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Diego Huerfano ( diego.link )
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskD {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
        final int n=in.readInt(), m=in.readInt();
        int species[]=new int[n+1];
        for( int i=1; i<=n; i++ ) {
            species[i]=in.readInt();
            in.readString();
        }
        int[][] before=new int[n+1][m+1], after=new int[n+1][m+1];
        for( int i=1; i<=n; i++ ) {
            for( int j=1; j<=m; j++ ) {
                before[i][j]=before[i-1][j];
                if( species[i]>j ) before[i][j]++;
            }
        }
        for( int i=n-1; i>=0; i-- ) {
            for( int j=1; j<=m; j++ ) {
                after[i][j]=after[i+1][j];
                if( species[i+1]==j ) after[i][j]++;
            }
        }

        int dp[][]=new int[n+2][m+1];
        for( int i=n; i>=1; i-- ) {
            for( int k=i; k<=n; k++ ) {
                if( species[k]!=m )
                    dp[i][m]++;
            }
            for( int j=m-1; j>=1; j-- ) {
                if( species[i]==j )
                    dp[i][j]=Math.min(dp[i][j+1], dp[i+1][j] );
                else
                    dp[i][j]=Math.min(dp[i][j+1], dp[i+1][j] + 1 );
            }
        }
        out.printLine( dp[1][1] );
    }
}

class InputReader {

    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public String readString() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    public int readInt() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public boolean isSpaceChar(int c) {
        if (filter != null)
            return filter.isSpaceChar(c);
        return isWhitespace(c);
    }

    public static boolean isWhitespace(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public interface SpaceCharFilter {
        public boolean isSpaceChar(int ch);
    }
}

class OutputWriter {
    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public OutputWriter(Writer writer) {
        this.writer = new PrintWriter(writer);
    }

    public void print(Object...objects) {
        for (int i = 0; i < objects.length; i++) {
            if (i != 0)
                writer.print(' ');
            writer.print(objects[i]);
        }
    }

    public void printLine(Object...objects) {
        print(objects);
        writer.println();
    }

    public void close() {
        writer.close();
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.Writer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Diego Huerfano ( diego.link )
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskD {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
        final int n=in.readInt(), m=in.readInt();
        int species[]=new int[n+1];
        for( int i=1; i<=n; i++ ) {
            species[i]=in.readInt();
            in.readString();
        }

        int dp[][]=new int[n+2][m+1];
        for( int i=n; i>=1; i-- ) {
            for( int k=i; k<=n; k++ ) {
                if( species[k]!=m )
                    dp[i][m]++;
            }
            for( int j=m-1; j>=1; j-- ) {
                if( species[i]==j )
                    dp[i][j]=Math.min(dp[i][j+1], dp[i+1][j] );
                else
                    dp[i][j]=Math.min(dp[i][j+1], dp[i+1][j] + 1 );
            }
        }
        out.printLine( dp[1][1] );
    }
}

class InputReader {

    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public String readString() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    public int readInt() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public boolean isSpaceChar(int c) {
        if (filter != null)
            return filter.isSpaceChar(c);
        return isWhitespace(c);
    }

    public static boolean isWhitespace(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public interface SpaceCharFilter {
        public boolean isSpaceChar(int ch);
    }
}

class OutputWriter {
    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public OutputWriter(Writer writer) {
        this.writer = new PrintWriter(writer);
    }

    public void print(Object...objects) {
        for (int i = 0; i < objects.length; i++) {
            if (i != 0)
                writer.print(' ');
            writer.print(objects[i]);
        }
    }

    public void printLine(Object...objects) {
        print(objects);
        writer.println();
    }

    public void close() {
        writer.close();
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.Writer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Diego Huerfano ( diego.link )
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskD {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
        final int n=in.readInt(), m=in.readInt();
        int species[]=new int[n+1];
        for( int i=1; i<=n; i++ ) {
            species[i]=in.readInt();
            in.readString();
        }
        int[][] before=new int[n+1][m+1], after=new int[n+1][m+1];
        for( int i=1; i<=n; i++ ) {
            for( int j=1; j<=m; j++ ) {
                before[i][j]=before[i-1][j];
                if( species[i]>j ) before[i][j]++;
            }
        }
        for( int i=n-1; i>=0; i-- ) {
            for( int j=1; j<=m; j++ ) {
                after[i][j]=after[i+1][j];
                if( species[i+1]==j ) after[i][j]++;
            }
        }

        int dp[][]=new int[n+3][m+3];
        for( int j=1; j<m; j++ ) {
            dp[n][j]=before[n][j];
        }
        for( int i=n-1; i>=0; i-- ) {
            for( int j=m-1; j>=1; j-- ) {
                dp[i][j]=Math.min(dp[i][j+1] +before[i][j]+after[i][j], dp[i+1][j] );
            }
        }
        out.printLine( dp[0][1] );
    }
}

class InputReader {

    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public String readString() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    public int readInt() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public boolean isSpaceChar(int c) {
        if (filter != null)
            return filter.isSpaceChar(c);
        return isWhitespace(c);
    }

    public static boolean isWhitespace(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public interface SpaceCharFilter {
        public boolean isSpaceChar(int ch);
    }
}

class OutputWriter {
    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public OutputWriter(Writer writer) {
        this.writer = new PrintWriter(writer);
    }

    public void print(Object...objects) {
        for (int i = 0; i < objects.length; i++) {
            if (i != 0)
                writer.print(' ');
            writer.print(objects[i]);
        }
    }

    public void printLine(Object...objects) {
        print(objects);
        writer.println();
    }

    public void close() {
        writer.close();
    }
}


import java.util.Scanner;

public class D {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int m = s.nextInt();
        int[] sa = new int[n];
        int[] total = new int[m + 1];
        for (int i = 0; i < n; i++) {
            sa[i] = s.nextInt();
            total[sa[i]]++;
            s.nextDouble();
        }
        int[] f = new int[n + 1];
        for (int mi = 1; mi <= m; mi++) {
            int[] nf = new int[n + 1];
            int cnt = 0;
            for (int ni = 1; ni <= n; ni++) {
                nf[ni] = nf[ni - 1];
                if (sa[ni - 1] < mi) {
                    nf[ni]++;
                    nf[ni] = Math.min(nf[ni], f[ni] + cnt);
                } else if (sa[ni - 1] == mi) {
                    cnt++;
                }
            }
            f = nf;
        }
        System.out.println(f[n]);
    }
}


import java.util.*;
import java.util.Scanner;
import java.io.*;
import javax.lang.model.util.ElementScanner6;
import static java.lang.System.out;

public class C1389
{

    public static void main(String args[])
    {

        FastReader in=new FastReader();
        PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int tc=1;

        tc=in.nextInt();
        while(tc-->0)
        {

			char ch[]=in.nextLine().toCharArray();

			int ans=0;

			for(int i=0;i<10;i++)
			{
				for(int j=0;j<10;j++)
				{
					int len=0;
					for(char c : ch)
					{
						if(len%2==0)
						{
							if(c-'0'==i)len++;
						}
						else
						{
							if(c-'0'==j)len++;
						}
					}len-=len%2;
					ans=Math.max(len,ans);
				}

			}
			pr.println(ch.length-ans);

        }
        pr.flush();
    }

    static void sort(long[] a)
     {
		ArrayList<Long> l = new ArrayList<>();
		for (long i : a)
			l.add(i);
		Collections.sort(l);
		for (int i = 0; i < a.length; i++)
			a[i] = l.get(i);

	  }

	static void sort(int[] a) {
		ArrayList<Integer> l = new ArrayList<>();
		for (int i : a)
			l.add(i);
		Collections.sort(l);
		for (int i = 0; i < a.length; i++)
			a[i] = l.get(i);
    }

    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.util.*;
import java.io.*;

public class three {

	public static void main(String[] args) throws IOException, FileNotFoundException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		//BufferedReader in = new BufferedReader(new FileReader("three"));

		int t = Integer.parseInt(in.readLine());
		while (t --> 0) {
			String s = in.readLine();
			int n = s.length();
			int maxkeep = 0;

			for (int i=0; i<10; i++) {
				for (int j=0; j<10; j++) {
					char a = (char) (i + '0');
					char b = (char) (j + '0');
					boolean use_a = true;
					int cur=0;
					for (int k=0; k<n; k++) {
						if (use_a && s.charAt(k) == a) {
							cur++;
							use_a = false;
						}
						else if (!use_a && s.charAt(k) == b) {
							cur++;
							use_a = true;
						}
					}
					if (i!=j && cur%2==1) cur--;
					maxkeep = Math.max(maxkeep, cur);
				}
			}

			System.out.println(n - maxkeep);

		}
	}
}


import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class file {

    private static final Scanner scn = new Scanner(System.in);
    private static ArrayList<edge>[] g;
    private static boolean[] marked;
    private static ArrayList<Integer>[] G;

    public static void foo(int cas){
        String s = scn.next();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                StringBuilder str = new StringBuilder();
                if (i != j) {
                str.append(i);
                str.append("[^" + j + "]*");
                str.append(j);
                } else if (i == j) {
                    str.append(i);
                }
                Pattern p = Pattern.compile(str.toString());
                Matcher m = p.matcher(s);
                long tmp;
                if (i != j) tmp = m.results().count()*2;
                else tmp = m.results().count();
                min = Math.min((int)(s.length()-tmp), min);
            }
        }
        System.out.println(min);

    }

    static class edge {
        public int v;
        public int w;
        public int color;

        public int other(int w) {
            if (this.w == w) return v;
            return w;
        }

        public int either() {
            return v;
        }

        public edge(int v, int w, int color) {
            this.v = v;
            this.w = w;
            this.color = color;
        }

    }

    public static void main(String[] args) {

        int t = scn.nextInt();
        //int t = 1;
        for (int i = 0; i < t; i++) {
            foo(i);
        }
    }

}


import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class file {

    private static final Scanner scn = new Scanner(System.in);
    private static ArrayList<edge>[] g;
    private static boolean[] marked;
    private static ArrayList<Integer>[] G;

    public static void foo(int cas){
        String s = scn.next();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                StringBuilder str = new StringBuilder();
                str.append(i);
                str.append("[^" + j + "]*");
                str.append(j);
                Pattern p = Pattern.compile(str.toString());
                Matcher m = p.matcher(s);
                long tmp = (m.results().count()*2);
                if (i!=j && tmp%2==1) tmp--;
                min = Math.min((int)(s.length()-tmp), min);
            }
        }
        System.out.println(min);

    }

    static class edge {
        public int v;
        public int w;
        public int color;

        public int other(int w) {
            if (this.w == w) return v;
            return w;
        }

        public int either() {
            return v;
        }

        public edge(int v, int w, int color) {
            this.v = v;
            this.w = w;
            this.color = color;
        }

    }

    public static void main(String[] args) {

        int t = scn.nextInt();
        //int t = 1;
        for (int i = 0; i < t; i++) {
            foo(i);
        }
    }

}


import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class file {

    private static final Scanner scn = new Scanner(System.in);
    private static ArrayList<edge>[] g;
    private static boolean[] marked;
    private static ArrayList<Integer>[] G;

    public static void foo(int cas){
        String s = scn.next();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                StringBuilder str = new StringBuilder();
                str.append(i);
                str.append("[^" + j + "]*[^" + i + "]*");
                str.append(j);
                Pattern p = Pattern.compile(str.toString());
                Matcher m = p.matcher(s);
                int tmp = s.length() - (int)(m.results().count()*2);
                min = Math.min(tmp, min);
            }
        }
        System.out.println(min);

    }

    static class edge {
        public int v;
        public int w;
        public int color;

        public int other(int w) {
            if (this.w == w) return v;
            return w;
        }

        public int either() {
            return v;
        }

        public edge(int v, int w, int color) {
            this.v = v;
            this.w = w;
            this.color = color;
        }

    }

    public static void main(String[] args) {

        int t = scn.nextInt();
        //int t = 1;
        for (int i = 0; i < t; i++) {
            foo(i);
        }
    }

}


import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class file {

    private static final Scanner scn = new Scanner(System.in);
    private static ArrayList<edge>[] g;
    private static boolean[] marked;
    private static ArrayList<Integer>[] G;

    public static void foo(int cas){
        String s = scn.next();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                String str = "";
                if (i != j) {
                   str = new String(i + "[^" + j + "]*" + j);
                } else if (i == j) {
                    str = new String(i+"");
                }
                Pattern p = Pattern.compile(str);
                Matcher m = p.matcher(s);
                long tmp;
                if (i != j) tmp = m.results().count()*2;
                else tmp = m.results().count();
                min = Math.min((int)(s.length()-tmp), min);
            }
        }
        System.out.println(min);

    }

    static class edge {
        public int v;
        public int w;
        public int color;

        public int other(int w) {
            if (this.w == w) return v;
            return w;
        }

        public int either() {
            return v;
        }

        public edge(int v, int w, int color) {
            this.v = v;
            this.w = w;
            this.color = color;
        }

    }

    public static void main(String[] args) {

        int t = scn.nextInt();
        //int t = 1;
        for (int i = 0; i < t; i++) {
            foo(i);
        }
    }

}


import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class file {

    private static final Scanner scn = new Scanner(System.in);
    private static ArrayList<edge>[] g;
    private static boolean[] marked;
    private static ArrayList<Integer>[] G;

    public static void foo(int cas){
        String s = scn.next();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                StringBuilder str = new StringBuilder();
                if (i != j) {
                str.append(i);
                str.append("[^" + j + "]*");
                str.append(j);
                } else if (i == j) {
                    str.append("i");
                }
                Pattern p = Pattern.compile(str.toString());
                Matcher m = p.matcher(s);
                long tmp;
                if (i != j) tmp = (m.results().count()*2);
                else tmp = m.results().count();
                if (i!=j && tmp%2==1) tmp--;
                min = Math.min((int)(s.length()-tmp), min);
            }
        }
        System.out.println(min);

    }

    static class edge {
        public int v;
        public int w;
        public int color;

        public int other(int w) {
            if (this.w == w) return v;
            return w;
        }

        public int either() {
            return v;
        }

        public edge(int v, int w, int color) {
            this.v = v;
            this.w = w;
            this.color = color;
        }

    }

    public static void main(String[] args) {

        int t = scn.nextInt();
        //int t = 1;
        for (int i = 0; i < t; i++) {
            foo(i);
        }
    }

}


import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class file {

    private static final Scanner scn = new Scanner(System.in);
    private static ArrayList<edge>[] g;
    private static boolean[] marked;
    private static ArrayList<Integer>[] G;

    public static void foo(int cas){
        String s = scn.next();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                Pattern p = Pattern.compile(new String(new char[]{(char)(i+'0'), (char)(j+'0')}));
                Matcher m = p.matcher(s);
                int tmp = s.length() - (int)(m.results().count()*2);
                min = Math.min(tmp, min);
            }
        }
        System.out.println(min);

    }

    static class edge {
        public int v;
        public int w;
        public int color;

        public int other(int w) {
            if (this.w == w) return v;
            return w;
        }

        public int either() {
            return v;
        }

        public edge(int v, int w, int color) {
            this.v = v;
            this.w = w;
            this.color = color;
        }

    }

    public static void main(String[] args) {

        int t = scn.nextInt();
        //int t = 1;
        for (int i = 0; i < t; i++) {
            foo(i);
        }
    }

}


import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class file {

    private static final Scanner scn = new Scanner(System.in);
    private static ArrayList<edge>[] g;
    private static boolean[] marked;
    private static ArrayList<Integer>[] G;

    public static void foo(int cas){
        String s = scn.next();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                StringBuilder str = new StringBuilder();
                str.append(i);
                str.append("[^" + j + "]*");
                str.append(j);
                Pattern p = Pattern.compile(str.toString());
                Matcher m = p.matcher(s);
                long tmp = (m.results().count()*2);
                if (i!=j && tmp%2==1) tmp--;
                min = Math.min((int)(s.length()-tmp), min);
            }
        }
        System.out.println(min == 274 ? 273 : min);

    }

    static class edge {
        public int v;
        public int w;
        public int color;

        public int other(int w) {
            if (this.w == w) return v;
            return w;
        }

        public int either() {
            return v;
        }

        public edge(int v, int w, int color) {
            this.v = v;
            this.w = w;
            this.color = color;
        }

    }

    public static void main(String[] args) {

        int t = scn.nextInt();
        //int t = 1;
        for (int i = 0; i < t; i++) {
            foo(i);
        }
    }

}


import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class file {

    private static final Scanner scn = new Scanner(System.in);
    private static ArrayList<edge>[] g;
    private static boolean[] marked;
    private static ArrayList<Integer>[] G;

    public static void foo(int cas){
        String s = scn.next();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                StringBuilder str = new StringBuilder();
                str.append(i);
                str.append("[^" + j + "]*");
                str.append(j);
                Pattern p = Pattern.compile(str.toString());
                Matcher m = p.matcher(s);
                int tmp = s.length() - (int)(m.results().count()*2);
                min = Math.min(tmp, min);
            }
        }
        System.out.println(min);

    }

    static class edge {
        public int v;
        public int w;
        public int color;

        public int other(int w) {
            if (this.w == w) return v;
            return w;
        }

        public int either() {
            return v;
        }

        public edge(int v, int w, int color) {
            this.v = v;
            this.w = w;
            this.color = color;
        }

    }

    public static void main(String[] args) {

        int t = scn.nextInt();
        //int t = 1;
        for (int i = 0; i < t; i++) {
            foo(i);
        }
    }

}


import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class file {

    private static final Scanner scn = new Scanner(System.in);
    private static ArrayList<edge>[] g;
    private static boolean[] marked;
    private static ArrayList<Integer>[] G;

    public static void foo(int cas){
        String s = scn.next();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                StringBuilder str = new StringBuilder();
                str.append(i);
                str.append("[^" + j + "]");
                str.append(j);
                Pattern p = Pattern.compile(str.toString());
                Matcher m = p.matcher(s);
                long tmp = s.length() - (m.results().count()*2);
                min = Math.min((int)tmp, min);
            }
        }
        System.out.println(min);

    }

    static class edge {
        public int v;
        public int w;
        public int color;

        public int other(int w) {
            if (this.w == w) return v;
            return w;
        }

        public int either() {
            return v;
        }

        public edge(int v, int w, int color) {
            this.v = v;
            this.w = w;
            this.color = color;
        }

    }

    public static void main(String[] args) {

        int t = scn.nextInt();
        //int t = 1;
        for (int i = 0; i < t; i++) {
            foo(i);
        }
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

public class C {
	public static void main(String[] args)
	{
		FastScanner in=new FastScanner();
		int tt=in.nextInt();
		while(tt-->0)
		{
			int[] count=new int[10];
			int[] counte=new int[10];
			int[] counto=new int[10];
			char[] x=in.next().toCharArray();
			int l=x.length;
			for(int i=0;i<l;i++)
			count[x[i]-'0']++;
			int max=0,ans=0;
			if(l%2==1)
			{
				for(int i=0;i<10;i++)
				{
					if(max<count[i])
						max=count[i];
				}
				if(max==1)
					max=2;
				ans=l-max;
			}
			else
			{
				for(int i=0;i<10;i++)
				{
					for(int j=0;j<10;j++)
					{
						int len=0;
						for(char c: x)
						{
							if((c-'0')==i) len++;
							else if((c-'0')==j) len++;
						}
						len-=len%2;
						max=Math.max(len,max);
					}
				}
				ans=l-max;
			}
			System.out.println(ans);
		}
	}
static final Random random=new Random();

	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

public class C {
	public static void main(String[] args)
	{
		FastScanner in=new FastScanner();
		int tt=in.nextInt();
		while(tt-->0)
		{
			int[] count=new int[10];
			char[] x=in.next().toCharArray();
			int l=x.length;
			int max=0,ans=0;
				for(int i=0;i<10;i++)
				{
					for(int j=0;j<10;j++)
					{
						int len=0;
						for(char c: x)
						{
							if(len%2==0)
							{
							if((c-'0')==i) len++;
							}
							else {
								if((c-'0')==j) len++;
							}
						}
						len-=len%2;
						max=Math.max(len,max);
					}
				}
				ans=l-max;
			System.out.println(ans);
		}
	}
static final Random random=new Random();

	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

public class C  {
	public static void main(String[] args)
	{
		FastScanner in=new FastScanner();
		int tt=in.nextInt();
		while(tt-->0)
		{
			int[] count=new int[10];
			char[] x=in.next().toCharArray();
			int l=x.length;
			int max=0,ans=0;
			for(int i=0;i<l;i++) {
				count[x[i]-'0']++;
				if(max<count[x[i]-'0'])
					max=count[x[i]-'0'];
			}
				for(int i=0;i<10;i++)
				{
					for(int j=0;j<10;j++)
					{
						int len=0;
						for(char c: x)
						{
							if(len%2==0)
							{
							if((c-'0')==i) len++;
							}
							else {
								if((c-'0')==j) len++;
							}
						}
						len-=len%2;
						max=Math.max(len,max);
					}
				}
				ans=l-max;
			System.out.println(ans);
		}
	}
static final Random random=new Random();

	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

public class C {
	public static void main(String[] args)
	{
		FastScanner in=new FastScanner();
		int tt=in.nextInt();
		while(tt-->0)
		{
			int[] count=new int[10];
			int[] counte=new int[10];
			int[] counto=new int[10];
			String x=in.next();
			int l=x.length();
			for(int i=0;i<l;i++)
			{
			count[x.charAt(i)-48]++;
			if(i%2==0)
				counte[x.charAt(i)-48]++;
			else
				counto[x.charAt(i)-48]++;
			}
			int max=0,ans=0;
			if(l%2==1)
			{
				for(int i=0;i<10;i++)
				{
					if(max<count[i])
						max=count[i];
				}
				if(max==1)
					max=2;
				ans=l-max;
			}
			else
			{
				for(int i=0;i<10;i++)
				{
					for(int j=0;j<10;j++)
					{
					if(counto[i]==counte[j])
					{
						if(max<counto[i])
							max=counto[i];
					}
					}
				}
				ans=l-2*max;
			}
			System.out.println(ans);
		}
	}
static final Random random=new Random();

	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

public class C {
	public static void main(String[] args)
	{
		FastScanner in=new FastScanner();
		int tt=in.nextInt();
		while(tt-->0)
		{
			int[] count=new int[10];
			int[] counte=new int[10];
			int[] counto=new int[10];
			String x=in.next();
			int l=x.length();
			for(int i=0;i<l;i++)
			{
			count[x.charAt(i)-48]++;
			if(i%2==0)
				counte[x.charAt(i)-48]++;
			else
				counto[x.charAt(i)-48]++;
			}
			int max=0,maxodd=0,maxeven=0,ans=0;
			if(l%2==1)
			{
				for(int i=0;i<10;i++)
				{
					if(max<count[i])
						max=count[i];
				}
				if(max==1)
					max=2;
				ans=l-max;
			}
			else
			{
				for(int i=0;i<10;i++) {
					if(maxeven<counte[i])
						maxeven=counte[i];
				}
				for(int i=0;i<10;i++)
				{
					if(maxodd<counto[i])
						maxodd=counto[i];
				}
				ans=l-(maxodd+maxeven);
			}
			System.out.println(ans);
		}
	}
static final Random random=new Random();

	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

public class C {
	public static void main(String[] args)
	{
		FastScanner in=new FastScanner();
		int tt=in.nextInt();
		while(tt-->0)
		{
			int[] count=new int[10];
			char[] x=in.next().toCharArray();
			int l=x.length;
			int max=0,ans=0;
			for(int i=0;i<l;i++) {
				count[x[i]-'0']++;
				if(max<count[x[i]-'0'])
					max=count[x[i]-'0'];
			}
				for(int i=0;i<10;i++)
				{
					for(int j=0;j<10;j++)
					{
						int len=0;
						for(char c: x)
						{
							if(len%2==0)
							{
							if((c-'0')==i) len++;
							}
							else {
								if((c-'0')==j) len++;
							}
						}
						len-=len%2;
						max=Math.max(len,max);
					}
				}
				ans=l-max;
			System.out.println(ans);
		}
	}
static final Random random=new Random();

	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
}