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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        static final int INF = (int) 1e9;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] delta = new int[m + 1];
            for (int i = 0; i < n; ++i) {
                int l = in.nextInt() - 1;
                int r = in.nextInt();
                ++delta[l];
                --delta[r];
            }
            int[] cover = new int[m];
            for (int i = 0; i < m; ++i) {
                cover[i] = delta[i];
                if (i > 0) cover[i] += cover[i - 1];
            }
            int[] a = maxNonDecreasing(cover);
            int[] b = maxNonDecreasing(reverse(cover));
            int res = 0;
            for (int i = 0; i <= m; ++i) {
                res = Math.max(res, a[i] + b[m - i]);
            }
            out.println(res);
        }

        private int[] maxNonDecreasing(int[] cover) {
            int n = cover.length;
            int[] res = new int[n + 1];
            int[] best = new int[n + 1];
            Arrays.fill(best, INF);
            best[0] = 0;
            int maxSoFar = 0;
            for (int i = 0; i < n; ++i) {
                int cur = cover[i];
                int left = 0;
                int right = maxSoFar + 1;
                while (right - left > 1) {
                    int middle = (left + right) >> 1;
                    if (best[middle] <= cur) {
                        left = middle;
                    } else {
                        right = middle;
                    }
                }
                if (left == maxSoFar) ++maxSoFar;
                best[right] = cur;
                res[i + 1] = maxSoFar;
            }
            return res;
        }

        int[] reverse(int[] x) {
            int[] y = new int[x.length];
            for (int i = 0; i < x.length; ++i) y[i] = x[x.length - 1 - i];
            return y;
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

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

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

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

    }
}

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

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

    static class TaskC {
        static int binarySearch(ArrayList<Integer> arr, int val) {
            if (arr.size() == 0) return -1;
            if (val >= arr.get(arr.size() - 1)) return -(arr.size() + 1);

            int lo = 0, hi = arr.size() - 1, best = 0;
            for (int cnt = 0; cnt <= 21; cnt++) {
                int mid = lo + ((hi - lo) >> 1);
                if (arr.get(mid) > val) {
                    best = mid;
                    hi = Math.max(lo, mid - 1);
                } else {
                    lo = Math.min(hi, mid + 1);
                }
            }
            return best;
        }

        static int[] Prefix_LNS(ArrayList<Integer> arr) {
            int n = arr.size();
            ArrayList<Integer> L = new ArrayList<>();
            int lis = 0;
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                int num = arr.get(i);
                int pos = binarySearch(L, num);
                if (pos < 0) pos = -pos - 1;
                res[i] = pos + 1;
                if (pos + 1 > lis) {
                    lis++;
                    L.add(num);
                } else {
                    L.set(pos, num);
                }
            }

            return res;
        }

        public void solve(int testNumber, Scanner sc, PrintWriter out) {
            int n = sc.nextInt(), m = sc.nextInt();
            int[] cnt = new int[m + 2];
            for (int i = 0; i < n; i++) {
                cnt[sc.nextInt()]++;
                cnt[sc.nextInt() + 1]--;
            }

            ArrayList<Integer> a = new ArrayList<>();
            int cum = 0;
            for (int i = 1; i <= m; i++) {
                a.add(cum += cnt[i]);
            }
            int[] first = Prefix_LNS(a);
            Collections.reverse(a);
            int[] second = Prefix_LNS(a);
            int max = 0;
            for (int i = 0; i < m; i++) {
                max = Math.max(max, first[i] + second[m - i - 1] - 1);
            }
            out.println(max);
        }

    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

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

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

    }
}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class codeforces {
	public static void main(String[] args) {
		FS scan = new FS(System.in);

		int N = scan.nextInt(), X = scan.nextInt();
		int[] interval = new int[X];
		ArrayList<Integer> list = new ArrayList<>();
		for(int i=0;i<N;i++) {
			int a = scan.nextInt()-1, b = scan.nextInt()-1;
			list.add(a*2);
			list.add(b*2+1);
		}

		Collections.sort(list);

		int stack = 0, idx = 0;
		for(int j = 0; j < 2*N; j++) {
			int i = list.get(j);
			if(j==0||(i>>1)!=(list.get(j-1)>>1))
				while(idx<(i>>1))interval[idx++] = stack;
			if(j==2*N-1 || (i%2)==1)
				while(idx<=(i>>1))interval[idx++] = stack;
			if((i&1)==0)
				stack++;
			if((i&1)==1)
				stack--;
		}

		int[] lis = new int[X], lds = new int[X];

		TreeMap<Integer,Integer> map = new TreeMap<>();
		for(int i = 0; i < X ; i++) {
			Integer floorkey =  map.floorKey(interval[i]),
					ceilkey = null;
			lis[i] = (floorkey==null) ? 1 : map.get(floorkey)+1;
			while((ceilkey = map.ceilingKey(interval[i]))!=null) {
				if(map.get(ceilkey) < lis[i])
					map.remove(ceilkey);
				else break;
			}
			map.put(interval[i], lis[i]);
		}

		map = new TreeMap<>();
		for(int i = X-1; i>=0;i--) {
			Integer floorkey =  map.floorKey(interval[i]),
					ceilkey = null;
			lds[i] = (floorkey==null) ? 1 : map.get(floorkey)+1;
			while((ceilkey = map.ceilingKey(interval[i]))!=null) {
				if(map.get(ceilkey) < lds[i])
					map.remove(ceilkey);
				else break;
			}
			map.put(interval[i], lds[i]);
		}

		int max = 0;
		for(int i=0;i<X-1;i++)
			max = (max < lis[i]+lds[i+1]) ? lis[i]+lds[i+1] : max;
		System.out.println(max);
	}
	private static class FS {
		BufferedReader br;
		StringTokenizer st;
		public FS(InputStream in) {
			br = new BufferedReader(new InputStreamReader(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());}
	}
}

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

public class C {

	public static void main(String[] args) {
//		int[] arr = {3,2,5,7,8,5,3,2,4,5,7,4,2,1,5,6,4};
//		System.out.println(Arrays.toString(arr));
//		System.out.println(Arrays.toString(LIS(arr)));
 		FastScanner sc = new FastScanner();
		int n = sc.nextInt();
		int m = sc.nextInt();
		int[] d = new int[m+1];
		for(int i = 0; i < n; i++) {
			int L = sc.nextInt()-1;
			int R = sc.nextInt()-1;
			d[L]++;
			d[R+1]--;
		}
		int[] h = new int[m];
		for(int i = 0; i < m; i++) {
			if(i > 0) h[i] += h[i-1];
			h[i] += d[i];
		}
//		System.out.println(Arrays.toString(h));
		int[] lispref = LIS(h);
		int[] lissuff = invert(LIS(invert(h)));
		int res = 0;
		for(int i = 0; i < m; i++) {
			res = Math.max(res, lispref[i] + lissuff[i] - 1);
		}
		System.out.println(res);
	}

	static int[] invert(int[] arr) {
		int n = arr.length;
		int[] res = new int[n];
		for(int i = 0; i < n; i++) {
			res[i] = arr[n-i-1];
		}
		return res;
	}
	static int[] LIS(int[] arr) {//Longest Increasing Subsequence
		int n = arr.length;
		int inf = (int) 1e9;
		int[] d = new int[n+1];
		Arrays.fill(d, inf);
		d[0] = -inf;
		int[] res = new int[n];
		for(int i = 0; i < n; i++) {
			int a = 0, b = n; //binsearch
			while(b - a > 1) {
				int c = (a+b)/2;
				if(d[c] <= arr[i]) a = c;
				else b = c;
			}
			res[i] = b;
			if(d[b] > arr[i]) d[b] = arr[i];
		}
		return res;
	}

	static class FastScanner {
		public BufferedReader reader;
		public StringTokenizer tokenizer;
		public FastScanner() {
			reader = new BufferedReader(new InputStreamReader(System.in), 32768);
			tokenizer = null;
		}
		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public double nextDouble() {
			return Double.parseDouble(next());
		}
		public String nextLine() {
			try {
				return reader.readLine();
			} catch(IOException e) {
				throw new RuntimeException(e);
			}
		}
	}

}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.TreeMap;
import java.io.Closeable;
import java.util.Map;
import java.io.Writer;
import java.util.Map.Entry;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastInput in = new FastInput(inputStream);
            FastOutput out = new FastOutput(outputStream);
            CTeodorIsNotALiar solver = new CTeodorIsNotALiar();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class CTeodorIsNotALiar {
        public void solve(int testNumber, FastInput in, FastOutput out) {
            int n = in.readInt();
            int m = in.readInt();
            IntegerBIT bit = new IntegerBIT(m + 1);
            for (int i = 0; i < n; i++) {
                int l = in.readInt();
                int r = in.readInt();
                bit.update(l, 1);
                bit.update(r + 1, -1);
            }
            int[] seq = new int[m];
            for (int i = 0; i < m; i++) {
                seq[i] = bit.query(i + 1);
            }

            int[] l2r = inc(seq);
            SequenceUtils.reverse(seq, 0, seq.length - 1);
            int[] r2l = inc(seq);
            SequenceUtils.reverse(r2l, 0, seq.length - 1);

            int ans = 0;
            for (int i = 0; i < seq.length; i++) {
                ans = Math.max(l2r[i] + r2l[i] - 1, ans);
            }

            out.println(ans);
        }

        public int[] inc(int[] seq) {
            TreeMap<Integer, Integer> map = new TreeMap<>();
            int[] ans = new int[seq.length];
            for (int i = 0; i < seq.length; i++) {
                Map.Entry<Integer, Integer> floor = map.floorEntry(seq[i]);
                if (floor == null) {
                    ans[i] = 1;
                } else {
                    ans[i] = floor.getValue() + 1;
                }
                while (true) {
                    Map.Entry<Integer, Integer> ceil = map.ceilingEntry(seq[i]);
                    if (ceil == null || ceil.getValue() > ans[i]) {
                        break;
                    }
                    map.remove(ceil.getKey());
                }
                map.put(seq[i], ans[i]);
            }
            return ans;
        }

    }

    static class FastOutput implements AutoCloseable, Closeable, Appendable {
        private StringBuilder cache = new StringBuilder(10 << 20);
        private final Writer os;

        public FastOutput append(CharSequence csq) {
            cache.append(csq);
            return this;
        }

        public FastOutput append(CharSequence csq, int start, int end) {
            cache.append(csq, start, end);
            return this;
        }

        public FastOutput(Writer os) {
            this.os = os;
        }

        public FastOutput(OutputStream os) {
            this(new OutputStreamWriter(os));
        }

        public FastOutput append(char c) {
            cache.append(c);
            return this;
        }

        public FastOutput println(int c) {
            cache.append(c);
            println();
            return this;
        }

        public FastOutput println() {
            cache.append(System.lineSeparator());
            return this;
        }

        public FastOutput flush() {
            try {
                os.append(cache);
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                os.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }

    static class SequenceUtils {
        public static void swap(int[] data, int i, int j) {
            int tmp = data[i];
            data[i] = data[j];
            data[j] = tmp;
        }

        public static void reverse(int[] data, int l, int r) {
            while (l < r) {
                swap(data, l, r);
                l++;
                r--;
            }
        }

    }

    static class FastInput {
        private final InputStream is;
        private byte[] buf = new byte[1 << 20];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

    }

    static class IntegerBIT {
        private int[] data;
        private int n;

        public IntegerBIT(int n) {
            this.n = n;
            data = new int[n + 1];
        }

        public int query(int i) {
            int sum = 0;
            for (; i > 0; i -= i & -i) {
                sum += data[i];
            }
            return sum;
        }

        public void update(int i, int mod) {
            if (i <= 0) {
                return;
            }
            for (; i <= n; i += i & -i) {
                data[i] += mod;
            }
        }

        public String toString() {
            StringBuilder builder = new StringBuilder();
            for (int i = 1; i <= n; i++) {
                builder.append(query(i) - query(i - 1)).append(' ');
            }
            return builder.toString();
        }

    }
}

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

public class c {

	public static void main(String[] args) throws IOException {
		FastScanner in = new FastScanner(System.in);
		int n = in.nextInt();
		int m = in.nextInt();

		int[] ls = new int[n];
		int[] rs = new int[n];
		int[] ds = new int[m + 1];
		for (int i = 0; i < n; i++) {
			ls[i] = in.nextInt() - 1;
			rs[i] = in.nextInt() - 1;
			ds[ls[i]]++;
			ds[rs[i] + 1]--;
		}
		int[] arr = new int[m];

		int d = 0;
		for (int i = 0; i < m; i++) {
			d += ds[i];
			arr[i] = d;
		}
		int[] rev = new int[m];
		for (int i = 0; i < m; i++)
			rev[i] = arr[m - i - 1];
		int[] lis = getLis(arr);
		int[] lisRev = getLis(rev);
		//System.out.println(Arrays.toString(lis));
		//System.out.println(Arrays.toString(lisRev));
		int max = 0;
		for (int i = 0; i < m; i++)
			max = Math.max(max, lis[i] + lisRev[m - i - 1] - 1);
		System.out.println(max);
	}

	static int[] getLis(int[] arr) {
		int n = arr.length;
		int[] lis = new int[n];
		TreeMap<Integer, Integer> tm = new TreeMap<>();
		tm.put(0, 0);
		for (int i = 0; i < n; i++) {
			int v = arr[i];
			int k = tm.floorKey(v);
			int amt = tm.get(k) + 1;
			lis[i] = amt;
			tm.put(v, amt);
			while (true) {
				Integer k2 = tm.higherKey(v);
				if (k2 == null) break;
				Integer amt2 = tm.get(k2);
				if (amt2 > amt) break;
				tm.remove(k2);
			}
		}
		return lis;
	}
	static class FastScanner {
	    BufferedReader br;
	    StringTokenizer st;

	    public FastScanner(InputStream i) {
	        br = new BufferedReader(new InputStreamReader(i));
	        st = new StringTokenizer("");
	    }

	    public String next() throws IOException {
	        if(st.hasMoreTokens())
	            return st.nextToken();
	        else
	            st = new StringTokenizer(br.readLine());
	        return next();
	    }

	    public int nextInt() throws IOException {
	        return Integer.parseInt(next());
	    }
	    //#
	    public long nextLong() throws IOException {
	        return Long.parseLong(next());
	    }
	    public double nextDouble() throws IOException {
	        return Double.parseDouble(next());
	    }
	    //$
	}
}

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

//http://codeforces.com/contest/930/problem/C
public class TeodorIsNotALiar {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int segments = sc.nextInt();
		int points = sc.nextInt();
		int[] segStarts = new int[points + 1];
		for (int i = 0; i < segments; i++) {
			segStarts[sc.nextInt() - 1]++;
			segStarts[sc.nextInt()]--;
		}
		sc.close();
		int[] pointCount = new int[points];
		int curSegs = 0;
		for (int i = 0; i < pointCount.length; i++) {
			curSegs += segStarts[i];
			pointCount[i] = curSegs;
		}
		int[] maxIncSeqLen = getMaxIncreasingSequencesForEachLength(pointCount);
		flip(pointCount);
		int[] maxRevIncSeqLen = getMaxIncreasingSequencesForEachLength(pointCount);
		flip(maxRevIncSeqLen);
		int best = maxIncSeqLen[0] + maxRevIncSeqLen[1];
		for (int i = 0; i < pointCount.length - 1; i++) {
			best = Math.max(best, maxIncSeqLen[i] + maxRevIncSeqLen[i+1]);
		}
		System.out.println(best);
	}

	private static void flip(int[] a) {
		for (int i = 0; i < a.length / 2; i++) {
			int temp = a[i];
			a[i] = a[a.length - i - 1];
			a[a.length - i - 1] = temp;
		}
	}

	private static int[] getMaxIncreasingSequencesForEachLength(int[] pointCount) {
		int[] incSeqLen = new int[pointCount.length];
		incSeqLen[0] = 1;
		double[] minForLen = new double[pointCount.length + 1];
		minForLen[0] = 0;
		minForLen[1] = pointCount[0];
		for (int i = 2; i < minForLen.length; i++) {
			minForLen[i] = Double.POSITIVE_INFINITY;
		}
		int highestNonInf = 1;
		for (int i = 1; i < incSeqLen.length; i++) {
			// ensures no duplicates
			double valToSearchFor = pointCount[i] + (double) (i) / 1000000;
			int lowInd = Math.abs(Arrays.binarySearch(minForLen, valToSearchFor)+1);
			minForLen[lowInd] = Math.min(minForLen[lowInd], valToSearchFor);
			highestNonInf = Math.max(highestNonInf, lowInd);
			incSeqLen[i] = highestNonInf;
		}
		return incSeqLen;
	}
}

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

public class C {

	public static void solve(FastScanner fs) {
		int nSegments=fs.nextInt();
		int size=fs.nextInt();
		int[] delta=new int[size+1];
		for (int i=0; i<nSegments; i++) {
			int start=fs.nextInt()-1;
			int end=fs.nextInt()-1;
			delta[start]++;
			delta[end+1]--;
		}
		int[] heights=new int[size];
		heights[0]=delta[0];
		for (int i=1; i<size; i++)
			heights[i]=heights[i-1]+delta[i];

		int[] heightsRev=new int[size];
		for (int i=0; i<size; i++)
			heightsRev[i]=heights[size-i-1];

		int[] empty=new int[nSegments+1];
		SegTree dp=new SegTree(empty, 0, nSegments);
		SegTree dpRev=new SegTree(empty, 0, nSegments);
		int[] maxAllRegular=new int[size+1], maxAllRev=new int[size+1];
		for (int i=0; i<size; i++) {
			int height=heights[i];
			int prevMax=dp.maxQuery(0, height);
			dp.update(height, prevMax+1);
			maxAllRegular[i+1]=dp.maxQuery(0, nSegments);

			int heightRev=heightsRev[i];
			prevMax=dpRev.maxQuery(0, heightRev);
			dpRev.update(heightRev, prevMax+1);
			maxAllRev[size-i-1]=dpRev.maxQuery(0, nSegments);
		}

		int ans=0;
		for (int i=0; i<=size; i++) {
			ans=Math.max(ans, maxAllRegular[i]+maxAllRev[i]);
		}
		System.out.println(ans);
	}

	private static void print(SegTree tree, int n) {
		String s="";
		for (int i=0; i<n; i++)
			s+=tree.maxQuery(i, i)+" ";
		System.out.println(s);
	}

	static class SegTree {
		SegTree lChild, rChild;
		int max;
		int l, r, mid;

		public SegTree(int[] a, int l, int r) {
			this.l=l;
			this.r=r;
			if (l==r) {
				max=a[l];
				return;
			}
			mid=(l+r)/2;
			lChild=new SegTree(a, l, mid);
			rChild=new SegTree(a, mid+1, r);
			max=Math.max(lChild.max, rChild.max);
		}

		public void update(int index, int to) {
			if (index<l||index>r)
				return;
			if (l==r) {
				max=to;
				return;
			}
			max=Math.max(max, to);
			if (index<=mid) {
				lChild.update(index, to);
			}
			else {
				rChild.update(index, to);
			}
		}

		public int maxQuery(int left, int right) {
			if (right<l||left>r)
				return -10000;
			if (left<=l&&right>=r) {
				return max;
			}
			return Math.max(lChild.maxQuery(left, right), rChild.maxQuery(left, right));
		}
	}

	public static void main(String[] args) throws NumberFormatException, IOException {
		FastScanner scanner = new FastScanner(System.in);
		solve(scanner);
	}

	private static class FastScanner {
		BufferedReader br;
		StringTokenizer st;
		public FastScanner(InputStream in) {
			br = new BufferedReader(new InputStreamReader(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;
		}
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++)
				a[i]=nextInt();
			return a;
		}
		long[] readLongArray(int n) {
			long[] a=new long[n];
			for (int i=0; i<n; i++)
				a[i]=nextLong();
			return a;
		}
	}
}

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

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

    static class TaskC {
        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            int n = in.nextInt(), m = in.nextInt();

            int[] l = new int[n], r = new int[n];
            int[] cnt = new int[m + 1];
            for (int i = 0; i < n; i++) {
                l[i] = in.nextInt() - 1;
                r[i] = in.nextInt() - 1;
                cnt[l[i]]++;
                cnt[r[i] + 1]--;
            }
            for (int i = 1; i <= m; i++) {
                cnt[i] += cnt[i - 1];
            }

            int[] dpLeft = new int[m], dpRight = new int[m];
            BITMax tree = new BITMax(n);
            for (int i = 0; i < m; i++) {
                int get = tree.get(cnt[i]);
                if (get == Integer.MIN_VALUE) {
                    get = 0;
                }
                dpLeft[i] = get + 1;
                tree.update(cnt[i], dpLeft[i]);
            }
            tree = new BITMax(n);
            for (int i = m - 1; i >= 0; i--) {
                int get = tree.get(cnt[i]);
                if (get == Integer.MIN_VALUE) {
                    get = 0;
                }
                dpRight[i] = get + 1;
                tree.update(cnt[i], dpRight[i]);
            }
            int max = 0;
            for (int i = 0; i < m; i++) {
                max = Math.max(max, dpLeft[i] + dpRight[i] - 1);
            }
            out.println(max);
        }

    }

    static class BITMax {
        int[] tree;

        public BITMax(int size) {
            tree = new int[size];
            Arrays.fill(tree, Integer.MIN_VALUE);
        }

        public int get(int pos) {
            int result = Integer.MIN_VALUE;
            while (pos >= 0) {
                result = Math.max(result, tree[pos]);
                pos = (pos & (pos + 1)) - 1;
            }
            return result;
        }

        public void update(int pos, int val) {
            while (pos < tree.length) {
                tree[pos] = Math.max(tree[pos], val);
                pos |= pos + 1;
            }
        }

    }

    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
        }

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

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

    }
}

import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;

import static java.lang.Math.*;

public class Main implements Runnable {
    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        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 nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
        public int nextInt()
        {
            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 long nextLong()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            long 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 double nextDouble()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.')
            {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.')
            {
                c = read();
                double m = 1;
                while (!isSpaceChar(c))
                {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        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 boolean isSpaceChar(int c)
        {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

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

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

    }
    public static void main(String args[]) throws Exception
    {
        new Thread(null, new Main(),"Main",1<<26).start();
    }
    public void run() {
        InputReader sc= new InputReader(System.in);
        PrintWriter w= new PrintWriter(System.out);

        int m = sc.nextInt();
        int n = sc.nextInt();

        int a[] = new int[n];

        for(int i = 0; i < m; ++i) {
            int l = sc.nextInt() - 1;
            int r = sc.nextInt();
            a[l]++;
            if(r < n)
                a[r]--;
        }

        for(int i = 1; i < n; ++i)
            a[i] = a[i - 1] + a[i];

        int smallestEle[] = new int[n];
        smallestEle[0] = a[0];
        int lis[] = new int[n];
        lis[0] = 1;
        int curLength = 1;

        for(int i = 1; i < n; ++i) {
            //w.println("LIS : " + i + " " + curLength + " " + smallestEle[curLength - 1]);
            if(a[i] >= smallestEle[curLength - 1])
                smallestEle[curLength++] = a[i];
            else {
                int start = 0, end = curLength - 2, pos = -1;
                while(start <= end) {
                    int mid = (start + end) >> 1;
                    if(a[i] >= smallestEle[mid]) {
                        pos = mid;
                        start = mid + 1;
                    }
                    else
                        end = mid - 1;
                }
                smallestEle[pos + 1] = a[i];
            }
            lis[i] = curLength;
        }

        smallestEle = new int[n];
        smallestEle[0] = a[n - 1];
        int lds[] = new int[n];
        lds[n - 1] = 1;
        curLength = 1;

        for(int i = n - 2; i >= 0; --i) {
            if(a[i] >= smallestEle[curLength - 1])
                smallestEle[curLength++] = a[i];
            else {
                int start = 0, end = curLength - 2, pos = -1;
                while(start <= end) {
                    int mid = (start + end) >> 1;
                    if(a[i] >= smallestEle[mid]) {
                        pos = mid;
                        start = mid + 1;
                    }
                    else
                        end = mid - 1;
                }
                smallestEle[pos + 1] = a[i];
            }
            lds[i] = curLength;
        }

        /*for(int i = 0; i < n; ++i)
            w.print(a[i] + " ");
        w.println();
        for(int i = 0; i < n; ++i)
            w.print(lis[i] + " ");
        w.println();
        for(int i = 0; i < n; ++i)
            w.print(lds[i] + " ");
        w.println(); */

        int maxid = lds[0];

        for(int i = 1; i < n; ++i)
            maxid = max(maxid, lis[i - 1] + lds[i]);

        w.print(maxid);

        w.close();
    }
}

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

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

	static class TaskC {
		public void solve(int testNumber, FastScanner in, PrintWriter out) {
			int n = in.nextInt();
			int m = in.nextInt();
			int[] cov = new int[m];
			int[] add = new int[m];
			for (int i = 0; i < n; i++) {
				int l = in.nextInt() - 1;
				int r = in.nextInt() - 1;
				++add[l];
				if (r + 1 < m) {
					--add[r + 1];
				}
			}
			{
				int curCov = 0;
				for (int i = 0; i < m; i++) {
					curCov += add[i];
					cov[i] = curCov;
				}
			}

			int[] l = calcLIS(cov);
			reverse(cov);
			int[] r = calcLIS(cov);
			reverse(r);

			int[] prefixL = l.clone();
			for (int i = 1; i < m; i++) {
				prefixL[i] = Math.max(prefixL[i], prefixL[i - 1]);
			}

			int[] suffixR = r.clone();
			for (int i = m - 2; i >= 0; i--) {
				suffixR[i] = Math.max(suffixR[i], suffixR[i + 1]);
			}

			int ans = 0;
			for (int i = 0; i + 1 < m; i++) {
				ans = Math.max(ans, prefixL[i] + suffixR[i + 1]);
			}
			out.println(ans);
		}

		private void reverse(int[] a) {
			for (int i = 0; ; i++) {
				int j = a.length - i - 1;
				if (i >= j) {
					break;
				}
				int t = a[i];
				a[i] = a[j];
				a[j] = t;
			}
		}

		private int[] calcLIS(int[] a) {
			int n = a.length;
			int[] lis = new int[n];
			int[] smallest = new int[n];
			int k = 0;
			for (int i = 0; i < n; i++) {
				int l = -1;
				int r = k;
				while (r - l > 1) {
					int m = (l + r) / 2;
					if (smallest[m] <= a[i]) {
						l = m;
					} else {
						r = m;
					}
				}
				smallest[r] = a[i];
				if (r == k) {
					++k;
				}
				lis[i] = r + 1;
			}
			return lis;
		}

	}

	static class FastScanner {
		private BufferedReader in;
		private StringTokenizer st;

		public FastScanner(InputStream stream) {
			in = new BufferedReader(new InputStreamReader(stream));
		}

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

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

	}
}

//package round468;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class C {
	InputStream is;
	PrintWriter out;
	String INPUT = "";

	void solve()
	{
		int n = ni(), m = ni();
		int[] imos = new int[m+1];
		for(int i = 0;i < n;i++){
			int l = ni()-1, r = ni()-1;
			imos[l]++;
			imos[r+1]--;
		}
		for(int i = 0;i < m;i++){
			imos[i+1] += imos[i];
		}
		long[] a = new long[m];
		for(int i = 0;i < m;i++){
			a[i] = (long)imos[i]<<32|i;
		}
		int[] lisf = lis(a);
		for(int i = 0;i < m;i++){
			a[i] = (long)imos[m-1-i]<<32|i;
		}
		int[] lisb = lis(a);
		int ans = 0;
		for(int i = 0;i < m;i++){
			ans = Math.max(ans, lisf[i] + lisb[m-1-i] - 1);
		}
		out.println(ans);
	}

	public static int[] lis(long[] in)
	{
		int n = in.length;
		int ret = 0;
		long[] h = new long[n + 1];
		Arrays.fill(h, Long.MIN_VALUE / 2);
		int[] lens = new int[n];
		for(int i = 0;i < n;i++){
			int ind = Arrays.binarySearch(h, 0, ret + 1, in[i]);
			if(ind < 0){
				ind = -ind-2;
				h[ind+1] = in[i];
				if(ind >= ret)ret++;
			}
			lens[i] = ret;
		}
		return lens;
	}

	void run() throws Exception
	{
		is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

		long s = System.currentTimeMillis();
		solve();
		out.flush();
		tr(System.currentTimeMillis()-s+"ms");
	}

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

	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;

	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}

	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }

	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}

	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}

	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}

	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}

		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}

		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
	private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class f {
	public static void main(String[] Args) throws Exception {
		FS sc = new FS(System.in);
		PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		int m = sc.nextInt();
		int n = sc.nextInt();
		int[] counts = new int[n];

		segs[] ses = new segs[m + m + n];
		int c = 0;
		for (int i = 0; i < m; i++) {
			int s = sc.nextInt() - 1;
			int e = sc.nextInt() - 1;
			ses[c++] = new segs(s, 1);
			ses[c++] = new segs(e, -1);
		}
		for (int i = 0; i < n; i++) {
			ses[c++] = new segs(i, 0);
		}
		Arrays.sort(ses);
		int cur = 0;
		for (int i = 0; i < c; i++) {
			if (ses[i].type == 0)
				counts[ses[i].s] = cur;
			cur += ses[i].type;
		}

		int[] ans1 = new int[n];
		Pair1[] ps = new Pair1[n];
		for (int i = 0; i < n; i++)
			ps[i] = new Pair1(i, counts[i]);
		IntervalTree it = new IntervalTree(n + 1);
		Arrays.sort(ps);
		for (int i = 0; i < n; i++) {
			ans1[ps[i].loc] = it.query(0, ps[i].loc);
			it.inc(ps[i].loc, ps[i].loc, ans1[ps[i].loc] + 1);
		}

		it = new IntervalTree(n + 1);

		int[] ans2 = new int[n];
		Pair2[] ps2 = new Pair2[n];
		for (int i = 0; i < n; i++)
			ps2[i] = new Pair2(i, counts[i]);
		Arrays.sort(ps2);
		for (int i = 0; i < n; i++) {
			ans2[ps2[i].loc] = it.query(ps2[i].loc, n);
			it.inc(ps2[i].loc, ps2[i].loc, ans2[ps2[i].loc] + 1);
		}

		int tans = 0;
		for (int i = 0; i < n; i++)
			if (tans < ans1[i] + ans2[i] + 1)
				tans = ans1[i] + ans2[i] + 1;
		out.println(tans);
		out.close();
	}

	public static class IntervalTree {
		static final int NULL_VALUE = -987654321; // change this
		int[] lo, hi, carry, val;

		public IntervalTree(int N) {
			lo = new int[4 * (N + 1)];
			hi = new int[4 * (N + 1)];
			carry = new int[4 * (N + 1)];
			val = new int[4 * (N + 1)];
			init(1, 0, N - 1);
		}

		public void inc(int l, int h, int v) {
			inc(1, l, h, v);
		}

		public int query(int l, int h) {
			return query(1, l, h);
		}

		private int aggregate(int x, int y) {
			return Math.max(x, y);
		} // change this

		private void init(int i, int l, int h) {
			lo[i] = l;
			hi[i] = h;
			if (l == h)
				return;
			int m = (l + h) / 2;
			init(2 * i, l, m);
			init(2 * i + 1, m + 1, h);
		}

		private void inc(int i, int l, int h, int v) {
			if (l <= lo[i] && hi[i] <= h) { // incorporation
				carry[i] += v;
				val[i] += v * (hi[i] - lo[i] + 1); // change this
			} else if (l <= hi[i] && lo[i] <= h) {
				push(i);
				inc(2 * i, l, h, v);
				inc(2 * i + 1, l, h, v);
				val[i] = aggregate(val[2 * i], val[2 * i + 1]);
			}
		}

		private int query(int i, int l, int h) {
			if (h < lo[i] || hi[i] < l)
				return NULL_VALUE;
			if (l <= lo[i] && hi[i] <= h)
				return val[i];
			push(i);
			return aggregate(query(2 * i, l, h), query(2 * i + 1, l, h));
		}

		private void push(int i) {
			if (carry[i] == 0)
				return;
			inc(2 * i, lo[2 * i], hi[2 * i], carry[i]);
			inc(2 * i + 1, lo[2 * i + 1], hi[2 * i + 1], carry[i]);
			carry[i] = 0;
		}
	}

	public static class segs implements Comparable<segs> {
		int s, type;

		segs(int x, int y) {
			s = x;
			type = y;
		}

		public int compareTo(segs o) {
			if (s == o.s)
				return o.type - type;
			return s - o.s;
		}
	}

	public static class Pair1 implements Comparable<Pair1> {
		int loc, h;

		Pair1(int x, int y) {
			loc = x;
			h = y;
		}

		public int compareTo(Pair1 o) {
			if (h == o.h)
				return loc - o.loc;
			return h - o.h;
		}
	}

	public static class Pair2 implements Comparable<Pair2> {
		int loc, h;

		Pair2(int x, int y) {
			loc = x;
			h = y;
		}

		public int compareTo(Pair2 o) {
			if (h == o.h)
				return o.loc - loc;
			return h - o.h;
		}
	}

	public static class FS {
		BufferedReader br;
		StringTokenizer st;

		FS(InputStream in) throws Exception {
			br = new BufferedReader(new InputStreamReader(in));
			st = new StringTokenizer(br.readLine());
		}

		FS(File f) throws Exception {
			br = new BufferedReader(new FileReader(f));
			st = new StringTokenizer(br.readLine());
		}

		String next() throws Exception {
			if (st.hasMoreTokens())
				return st.nextToken();
			st = new StringTokenizer(br.readLine());
			return next();
		}

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

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

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

import java.util.ArrayList;
import java.util.Scanner;
import java.lang.StringBuilder;
import java.util.Arrays;
import java.util.Stack;
import java.util.TreeMap;
public class Test10   {
    public static void main(String[] Args)   {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] a = new int[m + 1];
        int[] diff = new int[m + 2]; // diff[l] = cnt[l] - cnt[l - 1]
        for (int i = 0; i < n; i++)   {
            int l = sc.nextInt();
            int r = sc.nextInt();
            diff[l]++;
            diff[r + 1]--;
        }
        a[0] = diff[0];
        for (int i = 1; i <= m; i++)
            a[i] = a[i - 1] + diff[i];
        //dp + IST
        int[] mx1 = new int[m + 1];
        int pw2 = 1 << 20;
        int[] ist = new int[pw2 + 1]; //ist : 1100000 - 1000000
        for (int i = 0; i <= m; i++)   {
            //IST search
            int j = a[i];
            int k = 0;
            pw2 = 1 << 20;
            int mx = ist[0];
            for (int l = 19; l >= 0; l--)   {
                pw2 /= 2;
                if (j >= pw2)   {
                    k += pw2;
                    mx = Math.max(mx, ist[k]);
                    j -= pw2;
                }
            }
            mx1[i] = mx + 1;
            //update ist
            int s = a[i];
            ist[s] = Math.max(ist[s], mx1[i]);
            int huge = 1;
            for (int l = 0; l <= 20; l++)   {
                huge *= 2;
                if (s % 2 == 0)   {
                    int o = (s + 1) << l;
                    ist[o] = Math.max(ist[o], mx1[i]);
                }
                s = s / 2;
            }
        }
        //do everything backward
        int[] b = new int[m + 1];
        for (int i = 0; i <= m; i++) b[i] = a[m - i];
        //dp + IST
        int[] mx2 = new int[m + 1];
        pw2 = 1 << 20;
        ist = new int[pw2 + 1]; //ist : 1100000 - 1000000
        for (int i = 0; i <= m; i++)   {
            //IST search
            int j = b[i];
            int k = 0;
            pw2 = 1 << 20;
            int mx = ist[0];
            for (int l = 19; l >= 0; l--)   {
                pw2 /= 2;
                if (j >= pw2)   {
                    k += pw2;
                    mx = Math.max(mx, ist[k]);
                    j -= pw2;
                }
            }
            mx2[i] = mx + 1;
            //update ist
            int s = b[i];
            ist[s] = Math.max(ist[s], mx2[i]);
            int huge = 1;
            for (int l = 0; l <= 20; l++)   {
                huge *= 2;
                if (s % 2 == 0)   {
                    int o = (s + 1) << l;
                    ist[o] = Math.max(ist[o], mx2[i]);
                }
                s = s / 2;
            }
        }
        //get result
        int mx = 0;
        for (int i = 0; i <= m; i++)
            if (mx1[i] + mx2[m - i] > mx) mx = mx1[i] + mx2[m - i];
        System.out.println(mx - 2);
   }
}

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

public class utkarsh {

    InputStream is;
    PrintWriter out;

    int bs(int[] a, int l, int r, int x) {
        int m;
        while(l <= r) {
            m = (l + r) >> 1;
            if(a[m] > x)    r = m-1;
            else    l = m+1;
        }
        return l;
    }

    void solve() {
        //Enter code here utkarsh
        int n = ni();
        int m = ni();
        int a[] = new int[m+1];
        for(int i = 0; i < n; i++) {
            a[ni() - 1]++;
            a[ni()]--;
        }
        for(int i = 1; i <= m; i++) {
            a[i] += a[i-1];
        }
        if(m <= 2) {
            out.println(m);
            return;
        }
        int d[] = new int[m];
        int x[] = new int[m];
        int y[] = new int[m];
        x[0] = y[m-1] = 1;
        int z;

        d[0] = a[0];
        z = 1;
        for(int i = 1; i < m; i++) {
            int j = bs(d, 0, z-1, a[i]);
            if(j == z)  d[z++] = a[i];
            else    d[j] = a[i];
            x[i] = z;
        }

        d[0] = a[m-1];
        z = 1;
        for(int i = m-2; i >= 0; i--) {
            int j = bs(d, 0, z-1, a[i]);
            if(j == z)  d[z++] = a[i];
            else    d[j] = a[i];
            y[i] = z;
        }

        int ans = 0;
        for(int i = 0; i < m; i++) {
            //out.println(x[i] +" "+ y[i]);
            ans = Math.max(ans, x[i] + y[i] - 1);
        }
        out.println(ans);
    }

    //---------- I/O Template ----------

    public static void main(String[] args) { new utkarsh().run(); }
    void run() {
        is = System.in;
        out = new PrintWriter(System.out);
        solve();
        out.flush();
    }

    byte input[] = new byte[1024];
    int len = 0, ptr = 0;

    int readByte() {
        if(ptr >= len) { ptr = 0;
            try { len = is.read(input); }
            catch(IOException e) { throw new InputMismatchException(); }
            if(len <= 0) { return -1; }
        } return input[ptr++];
    }
    boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); }
    int skip() {
        int b = readByte();
        while(b != -1 && isSpaceChar(b)) { b = readByte(); }
        return b;
    }

    int ni() {
        int n = 0, b = readByte();
        boolean minus = false;
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
        if(b == '-') { minus = true; b = readByte(); }
        if(b == -1) { return -1; }  //no input
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
        return minus ? -n : n;
    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            if (m == 1) {
                out.println(0);
                return;
            }
            int[] psum = new int[m + 1];
            for (int i = 0; i < n; i++) {
                int a = in.nextInt() - 1, b = in.nextInt();
                psum[a]++;
                psum[b]--;
            }
            for (int i = 1; i <= m; i++) {
                psum[i] += psum[i - 1];
            }
            Debug.print(psum);
            int[] a1 = lis(psum);
            for (int i = 1; i <= m; i++) {
                a1[i] = Math.max(a1[i - 1], a1[i]);
            }
            AUtils.reverse(psum);
            int[] a2 = lis(psum);
            for (int i = 1; i <= m; i++) {
                a2[i] = Math.max(a2[i - 1], a2[i]);
            }
            Debug.print(a1, a2);
            int best = Math.max(a1[m], a2[m]);
            for (int sp = 0; sp < m; sp++) {
                best = Math.max(best, a1[sp] + a2[m - sp - 1]);
            }
            out.println(best - 1);
        }

        public static int[] lis(int[] a) {
            int n = a.length;
            int[] tail = new int[n];

            int len = 0;
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
//            if (a[i] == 0) continue;
                int pos = lower_bound(a, tail, len, a[i] + 1);
                if (pos == len) ++len;
                res[i] = len;
                tail[pos] = i;
            }
            return res;
        }

        static int lower_bound(int[] a, int[] tail, int len, int key) {
            int lo = -1;
            int hi = len;
            while (hi - lo > 1) {
                int mid = (lo + hi) >>> 1;
                if (a[tail[mid]] < key) {
                    lo = mid;
                } else {
                    hi = mid;
                }
            }
            return hi;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1 << 16];
        private int curChar;
        private int numChars;

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

        public int read() {
            if (this.numChars == -1) {
                throw new InputMismatchException();
            } else {
                if (this.curChar >= this.numChars) {
                    this.curChar = 0;

                    try {
                        this.numChars = this.stream.read(this.buf);
                    } catch (IOException var2) {
                        throw new InputMismatchException();
                    }

                    if (this.numChars <= 0) {
                        return -1;
                    }
                }

                return this.buf[this.curChar++];
            }
        }

        public int nextInt() {
            int c;
            for (c = this.read(); isSpaceChar(c); c = this.read()) {
                ;
            }

            byte sgn = 1;
            if (c == 45) {
                sgn = -1;
                c = this.read();
            }

            int res = 0;

            while (c >= 48 && c <= 57) {
                res *= 10;
                res += c - 48;
                c = this.read();
                if (isSpaceChar(c)) {
                    return res * sgn;
                }
            }

            throw new InputMismatchException();
        }

        public static boolean isSpaceChar(int c) {
            return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
        }

    }

    static 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 close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class Debug {
        public static boolean DEBUG;

        static {
            try {
                DEBUG = System.getProperty("user.dir").contains("Dropbox");
            } catch (Exception e) {
                DEBUG = false;
            }
        }

        private static ArrayList<String> getParams() {
            StackTraceElement[] t = Thread.currentThread().getStackTrace();
            StackTraceElement up = t[3];

            ArrayList<String> ret = new ArrayList<>();
            String qqq = up.toString();
            ret.add("." + up.getMethodName() + qqq.substring(qqq.indexOf("("), qqq.length()));
            try {
                BufferedReader br = new BufferedReader(new FileReader(
                        new File("src/" + up.getClassName().replaceAll("\\.", "/") + ".java")));
                int g = up.getLineNumber();
                while (--g > 0) br.readLine();
                String q = br.readLine();
                String[] ss = q.split("Debug\\.print\\(");
                String[] qq = ss[1].substring(0, ss[1].lastIndexOf(")")).split(",");
                for (int i = 0; i < qq.length; i++) {
                    ret.add(qq[i].trim());
                }
            } catch (Exception e) {
            }
            for (int i = 0; i < 100; i++) ret.add("???");
            return ret;
        }

        private static String toString(Object o) {
            if (o == null) {
                return "null";
            } else if (o instanceof Object[]) {
                return Arrays.toString((Object[]) o);
            } else if (o instanceof char[]) {
                return new String((char[]) o);
            } else if (o instanceof int[]) {
                return Arrays.toString((int[]) o);
            } else if (o instanceof long[]) {
                return Arrays.toString((long[]) o);
            } else if (o instanceof double[]) {
                return Arrays.toString((double[]) o);
            } else if (o instanceof boolean[]) {
                return Arrays.toString((boolean[]) o);
            } else {
                return o.toString();
            }
        }

        public static void print(Object... x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.print(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.print(s.get(i + 1) + " = " + toString(x[i]));
                if (i != x.length - 1) System.out.print(", ");
            }
            System.out.println();
        }

        public static void print(int[] x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.println(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.println(s.get(1) + "[" + i + "] = " + x[i]);
            }
            System.out.println("***");
        }

    }

    static class AUtils {
        public static void reverse(int[] arr) {
            for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
                int t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
            }
        }

    }
}

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

public class C {

	int[][] go(int[] arr) {
		int n = arr.length;
		int[] aux = new int[n + 1];
		Arrays.fill(aux, Integer.MAX_VALUE);
		aux[0] = Integer.MIN_VALUE;

		int[] justAns = new int[n];
		int[] takeAns = new int[n];
		int ans = 0;

		for (int i = 0; i < n; i++) {
			int x = arr[i];
			int low = 0; // <=
			int high = ans + 1; // >
			while (high - low > 1) {
				int mid = (low + high) >> 1;
				if (aux[mid] > x) {
					high = mid;
				} else {
					low = mid;
				}
			}
			aux[high] = x;
			if (high > ans) {
				ans = high;
			}
			justAns[i] = ans;
			takeAns[i] = high;
		}
		return new int[][] { justAns, takeAns };
	}

	void submit() {
		int n = nextInt();
		int m = nextInt();
		int[] arr = new int[m];
		for (int i = 0; i < n; i++) {
			int x = nextInt() - 1;
			int y = nextInt() - 1;
			arr[x]++;
			if (y < m - 1) {
				arr[y + 1]--;
			}
		}

		for (int i = 1; i < m; i++) {
			arr[i] += arr[i - 1];
		}

//		System.err.println(Arrays.toString(arr));
		int[][] inc = go(arr);
		int[][] dec = go(reverse(arr));
		dec[0] = reverse(dec[0]);
		dec[1] = reverse(dec[1]);

		// System.err.println(Arrays.toString(inc));
		// System.err.println(Arrays.toString(dec));

		int ans = 0;
		for (int mid = 0; mid < m; mid++) {
			int skipMid = 0;
			skipMid += mid > 0 ? inc[0][mid - 1] : 0;
			skipMid += mid < m - 1 ? dec[0][mid + 1] : 0;
			ans = Math.max(ans, skipMid);

			int takeMid = inc[1][mid] + dec[1][mid] - 1;
			ans = Math.max(ans, takeMid);
		}

		out.println(ans);
	}

	int[] reverse(int[] arr) {
		int[] ret = new int[arr.length];
		for (int i = 0, j = arr.length - 1; i < arr.length; i++, j--) {
			ret[j] = arr[i];
		}
		return ret;
	}

	void preCalc() {

	}

	void test() {

	}

	void stress() {
		for (int tst = 0;; tst++) {
			if (false) {
				throw new AssertionError();
			}
			System.err.println(tst);
		}
	}

	C() throws IOException {
		is = System.in;
		out = new PrintWriter(System.out);
		preCalc();
		submit();
		// stress();
		// test();
		out.close();
	}

	static final Random rng = new Random();
	static final int C = 5;

	static int rand(int l, int r) {
		return l + rng.nextInt(r - l + 1);
	}

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

	private InputStream is;
	PrintWriter out;

	private byte[] buf = new byte[1 << 14];
	private int bufSz = 0, bufPtr = 0;

	private int readByte() {
		if (bufSz == -1)
			throw new RuntimeException("Reading past EOF");
		if (bufPtr >= bufSz) {
			bufPtr = 0;
			try {
				bufSz = is.read(buf);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
			if (bufSz <= 0)
				return -1;
		}
		return buf[bufPtr++];
	}

	private boolean isTrash(int c) {
		return c < 33 || c > 126;
	}

	private int skip() {
		int b;
		while ((b = readByte()) != -1 && isTrash(b))
			;
		return b;
	}

	String nextToken() {
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while (!isTrash(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	String nextString() {
		int b = readByte();
		StringBuilder sb = new StringBuilder();
		while (!isTrash(b) || b == ' ') {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

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

	char nextChar() {
		return (char) skip();
	}

	int nextInt() {
		int ret = 0;
		int b = skip();
		if (b != '-' && (b < '0' || b > '9')) {
			throw new InputMismatchException();
		}
		boolean neg = false;
		if (b == '-') {
			neg = true;
			b = readByte();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				ret = ret * 10 + (b - '0');
			} else {
				if (b != -1 && !isTrash(b)) {
					throw new InputMismatchException();
				}
				return neg ? -ret : ret;
			}
			b = readByte();
		}
	}

	long nextLong() {
		long ret = 0;
		int b = skip();
		if (b != '-' && (b < '0' || b > '9')) {
			throw new InputMismatchException();
		}
		boolean neg = false;
		if (b == '-') {
			neg = true;
			b = readByte();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				ret = ret * 10 + (b - '0');
			} else {
				if (b != -1 && !isTrash(b)) {
					throw new InputMismatchException();
				}
				return neg ? -ret : ret;
			}
			b = readByte();
		}
	}
}

import java.text.DecimalFormat;
import java.util.*;
import java.io.*;

public class Main {
    private static final int MAXST = (1 << 17) << 1;
    private static int [] ST = new int[MAXST];

    private static void init(int cur,int s,int e) {
        ST[cur] = 0;
        if (s == e) return;
        int m = (s+e) >> 1,left = 2*cur+1,right = left + 1;
        init(left,s,m);
        init(right,m+1,e);
    }
    private static int query(int cur,int s,int e,int l,int r) {
        if (l <= s && e <= r) return ST[cur];
        int m = (s+e) >> 1,left = 2*cur+1,right = left + 1;
        if (r <= m) return query(left,s,m,l,r);
        if (m < l) return query(right,m+1,e,l,r);
        return Math.max(query(left,s,m,l,m),query(right,m+1,e,m+1,r));
    }
    private static void update(int cur,int s,int e,int p,int v) {
        ST[cur] = Math.max(ST[cur],v);
        if (s == e) return;
        int m = (s+e) >> 1,left = 2*cur+1,right = left + 1;
        if (p <= m) update(left,s,m,p,v);
        else update(right,m+1,e,p,v);
    }

    public static void main(String[] args) throws Exception{
        IO io = new IO(null,null);
        int n = io.getNextInt(),m = io.getNextInt();
        int [] cnt = new int[m + 2];
        for (int i = 0;i < n;i++) {
            int l = io.getNextInt(),r = io.getNextInt();
            cnt[l]++;
            cnt[r + 1]--;
        }
        for (int i = 0,v = 0;i <= m+1;i++)
            cnt[i] = v += cnt[i];

//        System.err.println(Arrays.toString(cnt));

        int [] prv = new int[m+1];
        int [] nxt = new int[m+2];

        init(0,0,n);
        for (int i = 1;i <= m;i++) {
            prv[i] = query(0,0,n,0,cnt[i]) + 1;
            update(0,0,n,cnt[i],prv[i]);
        }
        init(0,0,n);
        for (int i = m;i > 0;i--) {
            nxt[i] = query(0,0,n,0,cnt[i]) + 1;
            update(0,0,n,cnt[i],nxt[i]);
        }
        int ans = 0;
        for (int i = 1;i <= m;i++)
            ans = Math.max(ans,prv[i] + nxt[i] - 1);
        io.println(ans);
        io.close();
    }

}

class IO{
    private BufferedReader br;
    private StringTokenizer st;
    private PrintWriter writer;
    private String inputFile,outputFile;

    public boolean hasMore() throws IOException{
        if(st != null && st.hasMoreTokens()) return true;
        if(br != null && br.ready()) return true;
        return false;
    }

    public String getNext() throws FileNotFoundException, IOException{
        while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }

    public String getNextLine() throws FileNotFoundException, IOException{
        return br.readLine().trim();
    }

    public int getNextInt() throws FileNotFoundException, IOException{
        return Integer.parseInt(getNext());
    }
    public long getNextLong() throws FileNotFoundException, IOException{
        return Long.parseLong(getNext());
    }

    public void print(double x,int num_digits) throws  IOException{
        writer.printf("%." + num_digits + "f" ,x);
    }
    public void println(double x,int num_digits) throws  IOException{
        writer.printf("%." + num_digits + "f\n" ,x);
    }
    public void print(Object o) throws  IOException{
        writer.print(o.toString());
    }

    public void println(Object o) throws  IOException{
        writer.println(o.toString());
    }
    public IO(String x,String y) throws FileNotFoundException, IOException{
        inputFile = x;
        outputFile = y;
        if(x != null) br = new BufferedReader(new FileReader(inputFile));
        else br = new BufferedReader(new InputStreamReader(System.in));
        if(y != null) writer = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
        else writer = new PrintWriter(new OutputStreamWriter(System.out));
    }

    protected void close() throws IOException{
        br.close();
        writer.close();
    }
    public void outputArr(Object [] A) throws IOException{
        int L = A.length;
        for (int i = 0;i < L;i++) {
            if(i > 0) writer.print(" ");
            writer.print(A[i]);
        }
        writer.print("\n");
    }
}

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

public class Div1_468C {

	static int[] tree = new int[4 * 100_010];

	static void update(int nI, int cL, int cR, int uI, int uV) {
		if (cL == cR) {
			tree[nI] = Math.max(tree[nI], uV);
		} else {
			int mid = (cL + cR) >> 1;
			if (uI <= mid) {
				update(nI * 2, cL, mid, uI, uV);
			} else {
				update(nI * 2 + 1, mid + 1, cR, uI, uV);
			}
			tree[nI] = Math.max(tree[nI * 2], tree[nI * 2 + 1]);
		}
	}

	static int query(int nI, int cL, int cR, int qL, int qR) {
		if (qR < cL || cR < qL) {
			return 0;
		}
		if (qL <= cL && cR <= qR) {
			return tree[nI];
		}
		int mid = (cL + cR) >> 1;
		return Math.max(query(nI * 2, cL, mid, qL, qR), query(nI * 2 + 1, mid + 1, cR, qL, qR));
	}

	public static void main(String[] args) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		StringTokenizer inputData = new StringTokenizer(reader.readLine());
		int nS = Integer.parseInt(inputData.nextToken());
		int N = Integer.parseInt(inputData.nextToken());

		int[] delta = new int[N + 2];

		for (int i = 0; i < nS; i++) {
			inputData = new StringTokenizer(reader.readLine());
			int l = Integer.parseInt(inputData.nextToken());
			int r = Integer.parseInt(inputData.nextToken());
			delta[l]++;
			delta[r + 1]--;
		}

		int[] array = new int[N + 1];

		for (int i = 1; i <= N; i++) {
			array[i] = array[i - 1] + delta[i];
		}

		int[] lDecreasing = new int[N + 1];

		for (int i = N; i >= 1; i--) {
			int nLongest = query(1, 0, 100_000, 0, array[i]) + 1;
			lDecreasing[i] = nLongest;
			update(1, 0, 100_000, array[i], nLongest);
		}

		Arrays.fill(tree, 0);

		int ans = 0;
		for (int i = 1; i <= N; i++) {
			int nLongest = query(1, 0, 100_000, 0, array[i]) + 1;
			ans = Math.max(ans, nLongest + lDecreasing[i] - 1);
			update(1, 0, 100_000, array[i], nLongest);
		}
		printer.println(ans);
		printer.close();
	}
}

import java.util.*;

import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;

public class C {
	FastScanner in;
	PrintWriter out;
	boolean systemIO = true;

	public static class Point implements Comparable<Point> {
		int x;
		int type;

		public Point(int x, int type) {
			this.x = x;
			this.type = type;
		}

		@Override
		public int compareTo(Point o) {
			if (x != o.x) {
				return x - o.x;
			}
			return type - o.type;
		}

	}

	public void solve() {
		int n = in.nextInt();
		int m = in.nextInt();
		ArrayList<Point> list = new ArrayList<>();
		for (int i = 0; i < n; i++) {
			list.add(new Point(in.nextInt() - 1, 0));
			list.add(new Point(in.nextInt() - 1, 1));
		}
		Collections.sort(list);
		Point[] p = new Point[2 * n];
		for (int i = 0; i < p.length; i++) {
			p[i] = list.get(i);
		}
		int j = 0;
		int[] h = new int[m];
		int th = 0;
		for (int i = 0; i < h.length; i++) {
			while (p[j].type == 0 && p[j].x == i) {
				th++;
				j++;
			}
			h[i] = th;
			while (j < 2 * n && p[j].x == i) {
				th--;
				j++;
			}
			if (j == 2 * n) {
				break;
			}
		}
		int[] dpUp = new int[m + 1];
		int[] maxUp = new int[m + 1];
		for (int i = 1; i < maxUp.length; i++) {
			dpUp[i] = Integer.MAX_VALUE;
		}
		for (int i = 0; i < h.length; i++) {
			int l = -1;
			int r = m;
			while (l + 1 < r) {
				int med = (l + r) / 2;
				if (dpUp[med] > h[i]) {
					r = med;
				} else {
					l = med;
				}
			}
			maxUp[i + 1] = maxUp[i];
			if (dpUp[r] == Integer.MAX_VALUE) {
				maxUp[i + 1]++;
			}
			dpUp[r] = h[i];
		}
		int[] dpDown = new int[m + 1];
		int[] maxDown = new int[m + 1];
		for (int i = 1; i < maxUp.length; i++) {
			dpDown[i] = Integer.MAX_VALUE;
		}
		for (int i = h.length - 1; i >= 0; i--) {
			int l = -1;
			int r = m;
			while (l + 1 < r) {
				int med = (l + r) / 2;
				if (dpDown[med] > h[i]) {
					r = med;
				} else {
					l = med;
				}
			}
			maxDown[h.length - i] = maxDown[h.length - 1 - i];
			if (dpDown[r] == Integer.MAX_VALUE) {
				maxDown[h.length - i]++;
			}
			dpDown[r] = h[i];
		}
		int ans = 0;
		for (int i = 0; i < maxDown.length; i++) {
			ans = Math.max(ans, maxUp[i] + maxDown[m - i]);
		}
		out.print(ans);
	}

	public void run() {
		try {
			if (systemIO) {
				in = new FastScanner(System.in);
				out = new PrintWriter(System.out);
			} else {
				in = new FastScanner(new File("segments.in"));
				out = new PrintWriter(new File("segments.out"));
			}
			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(File f) {
			try {
				br = new BufferedReader(new FileReader(f));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

		FastScanner(InputStream f) {
			br = new BufferedReader(new InputStreamReader(f));
		}

		String nextLine() {
			try {
				return br.readLine();
			} catch (IOException e) {
				return null;
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				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());
		}

	}

	// AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
	public static void main(String[] arg) {
		new C().run();
	}
}

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

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

	FastReader in = new FastReader();
	PrintWriter out = new PrintWriter(System.out);
	void run(){
		work();
		out.flush();
	}
	long mod=1000000007;
	long gcd(long a,long b) {
		return a==0?b:b>=a?gcd(b%a,a):gcd(b,a);
	}
	void work() {
		int m=in.nextInt(),n=in.nextInt();
		int[] A=new int[n];
		for(int i=0;i<m;i++) {
			int s=in.nextInt()-1,e=in.nextInt()-1;
			A[s]++;
			if(e+1<n)A[e+1]--;
		}
		for(int i=1;i<n;i++) {
			A[i]+=A[i-1];
		}
		int[] r1=find1(A);
		int[] r2=find2(A);
		int max=0;
		for(int i=0;i<n-1;i++) {
			max=Math.max(max, r1[i]+r2[i+1]);
		}
		out.println(max);
	}
	private int[] find1(int[] A) {
		int n=A.length;
		int[] rec=new int[n];
		int[] ret=new int[n];
		int p=0;
		for(int i=0;i<n;i++) {
			int l=0,r=p;
			while(l<r) {
				int m=(l+r)/2;
				if(rec[m]<=A[i]) {//1:递增
					l=m+1;
				}else {
					r=m;
				}
			}
			rec[l]=A[i];
			if(l==p) {
				p++;
			}
			ret[i]=p;
		}
		return ret;
	}
	private int[] find2(int[] A) {
		int n=A.length;
		int[] rec=new int[n];
		int[] ret=new int[n];
		int p=0;
		for(int i=n-1;i>=0;i--) {
			int l=0,r=p;
			while(l<r) {
				int m=(l+r)/2;
				if(rec[m]<=A[i]) {//1:递增
					l=m+1;
				}else {
					r=m;
				}
			}
			rec[l]=A[i];
			if(l==p) {
				p++;
			}
			ret[i]=p;
		}
		return ret;
	}
}

class FastReader
{
	BufferedReader br;
	StringTokenizer st;

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

	public String next()
	{
		while(st==null || !st.hasMoreElements())//回车，空行情况
		{
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}

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

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

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

public class C {

	static int m;
	static int[] inc1, inc2;

	static void lis(int[] A, int n, int[] ans)		// Can be implemented with TreeSet (lower, remove, add)
	{
		ArrayList<Double> L = new ArrayList<Double>();

		for(int i = 0; i < n; ++i)
		{
			int pos = Collections.binarySearch(L, A[i] + 0.5);
			if (pos < 0) pos = -(pos + 1);

			if(pos >= L.size()) L.add(A[i]+0.0);
			else                 L.set(pos, A[i]+0.0);

			ans[i]  = pos + 1;

		}

	}

	static class SegmentTree {		// 1-based DS, OOP

		int N; 			//the number of elements in the array as a power of 2 (i.e. after padding)
		int[] array, sTree, lazy;

		SegmentTree(int[] in)
		{
			array = in; N = in.length - 1;
			sTree = new int[N<<1];		//no. of nodes = 2*N - 1, we add one to cross out index zero
			lazy = new int[N<<1];
			build(1,1,N);
		}

		void build(int node, int b, int e)	// O(n)
		{
			if(b == e)
				sTree[node] = array[b];
			else
			{
				int mid = b + e >> 1;
			build(node<<1,b,mid);
			build(node<<1|1,mid+1,e);
			sTree[node] = sTree[node<<1]+sTree[node<<1|1];
			}
		}

		void update_range(int i, int j, int val)		// O(log n)
		{
			update_range(1,1,N,i,j,val);
		}

		void update_range(int node, int b, int e, int i, int j, int val)
		{
			if(i > e || j < b)
				return;
			if(b >= i && e <= j)
			{
				sTree[node] += (e-b+1)*val;
				lazy[node] += val;
			}
			else
			{
				int mid = b + e >> 1;
			propagate(node, b, mid, e);
			update_range(node<<1,b,mid,i,j,val);
			update_range(node<<1|1,mid+1,e,i,j,val);
			sTree[node] = sTree[node<<1] + sTree[node<<1|1];
			}

		}
		void propagate(int node, int b, int mid, int e)
		{
			lazy[node<<1] += lazy[node];
			lazy[node<<1|1] += lazy[node];
			sTree[node<<1] += (mid-b+1)*lazy[node];
			sTree[node<<1|1] += (e-mid)*lazy[node];
			lazy[node] = 0;
		}

		int query(int i, int j)
		{
			return query(1,1,N,i,j);
		}

		int query(int node, int b, int e, int i, int j)	// O(log n)
		{
			if(i>e || j <b)
				return 0;
			if(b>= i && e <= j)
				return sTree[node];
			int mid = b + e >> 1;
			propagate(node, b, mid, e);
			int q1 = query(node<<1,b,mid,i,j);
			int q2 = query(node<<1|1,mid+1,e,i,j);
			return q1 + q2;

		}
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = sc.nextInt();
		int m = sc.nextInt();

		int M = 1; while(M < m) M <<= 1; //padding

		int[] in = new int[M + 1];
		SegmentTree st = new SegmentTree(in);

		for (int i = 0; i < n; i++) {
			st.update_range(sc.nextInt(), sc.nextInt(), 1);
		}

		int[] arr1 = new int[m];
		int[] arr2 = new int[m];
		for (int i = 0; i < m; i++) {
			arr1[i] = st.query(i + 1, i + 1);
			arr2[m - i - 1] = arr1[i];
		}

		inc1 = new int[m];
		inc2 = new int[m];

		lis(arr1, m, inc1);
		lis(arr2, m, inc2);

		int ans = 0;

		for (int i = 0; i < m; i++) {
			ans = Math.max(inc1[i] + inc2[m - i - 1] - 1, ans);
		}

		out.println(ans);
		out.close();

	}

	static class Scanner
	{
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s)
		{
			br = new BufferedReader(new InputStreamReader(s));
		}

		public String next() throws IOException
		{
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

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

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

		public String nextLine() throws IOException
		{
			return br.readLine();
		}

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

		public boolean ready() throws IOException
		{
			return br.ready();
		}
	}
}

import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.util.Map.Entry;

import static java.lang.Math.*;

public class C extends PrintWriter {

    void run() {
        int n = nextInt();
        int m = nextInt();

        int[] l = new int[n], r = new int[n];

        for (int i = 0; i < n; i++) {
            l[i] = nextInt() - 1;
            r[i] = nextInt() - 1;
        }

        int[] cnt = new int[m];

        int[] op = new int[m];
        int[] cl = new int[m];

        for (int i = 0; i < n; i++) {
            ++op[l[i]];
            ++cl[r[i]];
        }

        for (int c = 0, x = 0; x < m; x++) {
            c += op[x];
            cnt[x] = c;
            c -= cl[x];
        }

        int[] h = new int[n + 1];

        for (int x = 0; x < m; x++) {
            ++h[cnt[x]];
        }

        int ans = 0;

        for (int i = 0; i <= n; i++) {
            ans = max(ans, h[i]);
        }

        int[] d = new int[m + 2];

        int[] dpl = new int[m];
        Arrays.fill(d, n + 3);
        d[0] = 0;

        for (int x = 0; x < m; x++) {
            int lp = 0, rp = m + 1;

            while (rp - lp > 1) {
                int mp = (lp + rp) / 2;
                if (d[mp] <= cnt[x]) {
                    lp = mp;
                } else {
                    rp = mp;
                }
            }

            d[rp] = cnt[x];
            dpl[x] = rp;
        }

        int[] dpr = new int[m];
        Arrays.fill(d, n + 3);
        d[0] = 0;

        for (int x = m - 1; x >= 0; x--) {
            int lp = 0, rp = m + 1;

            while (rp - lp > 1) {
                int mp = (lp + rp) / 2;
                if (d[mp] <= cnt[x]) {
                    lp = mp;
                } else {
                    rp = mp;
                }
            }

            d[rp] = cnt[x];
            dpr[x] = rp;
        }

        for (int x = 0; x < m; x++) {
            ans = max(ans, dpl[x] + dpr[x] - 1);
        }

        println(ans);
    }

    boolean skip() {
        while (hasNext()) {
            next();
        }
        return true;
    }

    int[][] nextMatrix(int n, int m) {
        int[][] matrix = new int[n][m];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                matrix[i][j] = nextInt();
        return matrix;
    }

    String next() {
        while (!tokenizer.hasMoreTokens())
            tokenizer = new StringTokenizer(nextLine());
        return tokenizer.nextToken();
    }

    boolean hasNext() {
        while (!tokenizer.hasMoreTokens()) {
            String line = nextLine();
            if (line == null) {
                return false;
            }
            tokenizer = new StringTokenizer(line);
        }
        return true;
    }

    int[] nextArray(int n) {
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = nextInt();
        }
        return array;
    }

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

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

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

    String nextLine() {
        try {
            return reader.readLine();
        } catch (IOException err) {
            return null;
        }
    }

    public C(OutputStream outputStream) {
        super(outputStream);
    }

    static BufferedReader reader;
    static StringTokenizer tokenizer = new StringTokenizer("");
    static Random rnd = new Random();
    static boolean OJ;

    public static void main(String[] args) throws IOException {
        OJ = System.getProperty("ONLINE_JUDGE") != null;
        C solution = new C(System.out);
        if (OJ) {
            reader = new BufferedReader(new InputStreamReader(System.in));
            solution.run();
        } else {
            reader = new BufferedReader(new FileReader(new File(C.class.getName() + ".txt")));
            long timeout = System.currentTimeMillis();
            while (solution.hasNext()) {
                solution.run();
                solution.println();
                solution.println("----------------------------------");
            }
            solution.println("time: " + (System.currentTimeMillis() - timeout));
        }
        solution.close();
        reader.close();
    }
}

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

public class Main {
    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(System.out);
        //Scanner sc = new Scanner();
        Reader in = new Reader();
        Main solver = new Main();
        solver.solve(out, in);
        out.flush();
        out.close();

    }

    //<>
    //

    static int maxn = (int)1e5*5+5;
    static long mod=998244353 ;
    static int n,m,k;

    static int INF = (int)-1e9;
    static int[] lazy,arr;
    static int[][] tree;

    void solve(PrintWriter out, Reader in) throws IOException{
        n = in.nextInt();
        m = in.nextInt();

        arr = new int[m+1];
        lazy = new int[(m+1)*4];
        int u,v;
        for(int i=0;i<n;i++){
            u = in.nextInt();
            v = in.nextInt();
            update(1,1,m,u,v);
        }
        relax(1,1,m);

        int[][] dp = new int[n+1][2];
        tree = new int[(n+1)*4][2];

        for(int i=1;i<=m;i++){
            int temp = Math.max(query(1,0,n,arr[i],n,0),query(1,0,n,arr[i],n,1));

            if(temp+1>dp[arr[i]][1]){
                dp[arr[i]][1] = temp+1;
                upd(1,0,n,arr[i],temp+1,1);
            }

            temp = query(1,0,n,0,arr[i],0);
            if(temp+1>dp[arr[i]][0]){
                dp[arr[i]][0] = temp+1;
                upd(1,0,n,arr[i],temp+1,0);
            }
        }

        int ans = 0;
        for(int i=0;i<=n;i++)
            for(int j=0;j<=1;j++)
                ans = Math.max(ans,dp[i][j]);
        out.println(ans);

    }

    static void upd(int idx,int s,int e,int d,int val,int type){
        if(s==e){
            tree[idx][type] = val;
            return;
        }

        int mid = (s+e)/2;
        if(s<=d && d<=mid) upd(idx*2,s,mid,d,val,type);
        else upd(idx*2+1,mid+1,e,d,val,type);

        tree[idx][type] = Math.max(tree[idx*2][type],tree[idx*2+1][type]);
        return;
    }

    static int query(int idx,int s,int e,int l,int r,int type){
        if(r<s || e<l) return 0;

        if(l<=s && e<=r) return tree[idx][type];

        int mid = (s+e)/2;
        return Math.max(query(idx*2,s,mid,l,r,type),query(idx*2+1,mid+1,e,l,r,type));
    }

    static void update(int idx,int s,int e,int l,int r){
        if(r<s || e<l) return;

        if(s==e){
            arr[s]++;
            return;
        }

        if(l<=s && e<=r) {
            int mid = (s+e)/2;
            lazy[idx*2]++;
            lazy[idx*2+1]++;
            return;
        }

        int mid = (s+e)/2;
        update(idx*2,s,mid,l,r);
        update(idx*2+1,mid+1,e,l,r);

        return;
    }

    static void relax(int idx,int s,int e){
        if(lazy[idx]!=0){
            if(s!=e){
                lazy[idx*2]+=lazy[idx];
                lazy[idx*2+1]+=lazy[idx];
            }else{
                arr[s]+=lazy[idx];
            }
        }
        if(s==e) return;

        int mid = (s+e)/2;
        relax(idx*2,s,mid);
        relax(idx*2+1,mid+1,e);
        return;
    }

    static class Reader {

        private InputStream mIs;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public Reader() {
            this(System.in);
        }

        public Reader(InputStream is) {
            mIs = is;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();

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

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

        public 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();
        }

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

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long 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 int nextInt() {
            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) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

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

    }
}

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

/*
TASK: CFC
LANG: JAVA
 */
public class CFC {
    static int n , m;
    static long[] cnt;
    static long[] inv;
    public static void main(String[] args) throws IOException {
        FastScanner in = new FastScanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        inv = new long[m];
        cnt = new long[m];
        for(int i = 0;i < n; i++){
            cnt[in.nextInt()-1]++;
            int a = in.nextInt();
            if(a < m) {
                cnt[a]--;
            }
        }
        for(int i = 1;i < m;i++){
            cnt[i] += cnt[i-1];
        }
        for(int i = 0;i < m; i++){
            inv[i] = cnt[m-1-i];
        }
        LongIncreasingSub front = new LongIncreasingSub(cnt);
        LongIncreasingSub back = new LongIncreasingSub(inv);
        int max = Math.max(front.dp[0] , back.dp[0]);
        for(int i = 0;i< m-1; i++){
            max = Math.max(front.dp[i] + back.dp[m-2-i] , max);
        }
        System.out.println(max);
    }
    static class LongIncreasingSub {
        int n, cnt, ret;
        long[] a;
        int[] dp;
        HashSet<Long> set = new HashSet<Long>();
        long[] ind;
        SegTree s;

        public LongIncreasingSub(long[] li) {
            a = li;
            for (long k : a) set.add(k);
            ind = new long[set.size()];
            for (long a : set) ind[cnt++] = a;
            Arrays.sort(ind);
            dp = new int[a.length];
            n = a.length;
            s = new SegTree(new int[ind.length]);
            int k, l;
            for (int i = 0; i < n; i++) {
                k = ind(a[i]);
                dp[i] = 1 + s.max(0, k);
                l = s.max(k, k);
                if (l < dp[i]) s.increment(k, k, dp[i] - l);
            }
            for (int i = 0; i < n; i++) ret = Math.max(ret, dp[i]);
        }

        class SegTree {
            int n;
            int[] lo, hi, min, delta, val;

            public SegTree(int[] a) {
                this.n = a.length;
                val = a;
                lo = new int[4 * n + 1];
                hi = new int[4 * n + 1];
                min = new int[4 * n + 1];
                delta = new int[4 * n + 1];
                init(1, 0, n - 1);

            }

            public int init(int i, int a, int b) {
                lo[i] = a;
                hi[i] = b;
                if (a == b) {
                    min[i] = val[a];
                    return min[i];
                }

                int m = (a + b) / 2;
                min[i] = Math.max(init(2 * i, a, m), init(2 * i + 1, m + 1, b));
                return min[i];
            }

            public void prop(int i) {
                delta[2 * i] += delta[i];
                delta[2 * i + 1] += delta[i];
                delta[i] = 0;
            }

            public void update(int i) {
                min[i] = Math.max(min[2 * i] + delta[2 * i], min[2 * i + 1] + delta[2 * i + 1]);
            }

            public void increment(int min, int max, int val) {
                increment(1, min, max, val);
            }

            public void increment(int ind, int a, int b, int val) {
                if (hi[ind] < a || lo[ind] > b) return;
                if (a <= lo[ind] && b >= hi[ind]) {
                    delta[ind] += val;
                    return;
                }
                prop(ind);
                increment(2 * ind, a, b, val);
                increment(2 * ind + 1, a, b, val);
                update(ind);
            }

            public int max(int min, int max) {
                return rMin(1, min, max);
            }

            public int rMin(int ind, int a, int b) {
                if (hi[ind] < a || lo[ind] > b) return 0;
                if (a <= lo[ind] && b >= hi[ind]) {
                    return min[ind] + delta[ind];
                }
                prop(ind);
                int minLeft = rMin(2 * ind, a, b);
                int minRight = rMin(2 * ind + 1, a, b);
                update(ind);
                return Math.max(minLeft, minRight);
            }
        }

        public int ind(long a) {
            return Arrays.binarySearch(ind, a);
        }
    }
    private static class FastScanner {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

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

        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;
        }

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

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

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

        public 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();
        }

        public 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();
        }
    }
}

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

public class E {

	public static class RT {

		int st;
		int en;
		int val;
		RT l;
		RT r;
		boolean term;

		public RT(int st, int en) {
			this.st = st;
			this.en = en;
			this.val = 0;
			if(st == en) term = true;
			else {
				term = false;
				int hal = (st + en)/2;
				l = new RT(st, hal);
				r = new RT(hal+1, en);
			}
		}

		public void inc(int st, int en, int amn) {
			//System.err.println(st + " " + en + " ");
			//System.err.println("comp to " + this.st + " " + this.en);
			if(st == this.st && en == this.en) {
				this.val += amn;
				return;
			} else if(en <= l.en) {
				l.inc(st, en, amn);
			} else if(st >= r.st) {
				r.inc(st, en, amn);
			} else {
				l.inc(st, l.en, amn);
				r.inc(r.st, en, amn);
			}
		}

		public int val(int i) {
			//System.out.println("I am " + st + " " + en);
			//System.out.println("My val is " + val);
			//System.out.println();
			if(this.term) return val;
			else if(i <= l.en) return val + l.val(i);
			else return val + r.val(i);
		}
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
		String[] ree = in.readLine().split("\\s++");
		int n = Integer.parseInt(ree[0]);
		int r = Integer.parseInt(ree[1]);
		int no = r;
		RT rt = new RT(1, r);
		while(n-- > 0) {
			ree = in.readLine().split("\\s++");
			int a = Integer.parseInt(ree[0]);
			int b = Integer.parseInt(ree[1]);
			rt.inc(a, b, 1);
		}
		int[] arr = new int[no];
		for(int i = 0; i < no; i++) {
			arr[i] = rt.val(i + 1);
			//System.err.println(i + "> " + arr[i]);
		}
		//System.out.println("RT DONE");
		ArrayList<Integer> list = new ArrayList<Integer>();
		int[] lisrec = new int[no];
		boolean[] lisinc = new boolean[no];
		for(int i = 0; i < no; i++) {
			//System.err.println("list " + i);
			if(i == 0 || arr[i] >= list.get(list.size()-1)) {
				list.add(arr[i]);
				lisinc[i] = true;
			}
			else {
				//binary search
				int min = 0;
				int max = list.size()-1;
				while(min < max) {
					int test = (min + max)/2;
					if(list.get(test) <= arr[i]) {
						min = test + 1;
					} else {
						max = test;
					}
				}
				list.set(max, arr[i]);
			}
			lisrec[i] = list.size();
		}
		ArrayList<Integer> blist = new ArrayList<Integer>();
		int[] blisrec = new int[no];
		boolean[] blisinc = new boolean[no];
		for(int i = no - 1; i >= 0; i--) {
			//System.err.println("blist " + i);
			if(i ==(no - 1) || arr[i] >= blist.get(blist.size()-1)) {
				blist.add(arr[i]);
				blisinc[i] = true;
			}
			else {
				//System.err.println("bs");
				//binary search
				int min = 0;
				int max = blist.size()-1;
				while(min < max) {
					int test = (min + max)/2;
				//System.err.println(min + " m|M " + max + " : " + test);
					if(blist.get(test) <= arr[i]) {
						min = test + 1;
					} else {
						max = test;
					}
				}
				blist.set(max, arr[i]);
			}
			blisrec[i] = blist.size();
		}
		int ans = 0;
		for(int i = 0; i < no; i++) {
			int c = lisrec[i] + blisrec[i];
			if(lisinc[i] && blisinc[i]) c--;
			if(c > ans) ans = c;
		}
		out.write(ans + "\n");
		out.flush();
	}
}

import java.io.*;
import java.util.Arrays;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.function.Function;

public class E {
    String fileName = "E";

    public int n, m;

    public static class Event implements Comparable<Event> {
        public static final int OPEN_TYPE = 0;
        public static final int POINT_TYPE = 1;
        public static final int CLOSE_TYPE = 2;

        public int x, type;

        public Event(int x, int type) {
            this.x = x;
            this.type = type;
        }

        @Override
        public int compareTo(Event event) {
            if (Integer.compare(x, event.x) != 0)
                return Integer.compare(x, event.x);
            else return Integer.compare(type, event.type);
        }
    }

    public static final class Node {
        public int l, r;
        public int max, maxInd;

        public Node leftNode, rightNode;

        public Node(int l, int r) {
            this.l = l;
            this.r = r;
        }
    }

    public void relax(Node root) {
        if (root.leftNode.max > root.rightNode.max) {
            root.max = root.leftNode.max;
            root.maxInd = root.leftNode.maxInd;
        } else {
            root.max = root.rightNode.max;
            root.maxInd = root.rightNode.maxInd;
        }
    }

    public Node build(int l, int r, Function<Integer, Integer> getItem) {
        Node res = new Node(l, r);
        if (l == r) {
            res.max = getItem.apply(l);
            res.maxInd = l;
        } else {
            int m = (r + l) / 2;
            res.leftNode = build(l, m, getItem);
            res.rightNode = build(m + 1, r, getItem);
            relax(res);
        }
        return res;
    }

    public void set(Node root, int ind, int val) {
        if (ind < root.l || ind > root.r)
            return;
        if (root.l == root.r) {
            root.max = val;
            return;
        }
        set(root.leftNode, ind, val);
        set(root.rightNode, ind, val);
        relax(root);
    }

    public static class QueryResult {
        public int max, maxInd;

        public QueryResult(int max, int maxInd) {
            this.max = max;
            this.maxInd = maxInd;
        }

        public QueryResult(QueryResult first, QueryResult second) {
            if (first.max > second.max) {
                this.max = first.max;
                this.maxInd = first.maxInd;
            } else {
                this.max = second.max;
                this.maxInd = second.maxInd;
            }
        }
    }

    public QueryResult query(Node root, int l, int r) {
        if (r == -1)
            return new QueryResult(0, -1);
        if (r < root.l || l > root.r)
            return new QueryResult(Integer.MIN_VALUE, -1);
        if (l <= root.l && r >= root.r) {
            return new QueryResult(root.max, root.maxInd);
        }
        return new QueryResult(query(root.leftNode, l, r), query(root.rightNode, l, r));
    }

    public int[] getLens(int[] a) {
        Node root = build(0, n, ind -> 0);
        int[] len = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            int curVal = a[i];
            int prevLen = query(root, 0, curVal).max;
            len[i] = prevLen + 1;
            if (query(root, curVal, curVal).max < len[i])
                set(root, curVal, len[i]);
        }
        return len;
    }

    public void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        Event[] events = new Event[2 * n + m];
        for (int i = 1; i <= m; i++) {
            events[i - 1] = new Event(i, Event.POINT_TYPE);
        }
        for (int i = 0; i < n; i++) {
            int l = nextInt();
            int r = nextInt();
            events[m + 2 * i] = new Event(l, Event.OPEN_TYPE);
            events[m + 2 * i + 1] = new Event(r, Event.CLOSE_TYPE);
        }
        Arrays.sort(events);
        int[] cnt = new int[m];
        int balance = 0;
        for (int i = 0; i < events.length; i++) {
            Event curEvent = events[i];
            if (curEvent.type == Event.OPEN_TYPE) {
                balance++;
            } else if (curEvent.type == Event.CLOSE_TYPE) {
                balance--;
            } else if (curEvent.type == Event.POINT_TYPE) {
                cnt[curEvent.x - 1] = balance;
            }
        }
        int[] lenUp = getLens(cnt);
        int[] newCnt = new int[cnt.length];
        for (int i = 0; i < cnt.length; i++) {
            newCnt[i] = cnt[cnt.length - i - 1];
        }
        int[] lenDown = getLens(newCnt);
        int[] revLenDown = new int[lenDown.length];
        for (int i = 0; i < lenDown.length; i++) {
            revLenDown[i] = lenDown[lenDown.length - i - 1];
        }
        int max = 0;
        for (int i = 0; i < lenDown.length; i++) {
            int cur = lenUp[i] + revLenDown[i] - 1;
            max = Math.max(max, cur);
        }
        out.print(max);
    }

    public void run() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);

            solve();

            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    BufferedReader br;
    StringTokenizer in;
    PrintWriter out;

    public String nextToken() throws IOException {
        while (in == null || !in.hasMoreTokens()) {
            in = new StringTokenizer(br.readLine());
        }
        return in.nextToken();
    }

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

    public double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    public long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    public static void main(String[] args) throws IOException {
        Locale.setDefault(Locale.US);
        new E().run();
    }
}

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

import static java.lang.Math.*;

public class Main {
    BufferedReader br;
    StringTokenizer in;
    PrintWriter pw;
    Random r;
    int INF = (int) 1e9;
    int mod = (int) (1e9 + 7);
    //int p = 26;
    ArrayList<Integer> g[];

    void end(int a, int n) {
        if ((int) Math.pow(2, a) != n) pw.print(a);
        else pw.print("Final!");
        pw.close();
        System.exit(0);
    }

    class node {
        int val;
        int m;
        int size;
        int prior;
        node L;
        node R;

        public node(int v) {
            val = v;
            prior = r.nextInt();
            size = 1;
            L = null;
            R = null;
        }
    }

    int sgs(node c) {
        if (c == null) return 0;
        return c.size;
    }

    int sgm(node c) {
        if (c == null) return INF;
        return c.m;
    }

    void relax(node c) {
        if (c == null) return;
        c.size = 1 + sgs(c.L) + sgs(c.R);
    }

    class pair implements Comparable<pair> {
        int a;
        int b;

        public pair(int aa, int bb) {
            a = aa;
            b = bb;
        }

        @Override
        public int compareTo(pair o) {
            if (a != o.a) return a - o.a;
            return b - o.b;
        }
    }

    class road {
        int to;
        int w;

        public road(int tto, int ww) {
            to = tto;
            w = ww;
        }

        public road() throws IOException {
            to = nextInt();
            w = nextInt();
        }
    }

    void readG(int n, int m) throws IOException {
        g = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            g[i] = new ArrayList<>();
        }
        for (int i = 0; i < m; i++) {
            int a = nextInt() - 1;
            int b = nextInt() - 1;
            int w = nextInt();
//            g[a].add(new road(b, w));
//            g[b].add(new road(a, w));
        }
    }

    int p[];
    int d[];

    void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        event[] p = new event[n * 2];
        for (int i = 0; i < n; i++) {
            int l = nextInt();
            int r = nextInt();
            p[i] = new event(l, 1);
            p[i + n] = new event(r, -1);
        }
        int q = p.length;
        int b = 0;
        Arrays.sort(p);
        int bal[] = new int[m + 1];
        int prev = 0;
        for (int i = 0; i < q; i++) {
            event a = p[i];
            if (a.t == -1) {
                for (int j = prev + 1; j <= a.time; j++) {
                    bal[j] = b;
                }
                prev = a.time;
            } else {
                for (int j = prev + 1; j < a.time; j++) {
                    bal[j] = b;
                }
                prev = a.time - 1;
            }
            b += a.t;
        }
//        for (int i = 1; i <= m; i++) {
//            pw.print(bal[i]);
//        }
        int revbal[] = new int[m + 1];
        for (int i = 1; i <= m; i++) {
            revbal[i] = bal[m + 1 - i];
        }
        int nvp[] = getnvp(bal);
        int rnvp[] = getnvp(revbal);
        int ans = 0;
        for (int i = 1; i <= m; i++) {
            int z = nvp[i];
            int w = rnvp[m - i];
            int sum = z + w;
            ans = max(ans, sum);
        }
        pw.print(ans);
    }

    int[] getnvp(int a[]) {
        int n = a.length;
        int d[] = new int[n + 1];
        int ans[] = new int[n];
        Arrays.fill(d, INF);
        d[0] = -INF;
        int last = 0;
        for (int i = 1; i < n; i++) {
            ans[i] = ans[i - 1];
            int c = a[i];
            int l = 0;
            int r = last + 1;
            while (l + 1 != r) {
                int m = (l + r) / 2;
                if (d[m] <= c) l = m;
                else r = m;
            }
            if (d[r] == INF) {
                ans[i]++;
                last++;
            }
            d[r] = min(d[r], c);
        }
        return ans;
    }

    class event implements Comparable<event> {
        int time;
        int t;

        public event(int ttime, int tt) {
            time = ttime;
            t = tt;
        }

        @Override
        public int compareTo(event o) {
            if (time != o.time) return time - o.time;
            else return o.t - t;
        }
    }

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

    void run() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        pw = new PrintWriter(new OutputStreamWriter(System.out));
        r = new Random(5);
        solve();
        pw.close();
    }

    String nextToken() throws IOException {
        while (in == null || !in.hasMoreTokens()) in = new StringTokenizer(br.readLine());
        return in.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }
}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
    ArrayList<ArrayList<Integer>> q;
    boolean[] used;

    public void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        int[] a = new int[m + 1];
        for (int i = 0; i < n; i++) {
            a[nextInt() - 1]++;
            a[nextInt()]--;
        }
        for (int i = 1; i < m; i++) {
            a[i] += a[i - 1];
        }
        int[] d = new int[m + 1];
        Arrays.fill(d, Integer.MAX_VALUE);
        int k = 0;
        int[] v1 = new int[m + 2];
        int[] v2 = new int[m + 2];
        for (int i = 0; i < m; i++) {
            int l = 0;
            int r = m;
            while (r - l > 1) {
                int m1 = (r + l) / 2;
                if (d[m1] > a[i]) {
                    r = m1;
                } else {
                    l = m1;
                }
            }
            if (d[l] <= a[i]) {
                l++;
            }
            if (d[l] == Integer.MAX_VALUE){
                k++;
            }
            d[l] = a[i];
            v1[i] = k;
        }
        k = 0;
        int max = 0;
        Arrays.fill(d,Integer.MAX_VALUE);
        for (int i = m - 1; i >= 0; i--) {
            int l = 0;
            int r = m;
            while (r - l > 1) {
                int m1 = (r + l) / 2;
                if (d[m1] > a[i]) {
                    r = m1;
                } else {
                    l = m1;
                }
            }
            if (d[l] <= a[i]) {
                l++;
            }
            if (d[l] == Integer.MAX_VALUE){
                k++;
            }
            d[l] = a[i];
            v2[i] = k;
            max = Math.max(max,v1[i] + v2[i + 1]);
        }
        max = Math.max(max,v2[0]);
        out.print(max);
    }

    BufferedReader br;
    StringTokenizer sc;
    PrintWriter out;

    String nextToken() throws IOException {
        while (sc == null || !sc.hasMoreTokens()) {
            try {
                sc = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                sc = null;
            }
        }
        return sc.nextToken();
    }

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

    public Long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    public Double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

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

    public void run() throws IOException {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
            solve();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}

//package codeforces.R468;

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

public class C
{
	static int[] left;
	static int[] right;
	static int[] t;
	static int[] arr;
	public static void main(String[] args) throws IOException
	{
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);

		int n = sc.nextInt();
		int m = sc.nextInt();
		int[] drawing = new int[m+2];
		for (int i = 0; i < n; i++)
		{
			drawing[sc.nextInt()]++;
			drawing[sc.nextInt()+1]--;
		}

		for (int i = 1; i < drawing.length; i++)
			drawing[i] += drawing[i-1];

		arr = new int[m];
		for (int i = 0; i < arr.length; i++)
			arr[i] = drawing[i+1];

//		System.err.println(Arrays.toString(arr));
		left = new int[m];
		right = new int[m];

		int end = 0;
		t = new int[m];
		Arrays.fill(t, -1);
		t[0] = 0;
		left[0] = 1;

		for (int i = 1; i < arr.length; i++)
		{
			if(arr[i] >= arr[t[end]])
			{
				end++;
				t[end] = i;
				left[i] = end+1;
			}
			else
				if(arr[i] < arr[t[0]])
				{
					t[0] = i;
					left[i] = 1;
				}
				else
				{
					int ind = search(0, end, arr[i]);
					t[ind] = i;
					left[i] = ind+1;
				}
		}

//		System.err.println(Arrays.toString(left));
		int[] arr2 = new int[arr.length];
		for (int i = 0; i < arr2.length; i++)
			arr2[i] = arr[m-i-1];

		arr = arr2.clone();

		end = 0;
		t = new int[m];
		Arrays.fill(t, -1);
		t[0] = 0;
		right[m-1] = 1;

		for (int i = 1; i < arr.length; i++)
		{
			if(arr[i] >= arr[t[end]])
			{
				end++;
				t[end] = i;
				right[m-1-i] = end+1;
			}
			else
				if(arr[i] < arr[t[0]])
				{
					t[0] = i;
					right[m-1-i] = 1;
				}
				else
				{
					int ind = search(0, end, arr[i]);
					t[ind] = i;
					right[m-1-i] = ind+1;
				}
		}

//		System.err.println(Arrays.toString(right));

		int ans = 0;
		for (int i = 0; i < arr.length; i++)
			ans = Math.max(ans, left[i] + right[i] - 1);

		pw.println(ans);

		pw.flush();
		pw.close();
	}

	private static int search(int st, int en, int target)
	{
		int mid = st+(en-st)/2;
		if(st == en)
			return st;
		if(arr[t[mid]] > target)
			return search(st, mid, target);
		else
			return search(mid+1, en, target);
	}

	static class Scanner
	{
		StringTokenizer st; BufferedReader br;
		public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}
		public String next() throws IOException {while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());return st.nextToken();}
		public int nextInt() throws IOException {return Integer.parseInt(next());}
		public long nextLong() throws IOException {return Long.parseLong(next());}
		public String nextLine() throws IOException {return br.readLine();}
		public boolean ready() throws IOException {return br.ready();}
	}
}

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

public class A {
    FastScanner in;
    PrintWriter out;

    class SegmentTree {
        int[] a;

        SegmentTree(int n) {
            a = new int[n * 4];
        }

        void upd(int v, int l, int r, int pos, int nv) {
            if (l == r) {
                a[v] = nv;
            } else {
                int m = (l + r) >> 1;
                if (m >= pos) {
                    upd(v * 2 + 1, l, m, pos, nv);
                } else {
                    upd(v * 2 + 2, m + 1, r, pos, nv);
                }
                a[v] = Math.max(a[v * 2 + 1], a[v * 2 + 2]);
            }
        }

        int get(int v, int l, int r, int needL, int needR) {
            if (needL > needR) {
                return 0;
            }
            if (l == needL && r == needR) {
                return a[v];
            }
            int m = (l + r) >> 1;
            return Math.max(get(v * 2 + 1, l, m, needL, Math.min(needR, m)), get(v * 2 + 2, m + 1, r, Math.max(needL, m + 1), needR));
        }
    }

    void solve() {
        int cnt = in.nextInt();
        int n = in.nextInt();
        int[] a = new int[n + 1];
        for (int i = 0; i < cnt; i++) {
            a[in.nextInt() - 1]++;
            a[in.nextInt()]--;
        }
        for (int i = 1; i < a.length; i++) {
            a[i] += a[i - 1];
        }
        final int MAX = (int) 1e5 + 10;
        SegmentTree st = new SegmentTree(MAX);
        int[] maxLeft = new int[n];
        for (int i = 0; i < n; i++) {
            int cur = st.get(0, 0, MAX - 1, 0, a[i]);
            st.upd(0, 0, MAX - 1, a[i], cur + 1);
            maxLeft[i] = cur;
        }
        st = new SegmentTree(MAX);
        int res = 0;
        for (int i = n - 1; i >= 0; i--) {
            int cur = st.get(0, 0, MAX - 1, 0, a[i]);
            st.upd(0, 0, MAX - 1, a[i], cur + 1);
            res = Math.max(res, maxLeft[i] + cur + 1);
        }
        out.println(res);
    }

    void run() {
        try {
            in = new FastScanner(new File("A.in"));
            out = new PrintWriter(new File("A.out"));

            solve();

            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    void runIO() {

        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.close();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

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

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

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

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

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

    public static void main(String[] args) {
        new A().runIO();
    }
}

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

public class C {
	public static void main(String[] args) throws IOException {

		/**/
		Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		/*/
		Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(new FileInputStream("src/c.in"))));
		/**/

		int n = sc.nextInt();
		int m = sc.nextInt();
		int[] starts = new int[m+1];
		int[] ends = new int[m+1];
		for (int i = 0; i < n; i++) {
			starts[sc.nextInt()]++;
			ends[sc.nextInt()]++;
		}
		int[] counts = new int[m+1];
		for (int i = 1; i <= m; i++) {
			counts[i] = counts[i-1]+starts[i]-ends[i-1];
		}

		int[] lisp = new int[m+1];
		int[] lism = new int[m+1];
		int[] sublens = new int[m+1];

		int l = 0;
		for (int i = 1; i <= m; i++) {
			int lo = 1;
			int hi = l;
			while (lo <= hi) {
				int mid = (lo+hi+1)/2;
				if (counts[lism[mid]]<=counts[i]) {
					lo = mid+1;
				} else {
					hi = mid-1;
				}
			}
			int newl = lo;
			lisp[i] = lism[newl-1];
			lism[newl] = i;

			if (newl > l)
				l = newl;
			sublens[i] = newl;
		}

		lisp = new int[m+1];
		lism = new int[m+1];
		int[] sublens2 = new int[m+1];
		int[] cr = new int[m+1];
		for (int i = 0; i <= m; i++) {
			cr[m-i] = counts[i];
		}

		l = 0;
		for (int i = 0; i <= m; i++) {
			int lo = 1;
			int hi = l;
			while (lo <= hi) {
				int mid = (lo+hi+1)/2;
				if (cr[lism[mid]]<=cr[i]) {
					lo = mid+1;
				} else {
					hi = mid-1;
				}
			}
			int newl = lo;
			lisp[i] = lism[newl-1];
			lism[newl] = i;

			if (newl > l)
				l = newl;
			sublens2[i] = newl;
		}

		int ans = 0;
		for (int i = 1; i <= m; i++) {
			ans = Math.max(ans, sublens[i-1]+sublens2[m-i]);
		}
		System.out.println(ans);
	}
}

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

import static java.util.Arrays.fill;
import static java.lang.Math.*;
import static java.util.Arrays.sort;
import static java.util.Collections.sort;

public class F931
{

	public static int mod = 1000000007;
	public static long INF = (1L << 60);
	static FastScanner2 in = new FastScanner2();
	static OutputWriter out = new OutputWriter(System.out);
	static int n,m;
	static int[] arr;
	static int[] dp1;
	static int[] dp2;
	static int[] c1;
	static int[] c2;
	public static void main(String[] args)
	{

		n=in.nextInt();
		m=in.nextInt();
		arr=new int[m+2];
		dp1=new int[m+2];
		dp2=new int[m+2];
		c1=new int[m+1];
		c2=new int[m+1];
		for(int i=1;i<=n;i++)
		{
			int l=in.nextInt();
			int r=in.nextInt();
			arr[l]++;
			arr[r+1]--;
		}
		for(int i=1;i<=m;i++)
		{
			arr[i]+=arr[i-1];
		}
		arr[0]=mod;
		arr[m+1]=mod;
		dp1[1]=1;
		c1[1]=arr[1];
		int size=1;
		for(int i=2;i<=m;i++)
		{
			dp1[i]=1;
			if(arr[i]<c1[1])
			{
				dp1[i]=1;
				c1[1]=arr[i];
			}
			else
				if(arr[i]>=c1[size])
				{
					dp1[i]=size+1;
					//tr(size+" "+dp1[i]);
					c1[size+1]=arr[i];
					size++;
				}
				else
				{
					int answer=1;
					int left=1;
					int right=size;
					while(left<=right)
					{
						int mid=(left+right)/2;
						if(c1[mid]<=arr[i])
						{
							answer=mid;
							left=mid+1;
						}
						else
						{
							right=mid-1;
						}
					}
					answer++;
					c1[answer]=arr[i];
					dp1[i]=answer;
				}
		}
		dp2[1]=1;
		c2[1]=arr[m];
		size=1;
		for(int i=m-1;i>=1;i--)
		{
			if(arr[i]<c2[1])
			{
				dp2[1]=1;
				c2[1]=arr[i];
			}
			else
				if(arr[i]>=c2[size])
				{
					dp2[i]=size+1;
					c2[size+1]=arr[i];
					size++;
				}
				else
				{
					int answer=1;
					int left=1;
					int right=size;
					while(left<=right)
					{
						int mid=(left+right)/2;
						if(c2[mid]<=arr[i])
						{
							answer=mid;
							left=mid+1;
						}
						else
						{
							right=mid-1;
						}
					}
					answer++;
					c2[answer]=arr[i];
					dp2[i]=answer;
				}
		}
		int answer=1;
		for(int i=1;i<=m;i++)
		{
			answer=max(answer, dp1[i]+dp2[i]-1);
		}
		out.println(answer);
		out.close();

	}

	public static long pow(long x, long n)
	{
		long res = 1;
		for (long p = x; n > 0; n >>= 1, p = (p * p))
		{
			if ((n & 1) != 0)
			{
				res = (res * p);
			}
		}
		return res;
	}

	public static long pow(long x, long n, long mod)
	{
		long res = 1;
		for (long p = x; n > 0; n >>= 1, p = (p * p) % mod)
		{
			if ((n & 1) != 0)
			{
				res = (res * p % mod);
			}
		}
		return res;
	}

	public static long gcd(long n1, long n2)
	{
		long r;
		while (n2 != 0)
		{
			r = n1 % n2;
			n1 = n2;
			n2 = r;
		}
		return n1;
	}

	public static long lcm(long n1, long n2)
	{
		long answer = (n1 * n2) / (gcd(n1, n2));
		return answer;
	}

	static class FastScanner2
	{
		private byte[] buf = new byte[1024];
		private int curChar;
		private int snumChars;

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

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

		public String nextString()
		{
			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 long nextLong()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long 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 int nextInt()
		{
			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 int[] nextIntArray(int n)
		{
			int[] arr = new int[n];
			for (int i = 0; i < n; i++)
			{
				arr[i] = nextInt();
			}
			return arr;
		}

		public long[] nextLongArray(int n)
		{
			long[] arr = new long[n];
			for (int i = 0; i < n; i++)
			{
				arr[i] = nextLong();
			}
			return arr;
		}

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

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

	static class InputReader
	{
		public BufferedReader reader;
		public StringTokenizer tokenizer;

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

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

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

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

		public int[] nextIntArray(int n)
		{
			int a[] = new int[n];
			for (int i = 0; i < n; i++)
			{
				a[i] = nextInt();
			}
			return a;
		}

		public long[] nextLongArray(int n)
		{
			long a[] = new long[n];
			for (int i = 0; i < n; i++)
			{
				a[i] = nextLong();
			}
			return a;
		}

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

	static 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 println(Object... objects)
		{
			print(objects);
			writer.println();
		}

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

		public void flush()
		{
			writer.flush();
		}
	}
private static boolean oj = System.getProperty("ONLINE_JUDGE") != null;

	private static void tr(Object... o)
	{
		if (!oj)
			System.out.println(Arrays.deepToString(o));
	}
}

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

import static java.util.Arrays.fill;
import static java.lang.Math.*;
import static java.util.Arrays.sort;
import static java.util.Collections.sort;

public class F931
{

	public static int mod = 1000000007;
	public static long INF = (1L << 60);
	static FastScanner2 in = new FastScanner2();
	static OutputWriter out = new OutputWriter(System.out);
	static int n,m;
	static int[] arr;
	static int[] dp1;
	static int[] dp2;
	static int[] c1;
	static int[] c2;
	public static void main(String[] args)
	{

		n=in.nextInt();
		m=in.nextInt();
		arr=new int[m+2];
		dp1=new int[m+2];
		dp2=new int[m+2];
		c1=new int[m+1];
		c2=new int[m+1];
		for(int i=1;i<=n;i++)
		{
			int l=in.nextInt();
			int r=in.nextInt();
			arr[l]++;
			arr[r+1]--;
		}
		for(int i=1;i<=m;i++)
		{
			arr[i]+=arr[i-1];
		}
		arr[0]=mod;
		arr[m+1]=mod;
		dp1[1]=1;
		c1[1]=arr[1];
		int size=1;
		for(int i=2;i<=m;i++)
		{
			dp1[i]=1;
			if(arr[i]<c1[1])
			{
				dp1[i]=1;
				c1[1]=arr[i];
			}
			else
				if(arr[i]>=c1[size])
				{
					dp1[i]=size+1;
					//tr(size+" "+dp1[i]);
					c1[size+1]=arr[i];
					size++;
				}
				else
				{
					int answer=1;
					int left=1;
					int right=size;
					while(left<=right)
					{
						int mid=(left+right)/2;
						if(c1[mid]<=arr[i])
						{
							answer=mid;
							left=mid+1;
						}
						else
						{
							right=mid-1;
						}
					}
					answer++;
					c1[answer]=arr[i];
					dp1[i]=answer;
				}
		}
		dp2[1]=1;
		c2[1]=arr[m];
		size=1;
		for(int i=m-1;i>=1;i--)
		{
			if(arr[i]<c2[1])
			{
				dp2[1]=1;
				c2[1]=arr[i];
			}
			else
				if(arr[i]>=c2[size])
				{
					dp2[i]=size+1;
					c2[size+1]=arr[i];
					size++;
				}
				else
				{
					int answer=1;
					int left=1;
					int right=size;
					while(left<=right)
					{
						int mid=(left+right)/2;
						if(c2[mid]<=arr[i])
						{
							answer=mid;
							left=mid+1;
						}
						else
						{
							right=mid-1;
						}
					}
					answer++;
					c2[answer]=arr[i];
					dp2[i]=answer;
				}
		}
		int answer=dp2[1];
		for(int i=1;i<=m;i++)
		{
			answer=max(answer, dp1[i]+dp2[i+1]);
		}
		out.println(answer);
		out.close();

	}

	public static long pow(long x, long n)
	{
		long res = 1;
		for (long p = x; n > 0; n >>= 1, p = (p * p))
		{
			if ((n & 1) != 0)
			{
				res = (res * p);
			}
		}
		return res;
	}

	public static long pow(long x, long n, long mod)
	{
		long res = 1;
		for (long p = x; n > 0; n >>= 1, p = (p * p) % mod)
		{
			if ((n & 1) != 0)
			{
				res = (res * p % mod);
			}
		}
		return res;
	}

	public static long gcd(long n1, long n2)
	{
		long r;
		while (n2 != 0)
		{
			r = n1 % n2;
			n1 = n2;
			n2 = r;
		}
		return n1;
	}

	public static long lcm(long n1, long n2)
	{
		long answer = (n1 * n2) / (gcd(n1, n2));
		return answer;
	}

	static class FastScanner2
	{
		private byte[] buf = new byte[1024];
		private int curChar;
		private int snumChars;

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

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

		public String nextString()
		{
			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 long nextLong()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long 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 int nextInt()
		{
			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 int[] nextIntArray(int n)
		{
			int[] arr = new int[n];
			for (int i = 0; i < n; i++)
			{
				arr[i] = nextInt();
			}
			return arr;
		}

		public long[] nextLongArray(int n)
		{
			long[] arr = new long[n];
			for (int i = 0; i < n; i++)
			{
				arr[i] = nextLong();
			}
			return arr;
		}

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

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

	static class InputReader
	{
		public BufferedReader reader;
		public StringTokenizer tokenizer;

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

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

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

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

		public int[] nextIntArray(int n)
		{
			int a[] = new int[n];
			for (int i = 0; i < n; i++)
			{
				a[i] = nextInt();
			}
			return a;
		}

		public long[] nextLongArray(int n)
		{
			long a[] = new long[n];
			for (int i = 0; i < n; i++)
			{
				a[i] = nextLong();
			}
			return a;
		}

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

	static 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 println(Object... objects)
		{
			print(objects);
			writer.println();
		}

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

		public void flush()
		{
			writer.flush();
		}
	}
private static boolean oj = System.getProperty("ONLINE_JUDGE") != null;

	private static void tr(Object... o)
	{
		if (!oj)
			System.out.println(Arrays.deepToString(o));
	}
}

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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Jay Leeds
 */
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);
        TaskF solver = new TaskF();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskF {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] prefix = new int[m];
            for (int i = 0; i < n; i++) {
                int a = in.nextInt() - 1;
                int b = in.nextInt();
                prefix[a]++;
                if (b < m) prefix[b]--;
            }

            int[] data = new int[m];
            int count = 0;
            for (int i = 0; i < m; i++) {
                count += prefix[i];
                data[i] = count;
            }

            int[] increasingIndices = new int[m + 2];
            for (int i = 0; i < m + 2; i++) {
                increasingIndices[i] = n + 1;
            }

            increasingIndices[0] = -1;
            int[] increaseData = new int[m];
            for (int i = 0; i < m; i++) {
                int lo = 0;
                int hi = m;
                while (lo <= hi) {
                    int mid = (lo + hi + 1) / 2;
                    if (increasingIndices[mid] <= data[i]) {
                        lo = mid + 1;
                    } else {
                        hi = mid - 1;
                    }
                }
                increaseData[i] = lo;
                increasingIndices[lo] = Math.min(increasingIndices[lo], data[i]);
            }

            int[] decreasingIndices = new int[m + 2];
            for (int i = 0; i < m + 2; i++) decreasingIndices[i] = n + 1;
            decreasingIndices[0] = -1;
            int[] decreaseData = new int[m];
            for (int i = m - 1; i >= 0; i--) {
                int lo = 0;
                int hi = m;
                while (lo <= hi) {
                    int mid = (lo + hi + 1) / 2;
                    if (decreasingIndices[mid] <= data[i]) {
                        lo = mid + 1;
                    } else hi = mid - 1;
                }

                decreaseData[i] = lo;
                decreasingIndices[lo] = Math.min(decreasingIndices[lo], data[i]);
            }

            int max = 0;
            for (int i = 0; i < m; i++) {
                max = Math.max(max, decreaseData[i] + increaseData[i] - 1);
            }
            out.printLine(max);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream; //Fast IO by Egor Kulikov, modified by myself
        }

        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 int nextInt() {
            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);

        }

    }

    static 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 printLine(int i) {
            writer.println(i);
        }

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

    }
}

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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author palayutm
 */
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);
        ProblemFTeodorIsNotALiar solver = new ProblemFTeodorIsNotALiar();
        solver.solve(1, in, out);
        out.close();
    }

    static class ProblemFTeodorIsNotALiar {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            int[] a = new int[m + 2];
            for (int i = 0; i < n; i++) {
                int L = in.nextInt(), R = in.nextInt();
                a[L]++;
                a[R + 1]--;
            }
            for (int i = 1; i <= m; i++) {
                a[i] += a[i - 1];
            }
            int[] f = new int[m + 1];
            List<Integer> arr = new ArrayList<>();
            for (int i = 1; i <= m; i++) {
                add(arr, a[i]);
                f[i] = arr.size();
            }
            arr.clear();
            int ans = 0;
            for (int i = m; i >= 1; i--) {
                add(arr, a[i]);
                ans = Math.max(ans, f[i - 1] + arr.size());
            }
            out.println(ans);
        }

        private void add(List<Integer> arr, int val) {
            if (arr.isEmpty() || val >= arr.get(arr.size() - 1)) {
                arr.add(val);
            } else {
                int p = ArrayUtil.lowerBound(arr, val + 1);
                arr.set(p, val);
            }
        }

    }

    static class ArrayUtil {
        public static int lowerBound(List a, int val) {
            int low = 0, high = a.size() - 1;
            while (low <= high) {
                int mid = (low + high) / 2;
                if ((int) a.get(mid) >= val) high = mid - 1;
                else low = mid + 1;
            }
            return low;
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

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

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

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

    }

    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream out) {
            super(out);
        }

        public OutputWriter(Writer out) {
            super(out);
        }

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

    }
}

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

/**
 * @author Don Li
 */
public class TeodorIsNotALiar {

    int N = (int) 1e5 + 10;

    int[] bit = new int[N];

    void solve() {
        int n = in.nextInt(), m = in.nextInt();

        int[] a = new int[m + 1];
        for (int i = 0; i < n; i++) {
            int l = in.nextInt() - 1, r = in.nextInt();
            a[l]++;
            a[r]--;
        }
        for (int i = 1; i < m; i++) a[i] += a[i - 1];

        int[] cnt = new int[n + 1];
        for (int i = 0; i < m; i++) cnt[a[i]]++;
        for (int i = 1; i <= n; i++) cnt[i] += cnt[i - 1];

        int[] dpl = new int[m + 1];
        Arrays.fill(bit, 0);
        for (int i = 0; i < m; i++) {
            int x = query(a[i]);
            dpl[i + 1] = x + 1;
            update(a[i], dpl[i + 1]);
        }

        int[] dpr = new int[m + 1];
        Arrays.fill(bit, 0);
        for (int i = m - 1; i >= 0; i--) {
            int x = query(a[i]);
            dpr[m - i] = x + 1;
            update(a[i], dpr[m - i]);
        }

        int ans = 0;
        for (int i = 0; i < m; i++) {
            ans = Math.max(ans, dpl[i + 1] + dpr[m - i] - 1);
        }
        out.println(ans);
    }

    void update(int i, int val) {
        for (int x = i; x < N; x = x | (x + 1)) {
            bit[x] = Math.max(bit[x], val);
        }
    }

    int query(int i) {
        int max = 0;
        for (int x = i; x >= 0; x = (x & (x + 1)) - 1) {
            max = Math.max(max, bit[x]);
        }
        return max;
    }

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        new TeodorIsNotALiar().solve();
        out.close();
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

        public FastScanner(BufferedReader in) {
            this.in = in;
        }

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

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

        public long nextLong() {
            return Long.parseLong(nextToken());
        }

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

import java.util.*;
import java.io.*;
import java.math.*;
public class code7 {
	InputStream is;
	PrintWriter out;
	static long mod=pow(10,9)+7;
	int dx[]= {0,0,1,-1},dy[]={+1,-1,0,0};
	void solve()
	{
		int n=ni();
		int m=ni();
		int seg[]=new int[m+2];
		for(int i=0;i<n;i++)
		{
			int l=ni();
			int r=ni();
			seg[l]++;
			seg[r+1]--;
		}
		int count=0;
		for(int i=1;i<=m;i++)
		{
			count+=seg[i];
			seg[i]=count;
		}
		seg[0]=Integer.MAX_VALUE;
		seg[m+1]=Integer.MAX_VALUE;
		inc=new int[m+2];
		incr=new int[m+2];
		lis(seg);
		lisr(seg);
		int max=0;
		for(int i=1;i<=m;i++)
		{
			max=Math.max(max,inc[i]+incr[i]-1);
		}
		System.out.println(max);
	}
	int inc[],incr[];
	int lis(int a[])
	{
		int n=a.length;
		int dp[]=new int[n];
		dp[0]=a[0];
		int len=1;
		inc[0]=len;
		for(int i=1;i<n;i++)
		{
			if(a[i]<dp[0])
				dp[0]=a[i];
			else if(a[i]>=dp[len-1])
				dp[len++]=a[i];
			else
				dp[ceil(dp,0,len-1,a[i])]=a[i];
			inc[i]=len;

		}
		return  len;
	}
	int ceil(int a[],int l,int r,int key)
	{
		int ans=-1;
		while(l<=r)
		{
			int m=l+(r-l)/2;
			if(key<a[m])
			{
				ans=m;
				r=m-1;
			}
			else
				l=m+1;
		}
		return ans;
	}
	int lisr(int a[])
	{
		int n=a.length;
		int dp[]=new int[n];
		dp[0]=a[n-1];
		int len=1;
		incr[n-1]=len;
		for(int i=n-2;i>=0;i--)
		{
			if(a[i]<dp[0])
				dp[0]=a[i];
			else if(a[i]>=dp[len-1])
				dp[len++]=a[i];
			else
				dp[ceil(dp,0,len-1,a[i])]=a[i];
			incr[i]=len;
		}
		return  len;
	}
	int small[];
	void pre(int n)
	{
		small=new int[n+1];
		for(int i=2;i*i<=n;i++)
		{
			for(int j=i;j*i<=n;j++)
			{
				if(small[i*j]==0)
					small[i*j]=i;
			}
		}
		for(int i=0;i<=n;i++)
		{
			if(small[i]==0)
				small[i]=i;
		}
	}
	public static int count(int x)
	{
		int num=0;
		while(x!=0)
		{
			x=x&(x-1);
			num++;
		}
		return num;
	}
	static long d, x, y;
	void extendedEuclid(long A, long B) {
	    if(B == 0) {
	        d = A;
	        x = 1;
	        y = 0;
	    }
	    else {
	        extendedEuclid(B, A%B);
	        long temp = x;
	        x = y;
	        y = temp - (A/B)*y;
	    }
	}
	long modInverse(long A,long M) //A and M are coprime
	{
	    extendedEuclid(A,M);
	    return (x%M+M)%M;    //x may be negative
	}
	public static void mergeSort(int[] arr, int l ,int r){
		if((r-l)>=1){
			int mid = (l+r)/2;
			mergeSort(arr,l,mid);
			mergeSort(arr,mid+1,r);
			merge(arr,l,r,mid);
		}
	}
	public static void merge(int arr[], int l, int r, int mid){
		int n1 = (mid-l+1), n2 = (r-mid);
		int left[] = new int[n1];
		int right[] = new int[n2];
		for(int i =0 ;i<n1;i++) left[i] = arr[l+i];
		for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i];
		int i =0, j =0, k = l;
		while(i<n1 && j<n2){
			if(left[i]>right[j]){
				arr[k++] = right[j++];
			}
			else{
				arr[k++] = left[i++];
			}
		}
		while(i<n1) arr[k++] = left[i++];
		while(j<n2) arr[k++] = right[j++];
	}
	public static void mergeSort(long[] arr, int l ,int r){
		if((r-l)>=1){
			int mid = (l+r)/2;
			mergeSort(arr,l,mid);
			mergeSort(arr,mid+1,r);
			merge(arr,l,r,mid);
		}
	}
	public static void merge(long arr[], int l, int r, int mid){
		int n1 = (mid-l+1), n2 = (r-mid);
		long left[] = new long[n1];
		long right[] = new long[n2];
		for(int i =0 ;i<n1;i++) left[i] = arr[l+i];
		for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i];
		int i =0, j =0, k = l;
		while(i<n1 && j<n2){
			if(left[i]>right[j]){
				arr[k++] = right[j++];
			}
			else{
				arr[k++] = left[i++];
			}
		}
		while(i<n1) arr[k++] = left[i++];
		while(j<n2) arr[k++] = right[j++];
	}
	 static class Pair implements Comparable<Pair>{

	      int x,y,k,i;
		Pair (int x,int y,int k){
			this.x=x;
			this.y=y;
			this.k=k;
		}

		public int compareTo(Pair o) {
			return this.y-o.y;
		}
		public boolean equals(Object o) {
			if (o instanceof Pair) {
				Pair p = (Pair)o;
				return p.x == x && p.y == y;
			}
			return false;
		}
		public int hashCode() {
			return new Long(x).hashCode()*31 + new Long(y).hashCode() ;
		}

	        @Override
	        public String toString() {
	            return  "("+x + " " + y +" "+k+" "+i+" )";
	        }

	    }

	    public static boolean isPal(String s){
	        for(int i=0, j=s.length()-1;i<=j;i++,j--){
	                if(s.charAt(i)!=s.charAt(j)) return false;
	        }
	        return true;
	    }
	    public static String rev(String s){
			StringBuilder sb=new StringBuilder(s);
			sb.reverse();
			return sb.toString();
	    }

	    public static long gcd(long x,long y){
		if(x%y==0)
			return y;
		else
			return gcd(y,x%y);
	    }

	    public static int gcd(int x,int y){
	    	if(y==0)
	    		return x;
	    	return gcd(y,x%y);
	    }

	    public static long gcdExtended(long a,long b,long[] x){

	        if(a==0){
	            x[0]=0;
	            x[1]=1;
	            return b;
	        }
	        long[] y=new long[2];
	        long gcd=gcdExtended(b%a, a, y);

	        x[0]=y[1]-(b/a)*y[0];
	        x[1]=y[0];

	        return gcd;
	    }

	    public static int abs(int a,int b){
		return (int)Math.abs(a-b);
	    }

	    public static long abs(long a,long b){
		return (long)Math.abs(a-b);
	    }

	    public static int max(int a,int b){
		if(a>b)
			return a;
		else
			return b;
	    }

	    public static int min(int a,int b){
		if(a>b)
			return b;
		else
			return a;
	    }

	    public static long max(long a,long b){
		if(a>b)
			return a;
		else
			return b;
	    }

	    public static long min(long a,long b){
		if(a>b)
			return b;
		else
			return a;
	    }

	    public static long pow(long n,long p,long m){
		 long  result = 1;
		  if(p==0)
		    return 1;
		if (p==1)
		    return n;
		while(p!=0)
		{
		    if(p%2==1)
		        result *= n;
		    if(result>=m)
		    result%=m;
		    p >>=1;
		    n*=n;
		    if(n>=m)
		    n%=m;
		}
		return result;
	    }

	    public static long pow(long n,long p){
		long  result = 1;
		  if(p==0)
		    return 1;
		if (p==1)
		    return n;
		while(p!=0)
		{
		    if(p%2==1)
		        result *= n;
		    p >>=1;
		    n*=n;
		}
		return result;
	    }
	    public static void debug(Object... o) {
			System.out.println(Arrays.deepToString(o));
		}
	    void run() throws Exception {
			is = System.in;
			out = new PrintWriter(System.out);
			solve();
			out.flush();
		}

	    public static void main(String[] args) throws Exception {
			new Thread(null, new Runnable() {
				public void run() {
					try {
						new code7().run();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}, "1", 1 << 26).start();

		}
	    private byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		private int readByte() {
			if (lenbuf == -1)
				throw new InputMismatchException();
			if (ptrbuf >= lenbuf) {
				ptrbuf = 0;
				try {
					lenbuf = is.read(inbuf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return -1;
			}
			return inbuf[ptrbuf++];
		}

		private boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		private int skip() {
			int b;
			while ((b = readByte()) != -1 && isSpaceChar(b));
			return b;
		}

		private double nd() {
			return Double.parseDouble(ns());
		}

		private char nc() {
			return (char) skip();
		}

		private String ns() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		private char[] ns(int n) {
			char[] buf = new char[n];
			int b = skip(), p = 0;
			while (p < n && !(isSpaceChar(b))) {
				buf[p++] = (char) b;
				b = readByte();
			}
			return n == p ? buf : Arrays.copyOf(buf, p);
		}

		private char[][] nm(int n, int m) {
			char[][] map = new char[n][];
			for (int i = 0; i < n; i++)
				map[i] = ns(m);
			return map;
		}

		private int[] na(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = ni();
			return a;
		}

		private int ni() {
			int num = 0, b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		private long nl() {
			long num = 0;
			int b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

}

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

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

    static class TaskC {
        public void solve(int testNumber, Scan in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] data = new int[m + 1];
            for (int s = 0; s < n; s++) {
                data[in.nextInt() - 1]++;
                data[in.nextInt()]--;
            }
            for (int i = 1; i < m; i++) {
                data[i] += data[i - 1];
            }
            data = Arrays.copyOf(data, m);

            int[] f1 = mi(data);
            for (int t = 0; t < m / 2; t++) {
                int l = data[t];
                data[t] = data[m - t - 1];
                data[m - t - 1] = l;
            }
            int[] f2 = mi(data);
            int mr = 0;
            for (int i = 0; i < m; i++) {
                mr = Math.max(mr, f1[i] + f2[m - i - 1] - 1);
            }
            out.println(mr);

        }

        private int[] mi(int[] x) {
            int st = 0;
            int[] mmm = new int[x.length];
            int[] ret = new int[x.length];
            Arrays.fill(mmm, Integer.MAX_VALUE);
            for (int s = 0; s < x.length; s++) {
//            if (x[s] == 0) continue;
                if (st == 0 || x[s] >= mmm[st - 1]) {
                    mmm[st++] = x[s];
                    ret[s] = st;
                } else {
                    int f = 0;
                    int l = st;
                    while (f + 1 < l) {
                        int r = (f + l) / 2;
                        if (mmm[r] <= x[s]) {
                            f = r;
                        } else {
                            l = r;
                        }
                    }
                    mmm[f + 1] = x[s];
                    ret[s] = f + 1;
                }
            }
            return ret;
        }

    }

    static class Scan {
        BufferedReader br;
        StringTokenizer st;

        public Scan() {
            this(System.in);
        }

        public Scan(BufferedReader br) {
            this.br = br;
        }

        public Scan(InputStream in) {
            this(new BufferedReader(new InputStreamReader(in)));
        }

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

        public String next() {
            findToken();
            return st.nextToken();
        }

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

    }
}

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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author liym fans
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

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

            int[] delta = new int[m + 1];
            int[] sum = new int[m];

            for (int i = 0; i < n; ++i) {
                ++delta[in.nextInt() - 1];
                --delta[in.nextInt()];
            }

            sum[0] = delta[0];
            for (int i = 1; i < m; ++i) {
                sum[i] = sum[i - 1] + delta[i];
            }

            int[] left = new int[m];
            TaskC.BitIndexedTree tree = new TaskC.BitIndexedTree(m + 1);
            for (int i = 0; i < m; ++i) {
                int maxValue = tree.getMax(sum[i]);
                tree.update(sum[i], maxValue + 1);
                left[i] = maxValue;
            }

            int ans = 0;
            TaskC.BitIndexedTree tree2 = new TaskC.BitIndexedTree(m + 1);
            for (int i = m - 1; i >= 0; --i) {
                int maxValue = tree2.getMax(sum[i]);
                tree2.update(sum[i], maxValue + 1);
                ans = Math.max(ans, maxValue + left[i] + 1);
            }

            out.println(ans);
        }

        static class BitIndexedTree {
            private int[] arr;
            private int size;

            BitIndexedTree(int size) {
                this.size = size;
                arr = new int[size + 2];
            }

            private int lowbit(int x) {
                return x & -x;
            }

            public void update(int x, int y) {
                ++x;
                while (x <= size) {
                    arr[x] = Math.max(arr[x], y);
                    x += lowbit(x);
                }
            }

            public int getMax(int x) {
                ++x;
                int ret = 0;
                while (x > 0) {
                    ret = Math.max(ret, arr[x]);
                    x -= lowbit(x);
                }

                return ret;
            }

        }

    }

    static class InputReader {
        BufferedReader reader;
        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 (Exception e) {
                    throw new UnknownError();
                }
            }
            return tokenizer.nextToken();
        }

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

    }
}

import java.io.*;
import java.util.Arrays;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.function.Function;

public class E {
    String fileName = "E";

    public int n, m;

    public static class Event implements Comparable<Event> {
        public static final int OPEN_TYPE = 0;
        public static final int POINT_TYPE = 1;
        public static final int CLOSE_TYPE = 2;

        public int x, type;

        public Event(int x, int type) {
            this.x = x;
            this.type = type;
        }

        @Override
        public int compareTo(Event event) {
            if (Integer.compare(x, event.x) != 0)
                return Integer.compare(x, event.x);
            else return Integer.compare(type, event.type);
        }
    }

    public static final class Node {
        public int l, r;
        public int max, maxInd;

        public Node leftNode, rightNode;

        public Node(int l, int r) {
            this.l = l;
            this.r = r;
        }
    }

    public void relax(Node root) {
        if (root.leftNode.max > root.rightNode.max) {
            root.max = root.leftNode.max;
            root.maxInd = root.leftNode.maxInd;
        } else {
            root.max = root.rightNode.max;
            root.maxInd = root.rightNode.maxInd;
        }
    }

    public Node build(int l, int r, Function<Integer, Integer> getItem) {
        Node res = new Node(l, r);
        if (l == r) {
            res.max = getItem.apply(l);
            res.maxInd = l;
        } else {
            int m = (r + l) / 2;
            res.leftNode = build(l, m, getItem);
            res.rightNode = build(m + 1, r, getItem);
            relax(res);
        }
        return res;
    }

    public void set(Node root, int ind, int val) {
        if (ind < root.l || ind > root.r)
            return;
        if (root.l == root.r) {
            root.max = val;
            return;
        }
        set(root.leftNode, ind, val);
        set(root.rightNode, ind, val);
        relax(root);
    }

    public static class QueryResult {
        public int max, maxInd;

        public QueryResult(int max, int maxInd) {
            this.max = max;
            this.maxInd = maxInd;
        }

        public QueryResult(QueryResult first, QueryResult second) {
            if (first.max > second.max) {
                this.max = first.max;
                this.maxInd = first.maxInd;
            } else {
                this.max = second.max;
                this.maxInd = second.maxInd;
            }
        }
    }

    public QueryResult query(Node root, int l, int r) {
        if (r == -1)
            return new QueryResult(0, -1);
        if (r < root.l || l > root.r)
            return new QueryResult(Integer.MIN_VALUE, -1);
        if (l <= root.l && r >= root.r) {
            return new QueryResult(root.max, root.maxInd);
        }
        return new QueryResult(query(root.leftNode, l, r), query(root.rightNode, l, r));
    }

    public int[] getLens(int[] a) {
        Node root = build(0, n, ind -> 0);
        int[] len = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            int curVal = a[i];
            int prevLen = query(root, 0, curVal).max;
            len[i] = prevLen + 1;
            if (query(root, curVal, curVal).max < len[i])
                set(root, curVal, len[i]);
        }
        return len;
    }

    public void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        Event[] events = new Event[2 * n + m];
        for (int i = 1; i <= m; i++) {
            events[i - 1] = new Event(i, Event.POINT_TYPE);
        }
        for (int i = 0; i < n; i++) {
            int l = nextInt();
            int r = nextInt();
            events[m + 2 * i] = new Event(l, Event.OPEN_TYPE);
            events[m + 2 * i + 1] = new Event(r, Event.CLOSE_TYPE);
        }
        Arrays.sort(events);
        int[] cnt = new int[m];
        int balance = 0;
        for (int i = 0; i < events.length; i++) {
            Event curEvent = events[i];
            if (curEvent.type == Event.OPEN_TYPE) {
                balance++;
            } else if (curEvent.type == Event.CLOSE_TYPE) {
                balance--;
            } else if (curEvent.type == Event.POINT_TYPE) {
                cnt[curEvent.x - 1] = balance;
            }
        }
        int[] lenUp = getLens(cnt);
        int[] newCnt = new int[cnt.length];
        for (int i = 0; i < cnt.length; i++) {
            newCnt[i] = cnt[cnt.length - i - 1];
        }
        int[] lenDown = getLens(newCnt);
        int[] revLenDown = new int[lenDown.length];
        for (int i = 0; i < lenDown.length; i++) {
            revLenDown[i] = lenDown[lenDown.length - i - 1];
        }
        int max = 0;
        for (int i = 0; i < n; i++) {
            int cur = lenUp[i] + revLenDown[i] - 1;
            max = Math.max(max, cur);
        }
        out.print(max);
    }

    public void run() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);

            solve();

            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    BufferedReader br;
    StringTokenizer in;
    PrintWriter out;

    public String nextToken() throws IOException {
        while (in == null || !in.hasMoreTokens()) {
            in = new StringTokenizer(br.readLine());
        }
        return in.nextToken();
    }

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

    public double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    public long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    public static void main(String[] args) throws IOException {
        Locale.setDefault(Locale.US);
        new E().run();
    }
}

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

import static java.lang.Math.*;

public class Main {
    BufferedReader br;
    StringTokenizer in;
    PrintWriter pw;
    Random r;
    int INF = (int) 1e9;
    int mod = (int) (1e9 + 7);
    //int p = 26;
    ArrayList<Integer> g[];

    void end(int a, int n) {
        if ((int) Math.pow(2, a) != n) pw.print(a);
        else pw.print("Final!");
        pw.close();
        System.exit(0);
    }

    class node {
        int val;
        int m;
        int size;
        int prior;
        node L;
        node R;

        public node(int v) {
            val = v;
            prior = r.nextInt();
            size = 1;
            L = null;
            R = null;
        }
    }

    int sgs(node c) {
        if (c == null) return 0;
        return c.size;
    }

    int sgm(node c) {
        if (c == null) return INF;
        return c.m;
    }

    void relax(node c) {
        if (c == null) return;
        c.size = 1 + sgs(c.L) + sgs(c.R);
    }

    class pair implements Comparable<pair> {
        int a;
        int b;

        public pair(int aa, int bb) {
            a = aa;
            b = bb;
        }

        @Override
        public int compareTo(pair o) {
            if (a != o.a) return a - o.a;
            return b - o.b;
        }
    }

    class road {
        int to;
        int w;

        public road(int tto, int ww) {
            to = tto;
            w = ww;
        }

        public road() throws IOException {
            to = nextInt();
            w = nextInt();
        }
    }

    void readG(int n, int m) throws IOException {
        g = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            g[i] = new ArrayList<>();
        }
        for (int i = 0; i < m; i++) {
            int a = nextInt() - 1;
            int b = nextInt() - 1;
            int w = nextInt();
//            g[a].add(new road(b, w));
//            g[b].add(new road(a, w));
        }
    }

    int p[];
    int d[];

    void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        event[] p = new event[n * 2];
        for (int i = 0; i < n; i++) {
            int l = nextInt();
            int r = nextInt();
            p[i] = new event(l, 1);
            p[i + n] = new event(r, -1);
        }
        int q = p.length;
        int b = 0;
        Arrays.sort(p);
        int bal[] = new int[m + 1];
        int prev = 0;
        for (int i = 0; i < q; i++) {
            event a = p[i];
            if (a.t == -1) {
                for (int j = prev + 1; j <= a.time; j++) {
                    bal[j] = b;
                }
                prev = a.time;
            } else {
                for (int j = prev + 1; j < a.time; j++) {
                    bal[j] = b;
                }
                prev = a.time - 1;
            }
            b += a.t;
        }
//        for (int i = 1; i <= m; i++) {
//            pw.print(bal[i]);
//        }
        int revbal[] = new int[m + 1];
        for (int i = 1; i <= m; i++) {
            revbal[i] = bal[m + 1 - i];
        }
        int nvp[] = getnvp(bal);
        int rnvp[] = getnvp(revbal);
        int ans = 0;
        for (int i = 1; i <= m; i++) {
            int z = nvp[i];
            int w = rnvp[m - i + 1];
            int sum = z + w;
            if (z > nvp[i - 1] && (m - i >= 0 && w > rnvp[m - i])) {
                sum--;
            }
            ans = max(ans, sum);
        }
        pw.print(ans);
    }

    int[] getnvp(int a[]) {
        int n = a.length;
        int d[] = new int[n + 1];
        int ans[] = new int[n];
        Arrays.fill(d, INF);
        d[0] = -INF;
        int last = 0;
        for (int i = 1; i < n; i++) {
            ans[i] = ans[i - 1];
            int c = a[i];
            int l = 0;
            int r = last + 1;
            while (l + 1 != r) {
                int m = (l + r) / 2;
                if (d[m] <= c) l = m;
                else r = m;
            }
            if (d[r] == INF) {
                ans[i]++;
                last++;
            }
            d[r] = min(d[r], c);
        }
        return ans;
    }

    class event implements Comparable<event> {
        int time;
        int t;

        public event(int ttime, int tt) {
            time = ttime;
            t = tt;
        }

        @Override
        public int compareTo(event o) {
            if (time != o.time) return time - o.time;
            else return t - o.t;
        }
    }

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

    void run() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        pw = new PrintWriter(new OutputStreamWriter(System.out));
        r = new Random(5);
        solve();
        pw.close();
    }

    String nextToken() throws IOException {
        while (in == null || !in.hasMoreTokens()) in = new StringTokenizer(br.readLine());
        return in.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }
}

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

import static java.lang.Math.*;

public class Main {
    BufferedReader br;
    StringTokenizer in;
    PrintWriter pw;
    Random r;
    int INF = (int) 1e9;
    int mod = (int) (1e9 + 7);
    //int p = 26;
    ArrayList<Integer> g[];

    void end(int a, int n) {
        if ((int) Math.pow(2, a) != n) pw.print(a);
        else pw.print("Final!");
        pw.close();
        System.exit(0);
    }

    class node {
        int val;
        int m;
        int size;
        int prior;
        node L;
        node R;

        public node(int v) {
            val = v;
            prior = r.nextInt();
            size = 1;
            L = null;
            R = null;
        }
    }

    int sgs(node c) {
        if (c == null) return 0;
        return c.size;
    }

    int sgm(node c) {
        if (c == null) return INF;
        return c.m;
    }

    void relax(node c) {
        if (c == null) return;
        c.size = 1 + sgs(c.L) + sgs(c.R);
    }

    class pair implements Comparable<pair> {
        int a;
        int b;

        public pair(int aa, int bb) {
            a = aa;
            b = bb;
        }

        @Override
        public int compareTo(pair o) {
            if (a != o.a) return a - o.a;
            return b - o.b;
        }
    }

    class road {
        int to;
        int w;

        public road(int tto, int ww) {
            to = tto;
            w = ww;
        }

        public road() throws IOException {
            to = nextInt();
            w = nextInt();
        }
    }

    void readG(int n, int m) throws IOException {
        g = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            g[i] = new ArrayList<>();
        }
        for (int i = 0; i < m; i++) {
            int a = nextInt() - 1;
            int b = nextInt() - 1;
            int w = nextInt();
//            g[a].add(new road(b, w));
//            g[b].add(new road(a, w));
        }
    }

    int p[];
    int d[];

    void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        event[] p = new event[n * 2];
        for (int i = 0; i < n; i++) {
            int l = nextInt();
            int r = nextInt();
            p[i] = new event(l, 1);
            p[i + n] = new event(r, -1);
        }
        int q = p.length;
        int b = 0;
        Arrays.sort(p);
        int bal[] = new int[m + 1];
        int prev = 0;
        for (int i = 0; i < q; i++) {
            event a = p[i];
            if (a.t == -1) {
                for (int j = prev + 1; j <= a.time; j++) {
                    bal[j] = b;
                }
                prev = a.time;
            } else {
                for (int j = prev + 1; j < a.time; j++) {
                    bal[j] = b;
                }
                prev = a.time - 1;
            }
            b += a.t;
        }
//        for (int i = 1; i <= m; i++) {
//            pw.print(bal[i]);
//        }
        int revbal[] = new int[m + 1];
        for (int i = 1; i <= m; i++) {
            revbal[i] = bal[m + 1 - i];
        }
        int nvp[] = getnvp(bal);
        int rnvp[] = getnvp(revbal);
        int ans = 0;
        for (int i = 1; i <= m; i++) {
            int z = nvp[i];
            int w = rnvp[m - i];
            int sum = z + w;
            if (z > nvp[i - 1] && (m - i - 1 >= 0 && w > rnvp[m - i - 1])) {
                sum--;
            }
            ans = max(ans, sum);
        }
        pw.print(ans);
    }

    int[] getnvp(int a[]) {
        int n = a.length;
        int d[] = new int[n + 1];
        int ans[] = new int[n];
        Arrays.fill(d, INF);
        d[0] = -INF;
        int last = 0;
        for (int i = 1; i < n; i++) {
            ans[i] = ans[i - 1];
            int c = a[i];
            int l = 0;
            int r = last + 1;
            while (l + 1 != r) {
                int m = (l + r) / 2;
                if (d[m] <= c) l = m;
                else r = m;
            }
            if (d[r] == INF) {
                ans[i]++;
                last++;
            }
            d[r] = min(d[r], c);
        }
        return ans;
    }

    class event implements Comparable<event> {
        int time;
        int t;

        public event(int ttime, int tt) {
            time = ttime;
            t = tt;
        }

        @Override
        public int compareTo(event o) {
            if (time != o.time) return time - o.time;
            else return t - o.t;
        }
    }

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

    void run() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        pw = new PrintWriter(new OutputStreamWriter(System.out));
        r = new Random(5);
        solve();
        pw.close();
    }

    String nextToken() throws IOException {
        while (in == null || !in.hasMoreTokens()) in = new StringTokenizer(br.readLine());
        return in.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }
}

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

import static java.lang.Math.*;

public class Main {
    BufferedReader br;
    StringTokenizer in;
    PrintWriter pw;
    Random r;
    int INF = (int) 1e9;
    int mod = (int) (1e9 + 7);
    //int p = 26;
    ArrayList<Integer> g[];

    void end(int a, int n) {
        if ((int) Math.pow(2, a) != n) pw.print(a);
        else pw.print("Final!");
        pw.close();
        System.exit(0);
    }

    class node {
        int val;
        int m;
        int size;
        int prior;
        node L;
        node R;

        public node(int v) {
            val = v;
            prior = r.nextInt();
            size = 1;
            L = null;
            R = null;
        }
    }

    int sgs(node c) {
        if (c == null) return 0;
        return c.size;
    }

    int sgm(node c) {
        if (c == null) return INF;
        return c.m;
    }

    void relax(node c) {
        if (c == null) return;
        c.size = 1 + sgs(c.L) + sgs(c.R);
    }

    class pair implements Comparable<pair> {
        int a;
        int b;

        public pair(int aa, int bb) {
            a = aa;
            b = bb;
        }

        @Override
        public int compareTo(pair o) {
            if (a != o.a) return a - o.a;
            return b - o.b;
        }
    }

    class road {
        int to;
        int w;

        public road(int tto, int ww) {
            to = tto;
            w = ww;
        }

        public road() throws IOException {
            to = nextInt();
            w = nextInt();
        }
    }

    void readG(int n, int m) throws IOException {
        g = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            g[i] = new ArrayList<>();
        }
        for (int i = 0; i < m; i++) {
            int a = nextInt() - 1;
            int b = nextInt() - 1;
            int w = nextInt();
//            g[a].add(new road(b, w));
//            g[b].add(new road(a, w));
        }
    }

    int p[];
    int d[];

    void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        event[] p = new event[n * 2];
        for (int i = 0; i < n; i++) {
            int l = nextInt();
            int r = nextInt();
            p[i] = new event(l, 1);
            p[i + n] = new event(r, -1);
        }
        int q = p.length;
        int b = 0;
        Arrays.sort(p);
        int bal[] = new int[m + 1];
        int prev = 0;
        for (int i = 0; i < q; i++) {
            event a = p[i];
            if (a.t == -1) {
                for (int j = prev + 1; j <= a.time; j++) {
                    bal[j] = b;
                }
                prev = a.time;
            } else {
                for (int j = prev + 1; j < a.time; j++) {
                    bal[j] = b;
                }
                prev = a.time - 1;
            }
            b += a.t;
        }
//        for (int i = 1; i <= m; i++) {
//            pw.print(bal[i]);
//        }
        int revbal[] = new int[m + 1];
        for (int i = 1; i <= m; i++) {
            revbal[i] = bal[m + 1 - i];
        }
        int nvp[] = getnvp(bal);
        int rnvp[] = getnvp(revbal);
        int ans = 0;
        for (int i = 1; i <= m; i++) {
            int z = nvp[i];
            int w = rnvp[m - i];
            int sum = z + w;
            ans = max(ans, sum);
        }
        pw.print(ans);
    }

    int[] getnvp(int a[]) {
        int n = a.length;
        int d[] = new int[n + 1];
        int ans[] = new int[n];
        Arrays.fill(d, INF);
        d[0] = -INF;
        int last = 0;
        for (int i = 1; i < n; i++) {
            ans[i] = ans[i - 1];
            int c = a[i];
            int l = 0;
            int r = last + 1;
            while (l + 1 != r) {
                int m = (l + r) / 2;
                if (d[m] <= c) l = m;
                else r = m;
            }
            if (d[r] == INF) {
                ans[i]++;
                last++;
            }
            d[r] = min(d[r], c);
        }
        return ans;
    }

    class event implements Comparable<event> {
        int time;
        int t;

        public event(int ttime, int tt) {
            time = ttime;
            t = tt;
        }

        @Override
        public int compareTo(event o) {
            if (time != o.time) return time - o.time;
            else return t - o.t;
        }
    }

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

    void run() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        pw = new PrintWriter(new OutputStreamWriter(System.out));
        r = new Random(5);
        solve();
        pw.close();
    }

    String nextToken() throws IOException {
        while (in == null || !in.hasMoreTokens()) in = new StringTokenizer(br.readLine());
        return in.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }
}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
    ArrayList<ArrayList<Integer>> q;
    boolean[] used;

    public void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        int[] a = new int[m + 1];
        for (int i = 0; i < n; i++) {
            a[nextInt() - 1]++;
            a[nextInt()]--;
        }
        for (int i = 1; i < m; i++) {
            a[i] += a[i - 1];
        }
        int[] d = new int[m + 10];
        Arrays.fill(d, Integer.MAX_VALUE);
        int k = 0;
        int[] v1 = new int[m + 1];
        int[] v2 = new int[m + 1];
        for (int i = 0; i < m; i++) {
            int l = 0;
            int r = m;
            while (r - l > 1) {
                int m1 = (r + l) / 2;
                if (d[m1] > a[i]) {
                    r = m1;
                } else {
                    l = m1;
                }
            }
            if (d[l] <= a[i]) {
                l++;
            }
            if (d[l] == Integer.MAX_VALUE){
                k++;
            }
            d[l] = a[i];
            v1[i] = k;
        }
        k = 0;
        int max = 0;
        for (int i = m - 1; i >= 0; i--) {
            int l = 0;
            int r = m + 2;
            while (r - l > 1) {
                int m1 = (r + l) / 2;
                if (d[m1] > a[i]) {
                    r = m1;
                } else {
                    l = m1;
                }
            }
            if (d[l] <= a[i]) {
                l++;
            }
            if (d[l] == Integer.MAX_VALUE){
                k++;
            }
            d[l] = a[i];
            v2[i] = k;
            max = Math.max(max,v1[i] + v2[i + 1]);
        }
        out.print(max);
    }

    BufferedReader br;
    StringTokenizer sc;
    PrintWriter out;

    String nextToken() throws IOException {
        while (sc == null || !sc.hasMoreTokens()) {
            try {
                sc = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                sc = null;
            }
        }
        return sc.nextToken();
    }

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

    public Long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    public Double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

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

    public void run() throws IOException {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
            solve();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
    ArrayList<ArrayList<Integer>> q;
    boolean[] used;

    public void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        int[] a = new int[m + 1];
        for (int i = 0; i < n; i++) {
            a[nextInt() - 1]++;
            a[nextInt()]--;
        }
        for (int i = 1; i < m; i++) {
            a[i] += a[i - 1];
        }
        int[] d = new int[m + 10];
        Arrays.fill(d, Integer.MAX_VALUE);
        int k = 0;
        int[] v1 = new int[m + 1];
        int[] v2 = new int[m + 1];
        for (int i = 0; i < m; i++) {
            int l = 0;
            int r = m;
            while (r - l > 1) {
                int m1 = (r + l) / 2;
                if (d[m1] > a[i]) {
                    r = m1;
                } else {
                    l = m1;
                }
            }
            if (d[l] <= a[i]) {
                l++;
            }
            if (d[l] == Integer.MAX_VALUE){
                k++;
            }
            d[l] = a[i];
            v1[i] = k;
        }
        k = 0;
        int max = 0;
        for (int i = m - 1; i >= 0; i--) {
            int l = 0;
            int r = m + 2;
            while (r - l > 1) {
                int m1 = (r + l) / 2;
                if (d[m1] > a[i]) {
                    r = m1;
                } else {
                    l = m1;
                }
            }
            if (d[l] <= a[i]) {
                l++;
            }
            if (d[l] == Integer.MAX_VALUE){
                k++;
            }
            d[l] = a[i];
            v2[i] = k;
            max = Math.max(max,v1[i] + v2[i + 1]);
        }
        max = Math.max(max,v2[0]);
        out.print(max);
    }

    BufferedReader br;
    StringTokenizer sc;
    PrintWriter out;

    String nextToken() throws IOException {
        while (sc == null || !sc.hasMoreTokens()) {
            try {
                sc = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                sc = null;
            }
        }
        return sc.nextToken();
    }

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

    public Long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    public Double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

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

    public void run() throws IOException {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
            solve();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}

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

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

    static class TaskC {
        public void solve(int testNumber, Scan in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] data = new int[m + 1];
            for (int s = 0; s < n; s++) {
                data[in.nextInt() - 1]++;
                data[in.nextInt()]--;
            }
            for (int i = 1; i < m; i++) {
                data[i] += data[i - 1];
            }
            data = Arrays.copyOf(data, m);

            int[] f1 = mi(data);
            for (int t = 0; t < m / 2; t++) {
                int l = data[t];
                data[t] = data[m - t - 1];
                data[m - t - 1] = 1;
            }
            int[] f2 = mi(data);
            int mr = 0;
            for (int i = 0; i < m; i++) {
                mr = Math.max(mr, f1[i] + f2[m - i - 1] - 1);
            }
            out.println(mr);

        }

        private int[] mi(int[] x) {
            int st = 0;
            int[] mmm = new int[x.length];
            int[] ret = new int[x.length];
            Arrays.fill(mmm, Integer.MAX_VALUE);
            for (int s = 0; s < x.length; s++) {
                if (x[s] == 0) continue;
                if (st == 0 || x[s] >= mmm[st - 1]) {
                    mmm[st++] = x[s];
                    ret[s] = st;
                } else {
                    int f = 0;
                    int l = st;
                    while (f + 1 < l) {
                        int r = (f + l) / 2;
                        if (mmm[r] <= x[s]) {
                            f = r;
                        } else {
                            l = r;
                        }
                    }
                    mmm[f + 1] = x[s];
                    ret[s] = f + 1;
                }
            }
            return ret;
        }

    }

    static class Scan {
        BufferedReader br;
        StringTokenizer st;

        public Scan() {
            this(System.in);
        }

        public Scan(BufferedReader br) {
            this.br = br;
        }

        public Scan(InputStream in) {
            this(new BufferedReader(new InputStreamReader(in)));
        }

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

        public String next() {
            findToken();
            return st.nextToken();
        }

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

    }
}

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

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

    static class TaskC {
        public void solve(int testNumber, Scan in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] data = new int[m + 1];
            for (int s = 0; s < n; s++) {
                data[in.nextInt() - 1]++;
                data[in.nextInt()]--;
            }
            ArrayList<Integer> p = new ArrayList<>();
            if (data[0] != 0) {
                p.add(data[0]);
            }
            for (int i = 1; i < m; i++) {
                data[i] += data[i - 1];
                if (data[i] != 0) {
                    p.add(data[i]);
                }
            }
            int f1 = mi(m, p);
            Collections.reverse(p);
            int f2 = mi(m, p);
            out.println(Math.max(f1, f2));

        }

        private int mi(int m, ArrayList<Integer> p) {
            int st = 0;
            int[] mmm = new int[m];
            Arrays.fill(mmm, Integer.MAX_VALUE);
            for (int s = 0; s < p.size(); s++) {
                if (st == 0) {
                    mmm[st++] = p.get(s);
                    continue;
                }
                int f = 0;
                int l = st;
                while (f + 1 < l) {
                    int r = (f + l) / 2;
                    if (mmm[r] <= p.get(s)) {
                        f = r;
                    } else {
                        l = r;
                    }
                }

                mmm[f + 1] = Math.min(mmm[f + 1], p.get(s));
                st = Math.max(st, f + 2);
            }
            return st;
        }

    }

    static class Scan {
        BufferedReader br;
        StringTokenizer st;

        public Scan() {
            this(System.in);
        }

        public Scan(BufferedReader br) {
            this.br = br;
        }

        public Scan(InputStream in) {
            this(new BufferedReader(new InputStreamReader(in)));
        }

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

        public String next() {
            findToken();
            return st.nextToken();
        }

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

    }
}

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

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

    static class TaskC {
        public void solve(int testNumber, Scan in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] data = new int[m + 1];
            for (int s = 0; s < n; s++) {
                data[in.nextInt() - 1]++;
                data[in.nextInt()]--;
            }
            for (int i = 1; i < m; i++) {
                data[i] += data[i - 1];
            }
            data = Arrays.copyOf(data, m);

//        out.println(Arrays.toString(data));

            ArrayList<Integer> p = new ArrayList<>();
            for (int i = 0; i < m; i++) {
                if (data[i] != 0) {
                    p.add(data[i]);
                }
            }

            int f1 = lengthOfLIS(p);
            Collections.reverse(p);
            int f2 = lengthOfLIS(p);
            out.println(Math.max(Math.max(f1, f2), m - p.size()));

        }

        public int lengthOfLIS(ArrayList<Integer> nums) {
            if (nums == null || nums.size() == 0)
                return 0;

            ArrayList<Integer> list = new ArrayList<Integer>();

            for (int num : nums) {
                if (list.size() == 0 || num >= list.get(list.size() - 1)) {
                    list.add(num);
                } else {
                    int i = 0;
                    int j = list.size() - 1;

                    while (i < j) {
                        int mid = (i + j) / 2;
                        if (list.get(mid) <= num) {
                            i = mid + 1;
                        } else {
                            j = mid;
                        }
                    }

                    list.set(j, num);
                }
            }

            return list.size();
        }

    }

    static class Scan {
        BufferedReader br;
        StringTokenizer st;

        public Scan() {
            this(System.in);
        }

        public Scan(BufferedReader br) {
            this.br = br;
        }

        public Scan(InputStream in) {
            this(new BufferedReader(new InputStreamReader(in)));
        }

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

        public String next() {
            findToken();
            return st.nextToken();
        }

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

    }
}

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

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

    static class TaskC {
        public void solve(int testNumber, Scan in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] data = new int[m + 1];
            for (int s = 0; s < n; s++) {
                data[in.nextInt() - 1]++;
                data[in.nextInt()]--;
            }
            for (int i = 1; i < m; i++) {
                data[i] += data[i - 1];
            }
            int[] ml = new int[m];
            int[] mr = new int[m];
            ml[0] = data[0];
            mr[m - 1] = data[m - 1];
            for (int s = 1; s < m; s++) {
                ml[s] = Math.max(ml[s - 1], data[s]);
            }
            for (int s = m - 2; s >= 0; s--) {
                mr[s] = Math.max(mr[s + 1], data[s]);
            }
            int know = 0;
            for (int i = 0; i < m; i++) {
                if (data[i] >= ml[i] || data[i] >= mr[i]) {
                    know++;
                }

            }
            out.println(know);
        }

    }

    static class Scan {
        BufferedReader br;
        StringTokenizer st;

        public Scan() {
            this(System.in);
        }

        public Scan(BufferedReader br) {
            this.br = br;
        }

        public Scan(InputStream in) {
            this(new BufferedReader(new InputStreamReader(in)));
        }

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

        public String next() {
            findToken();
            return st.nextToken();
        }

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

    }
}

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

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

    static class TaskC {
        public void solve(int testNumber, Scan in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] data = new int[m + 1];
            for (int s = 0; s < n; s++) {
                data[in.nextInt() - 1]++;
                data[in.nextInt()]--;
            }
            for (int i = 1; i < m; i++) {
                data[i] += data[i - 1];
            }
            data = Arrays.copyOf(data, m);

            int[] f1 = mi(data);
            for (int t = 0; t < m / 2; t++) {
                int l = data[t];
                data[t] = data[m - t - 1];
                data[m - t - 1] = l;
            }
            int[] f2 = mi(data);
            int mr = 0;
            for (int i = 0; i < m; i++) {
                mr = Math.max(mr, f1[i] + f2[m - i - 1] - 1);
            }
            out.println(mr);

        }

        private int[] mi(int[] x) {
            int st = 0;
            int[] mmm = new int[x.length];
            int[] ret = new int[x.length];
            Arrays.fill(mmm, Integer.MAX_VALUE);
            for (int s = 0; s < x.length; s++) {
                if (x[s] == 0) continue;
                if (st == 0 || x[s] >= mmm[st - 1]) {
                    mmm[st++] = x[s];
                    ret[s] = st;
                } else {
                    int f = 0;
                    int l = st;
                    while (f + 1 < l) {
                        int r = (f + l) / 2;
                        if (mmm[r] <= x[s]) {
                            f = r;
                        } else {
                            l = r;
                        }
                    }
                    mmm[f + 1] = x[s];
                    ret[s] = f + 1;
                }
            }
            return ret;
        }

    }

    static class Scan {
        BufferedReader br;
        StringTokenizer st;

        public Scan() {
            this(System.in);
        }

        public Scan(BufferedReader br) {
            this.br = br;
        }

        public Scan(InputStream in) {
            this(new BufferedReader(new InputStreamReader(in)));
        }

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

        public String next() {
            findToken();
            return st.nextToken();
        }

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

    }
}

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

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

    static class TaskC {
        public void solve(int testNumber, Scan in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] data = new int[m + 1];
            for (int s = 0; s < n; s++) {
                data[in.nextInt() - 1]++;
                data[in.nextInt()]--;
            }
            for (int i = 1; i < m; i++) {
                data[i] += data[i - 1];
            }
            data = Arrays.copyOf(data, m);

//        out.println(Arrays.toString(data));

            ArrayList<Integer> p = new ArrayList<>();
            for (int i = 0; i < m; i++) {
                if (data[i] != 0) {
                    p.add(data[i]);
                }
            }

            int f1 = mi(m, p);
            Collections.reverse(p);
            int f2 = mi(m, p);
            out.println(Math.max(Math.max(f1, f2), m - p.size()));

        }

        private int mi(int m, ArrayList<Integer> p) {
            int st = 0;
            int[] mmm = new int[m + 1];
            Arrays.fill(mmm, Integer.MAX_VALUE);
            mmm[0] = 0;
            for (int s = 0; s < p.size(); s++) {
                int f = 0;
                int l = st + 1;
                while (f + 1 < l) {
                    int r = (f + l) / 2;
                    if (mmm[r] <= p.get(s)) {
                        f = r;
                    } else {
                        l = r;
                    }
                }
                mmm[f + 1] = Math.min(mmm[f + 1], p.get(s));
                st = Math.max(st, f + 1);
            }
            return st;
        }

    }

    static class Scan {
        BufferedReader br;
        StringTokenizer st;

        public Scan() {
            this(System.in);
        }

        public Scan(BufferedReader br) {
            this.br = br;
        }

        public Scan(InputStream in) {
            this(new BufferedReader(new InputStreamReader(in)));
        }

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

        public String next() {
            findToken();
            return st.nextToken();
        }

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

    }
}

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

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

    static class TaskC {
        public void solve(int testNumber, Scan in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] data = new int[m + 1];
            for (int s = 0; s < n; s++) {
                data[in.nextInt() - 1]++;
                data[in.nextInt()]--;
            }
            for (int i = 1; i < m; i++) {
                data[i] += data[i - 1];
            }
            data = Arrays.copyOf(data, m);

//        out.println(Arrays.toString(data));

            ArrayList<Integer> p = new ArrayList<>();
            for (int i = 0; i < m; i++) {
                if (data[i] != 0) {
                    p.add(data[i]);
                }
            }

            int f1 = lengthOfLIS(p);
            Collections.reverse(p);
            int f2 = lengthOfLIS(p);
            out.println(Math.max(Math.max(f1, f2), 0));

        }

        public int lengthOfLIS(ArrayList<Integer> nums) {
//        System.out.println(nums);
            if (nums == null || nums.size() == 0)
                return 0;

            ArrayList<Integer> list = new ArrayList<Integer>();

            for (int num : nums) {
                if (list.size() == 0 || num >= list.get(list.size() - 1)) {
                    list.add(num);
                } else {
                    int i = 0;
                    int j = list.size() - 1;

                    while (i < j) {
                        int mid = (i + j) / 2;
                        if (list.get(mid) <= num) {
                            i = mid + 1;
                        } else {
                            j = mid;
                        }
                    }

                    list.set(j, num);
                }
            }

            return list.size();
        }

    }

    static class Scan {
        BufferedReader br;
        StringTokenizer st;

        public Scan() {
            this(System.in);
        }

        public Scan(BufferedReader br) {
            this.br = br;
        }

        public Scan(InputStream in) {
            this(new BufferedReader(new InputStreamReader(in)));
        }

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

        public String next() {
            findToken();
            return st.nextToken();
        }

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

    }
}

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

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

    static class TaskC {
        public void solve(int testNumber, Scan in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] data = new int[m + 1];
            for (int s = 0; s < n; s++) {
                data[in.nextInt() - 1]++;
                data[in.nextInt()]--;
            }
            for (int i = 1; i < m; i++) {
                data[i] += data[i - 1];
            }
            data = Arrays.copyOf(data, m);

//        out.println(Arrays.toString(data));

            ArrayList<Integer> p = new ArrayList<>();
            for (int i = 0; i < m; i++) {
                if (data[i] != 0) {
                    p.add(data[i]);
                }
            }

            int f1 = mi(m, p);
            Collections.reverse(p);
            int f2 = mi(m, p);
            out.println(Math.max(Math.max(f1, f2), m - p.size()));

        }

        private int mi(int m, ArrayList<Integer> p) {
            int st = 0;
            int[] mmm = new int[m];
            Arrays.fill(mmm, Integer.MAX_VALUE);
            for (int s = 0; s < p.size(); s++) {
                if (st == 0) {
                    mmm[st++] = p.get(s);
                    continue;
                }
                int f = 0;
                int l = st;
                while (f + 1 < l) {
                    int r = (f + l) / 2;
                    if (mmm[r] <= p.get(s)) {
                        f = r;
                    } else {
                        l = r;
                    }
                }

                mmm[f + 1] = Math.min(mmm[f + 1], p.get(s));
                st = Math.max(st, f + 2);
            }
            return st;
        }

    }

    static class Scan {
        BufferedReader br;
        StringTokenizer st;

        public Scan() {
            this(System.in);
        }

        public Scan(BufferedReader br) {
            this.br = br;
        }

        public Scan(InputStream in) {
            this(new BufferedReader(new InputStreamReader(in)));
        }

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

        public String next() {
            findToken();
            return st.nextToken();
        }

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

    }
}

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

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

    static class TaskC {
        public void solve(int testNumber, Scan in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] data = new int[m + 1];
            for (int s = 0; s < n; s++) {
                data[in.nextInt() - 1]++;
                data[in.nextInt()]--;
            }
            ArrayList<Integer> p = new ArrayList<>();
            if (data[0] != 0) {
                p.add(data[0]);
            }
            for (int i = 1; i < m; i++) {
                data[i] += data[i - 1];
                if (data[i] != 0) {
                    p.add(data[i]);
                }
            }
            int f1 = mi(m, p);
            Collections.reverse(p);
            int f2 = mi(m, p);
            out.println(Math.max(Math.max(f1, f2), m - p.size()));

        }

        private int mi(int m, ArrayList<Integer> p) {
            int st = 0;
            int[] mmm = new int[m];
            Arrays.fill(mmm, Integer.MAX_VALUE);
            for (int s = 0; s < p.size(); s++) {
                if (st == 0) {
                    mmm[st++] = p.get(s);
                    continue;
                }
                int f = 0;
                int l = st;
                while (f + 1 < l) {
                    int r = (f + l) / 2;
                    if (mmm[r] <= p.get(s)) {
                        f = r;
                    } else {
                        l = r;
                    }
                }

                mmm[f + 1] = Math.min(mmm[f + 1], p.get(s));
                st = Math.max(st, f + 2);
            }
            return st;
        }

    }

    static class Scan {
        BufferedReader br;
        StringTokenizer st;

        public Scan() {
            this(System.in);
        }

        public Scan(BufferedReader br) {
            this.br = br;
        }

        public Scan(InputStream in) {
            this(new BufferedReader(new InputStreamReader(in)));
        }

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

        public String next() {
            findToken();
            return st.nextToken();
        }

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

    }
}

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

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

    static class TaskC {
        static int binarySearch(ArrayList<Integer> arr, int val) {
            if (arr.size() == 0) return -1;
            if (val >= arr.get(arr.size() - 1)) return -(arr.size() + 1);

            int lo = 0, hi = arr.size() - 1, best = 0;
            for (int cnt = 0; cnt <= 21; cnt++) {
                int mid = lo + ((hi - lo) >> 1);
                if (arr.get(mid) > val) {
                    best = mid;
                    hi = Math.max(lo, mid - 1);
                } else {
                    lo = Math.min(hi, mid + 1);
                }
            }
            return best;
        }

        static int Prefix_LNS(ArrayList<Integer> arr) {
            int n = arr.size();
            int[] P = new int[n], in = new int[n];
            ArrayList<Integer> L = new ArrayList<>();
            ArrayList<Integer> res = new ArrayList<>();
            int lis = 0;

            for (int i = 0; i < n; i++) {
                int num = arr.get(i);
                int pos = binarySearch(L, num);
                if (pos < 0) pos = -pos - 1;
                res.add(pos + 1);
                if (pos + 1 > lis) {
                    lis++;
                    L.add(num);
                } else {
                    L.set(pos, num);
                }
                P[i] = pos > 0 ? in[pos - 1] : -1;
                in[pos] = i;
            }

            return L.size();
        }

        public void solve(int testNumber, Scanner sc, PrintWriter out) {
            int n = sc.nextInt(), m = sc.nextInt();
            int[] cnt = new int[m + 2];
            for (int i = 0; i < n; i++) {
                cnt[sc.nextInt()]++;
                cnt[sc.nextInt() + 1]--;
            }

            ArrayList<Integer> a = new ArrayList<>();
            int cum = 0;
            for (int i = 1; i <= m; i++) {
                a.add(cum += cnt[i]);
            }
            out.println(Prefix_LNS(a));
        }

    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

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

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

    }
}

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

public class C {

	public static void main(String[] args) {
//		int[] arr = {3,2,5,7,8,5,3,2,4,5,7,4,2,1,5,6,4};
//		System.out.println(Arrays.toString(arr));
//		System.out.println(Arrays.toString(LIS(arr)));
 		FastScanner sc = new FastScanner();
		int n = sc.nextInt();
		int m = sc.nextInt();
		int[] d = new int[m+1];
		for(int i = 0; i < n; i++) {
			int L = sc.nextInt()-1;
			int R = sc.nextInt()-1;
			d[L]++;
			d[R+1]--;
		}
		int[] h = new int[m];
		for(int i = 0; i < m; i++) {
			if(i > 0) h[i] += h[i-1];
			h[i] += d[i];
		}
//		System.out.println(Arrays.toString(h));
		int[] lispref = LIS(h);
		int[] lissuff = invert(LIS(invert(h)));
		int res = 0;
		for(int i = 0; i < n; i++) {
			res = Math.max(res, lispref[i] + lissuff[i] - 1);
		}
		System.out.println(res);
	}

	static int[] invert(int[] arr) {
		int n = arr.length;
		int[] res = new int[n];
		for(int i = 0; i < n; i++) {
			res[i] = arr[n-i-1];
		}
		return res;
	}
	static int[] LIS(int[] arr) {//Longest Increasing Subsequence
		int n = arr.length;
		int inf = (int) 1e9;
		int[] d = new int[n+1];
		Arrays.fill(d, inf);
		d[0] = -inf;
		int[] res = new int[n];
		for(int i = 0; i < n; i++) {
			int a = 0, b = n; //binsearch
			while(b - a > 1) {
				int c = (a+b)/2;
				if(d[c] <= arr[i]) a = c;
				else b = c;
			}
			res[i] = b;
			if(d[b] > arr[i]) d[b] = arr[i];
		}
		return res;
	}

	static class FastScanner {
		public BufferedReader reader;
		public StringTokenizer tokenizer;
		public FastScanner() {
			reader = new BufferedReader(new InputStreamReader(System.in), 32768);
			tokenizer = null;
		}
		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public double nextDouble() {
			return Double.parseDouble(next());
		}
		public String nextLine() {
			try {
				return reader.readLine();
			} catch(IOException e) {
				throw new RuntimeException(e);
			}
		}
	}

}

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

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

	static class TaskC {
		public void solve(int testNumber, FastScanner in, PrintWriter out) {
			int n = in.nextInt();
			int m = in.nextInt();
			int[] cov = new int[m];
			int[] add = new int[m];
			for (int i = 0; i < n; i++) {
				int l = in.nextInt() - 1;
				int r = in.nextInt() - 1;
				++add[l];
				if (r + 1 < m) {
					--add[r + 1];
				}
			}
			{
				int curCov = 0;
				for (int i = 0; i < m; i++) {
					curCov += add[i];
					cov[i] = curCov;
				}
			}

			int[] l = new int[m];
			for (int i = 0; i < m; i++) {
				l[i] = 1;
				if (i > 0 && cov[i - 1] <= cov[i]) {
					l[i] += l[i - 1];
				}
			}

			int[] r = new int[m];
			for (int i = m - 1; i >= 0; i--) {
				r[i] = 1;
				if (i + 1 < m && cov[i + 1] <= cov[i]) {
					r[i] += r[i + 1];
				}
			}

			int[] prefixL = l.clone();
			for (int i = 1; i < m; i++) {
				prefixL[i] = Math.max(prefixL[i], prefixL[i - 1]);
			}

			int[] suffixR = r.clone();
			for (int i = m - 2; i >= 0; i--) {
				suffixR[i] = Math.max(suffixR[i], suffixR[i + 1]);
			}

			int ans = 0;
			for (int i = 0; i + 1 < m; i++) {
				ans = Math.max(ans, prefixL[i] + suffixR[i + 1]);
			}
			out.println(ans);
		}

	}

	static class FastScanner {
		private BufferedReader in;
		private StringTokenizer st;

		public FastScanner(InputStream stream) {
			in = new BufferedReader(new InputStreamReader(stream));
		}

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

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

	}
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            if (m == 1) {
                out.println(0);
                return;
            }
            int[] psum = new int[m + 1];
            for (int i = 0; i < n; i++) {
                int a = in.nextInt() - 1, b = in.nextInt();
                psum[a]++;
                psum[b]--;
            }
            for (int i = 1; i <= m; i++) {
                psum[i] += psum[i - 1];
            }
            Debug.print(psum);
            int[] a1 = lis2(psum);
            for (int i = 1; i <= m; i++) {
                a1[i] = Math.max(a1[i - 1], a1[i]);
            }
            AUtils.reverse(psum);
            int[] a2 = lis2(psum);
            for (int i = 1; i <= m; i++) {
                a2[i] = Math.max(a2[i - 1], a2[i]);
            }
            Debug.print(a1, a2);
            int best = Math.max(a1[m], a2[m]);
            for (int sp = 0; sp < m; sp++) {
                best = Math.max(best, a1[sp] + a2[m - sp - 1]);
            }
            out.println(best);
        }

        public int[] lis2(int[] nums) {
            int n = nums.length;
            int[] dp = new int[n];
            for (int i = 0; i < n; i++) {
                if (nums[i] == 0) continue;
                dp[i] = 1;
                for (int j = 0; j < i; j++) {
                    if (nums[j] <= nums[i]) {
                        dp[i] = dp[i] > dp[j] + 1 ? dp[i] : dp[j] + 1;
                    }
                }
            }
            return dp;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1 << 16];
        private int curChar;
        private int numChars;

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

        public int read() {
            if (this.numChars == -1) {
                throw new InputMismatchException();
            } else {
                if (this.curChar >= this.numChars) {
                    this.curChar = 0;

                    try {
                        this.numChars = this.stream.read(this.buf);
                    } catch (IOException var2) {
                        throw new InputMismatchException();
                    }

                    if (this.numChars <= 0) {
                        return -1;
                    }
                }

                return this.buf[this.curChar++];
            }
        }

        public int nextInt() {
            int c;
            for (c = this.read(); isSpaceChar(c); c = this.read()) {
                ;
            }

            byte sgn = 1;
            if (c == 45) {
                sgn = -1;
                c = this.read();
            }

            int res = 0;

            while (c >= 48 && c <= 57) {
                res *= 10;
                res += c - 48;
                c = this.read();
                if (isSpaceChar(c)) {
                    return res * sgn;
                }
            }

            throw new InputMismatchException();
        }

        public static boolean isSpaceChar(int c) {
            return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
        }

    }

    static 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 close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class Debug {
        public static boolean DEBUG;

        static {
            try {
                DEBUG = System.getProperty("user.dir").contains("Dropbox");
            } catch (Exception e) {
                DEBUG = false;
            }
        }

        private static ArrayList<String> getParams() {
            StackTraceElement[] t = Thread.currentThread().getStackTrace();
            StackTraceElement up = t[3];

            ArrayList<String> ret = new ArrayList<>();
            String qqq = up.toString();
            ret.add("." + up.getMethodName() + qqq.substring(qqq.indexOf("("), qqq.length()));
            try {
                BufferedReader br = new BufferedReader(new FileReader(
                        new File("src/" + up.getClassName().replaceAll("\\.", "/") + ".java")));
                int g = up.getLineNumber();
                while (--g > 0) br.readLine();
                String q = br.readLine();
                String[] ss = q.split("Debug\\.print\\(");
                String[] qq = ss[1].substring(0, ss[1].lastIndexOf(")")).split(",");
                for (int i = 0; i < qq.length; i++) {
                    ret.add(qq[i].trim());
                }
            } catch (Exception e) {
            }
            for (int i = 0; i < 100; i++) ret.add("???");
            return ret;
        }

        private static String toString(Object o) {
            if (o == null) {
                return "null";
            } else if (o instanceof Object[]) {
                return Arrays.toString((Object[]) o);
            } else if (o instanceof char[]) {
                return new String((char[]) o);
            } else if (o instanceof int[]) {
                return Arrays.toString((int[]) o);
            } else if (o instanceof long[]) {
                return Arrays.toString((long[]) o);
            } else if (o instanceof double[]) {
                return Arrays.toString((double[]) o);
            } else if (o instanceof boolean[]) {
                return Arrays.toString((boolean[]) o);
            } else {
                return o.toString();
            }
        }

        public static void print(Object... x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.print(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.print(s.get(i + 1) + " = " + toString(x[i]));
                if (i != x.length - 1) System.out.print(", ");
            }
            System.out.println();
        }

        public static void print(int[] x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.println(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.println(s.get(1) + "[" + i + "] = " + x[i]);
            }
            System.out.println("***");
        }

    }

    static class AUtils {
        public static void reverse(int[] arr) {
            for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
                int t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
            }
        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public static int INF = 1 << 29;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            int[] psum = new int[m + 1];
            for (int i = 0; i < n; i++) {
                int a = in.nextInt() - 1, b = in.nextInt();
                psum[a]++;
                psum[b]--;
            }
            for (int i = 1; i <= m; i++) {
                psum[i] += psum[i - 1];
            }
            for (int i = 0; i <= m; i++) {
                if (psum[i] == 0) {
                    psum[i] = INF;
                }
            }
            Debug.print(psum);
            int[] a1 = lis(psum);
            AUtils.reverse(psum);
            int[] a2 = lis(psum);
            Debug.print(a1, a2);
            int best = 0;
            for (int sp = 0; sp + 1 < m; sp++) {
                best = Math.max(best, a1[sp] + a2[m - sp - 1]);
            }
            out.println(best);
        }

        public static int[] lis(int[] a) {
            int n = a.length;
            int[] tail = new int[n];

            int len = 0;
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                if (a[i] >= INF) continue;
                int pos = lower_bound(a, tail, len, a[i] + 1);
                res[i] = len = Math.max(len, pos + 1);
                tail[pos] = i;
            }
            return res;
        }

        static int lower_bound(int[] a, int[] tail, int len, int key) {
            int lo = -1;
            int hi = len;
            while (hi - lo > 1) {
                int mid = (lo + hi) >>> 1;
                if (a[tail[mid]] < key) {
                    lo = mid;
                } else {
                    hi = mid;
                }
            }
            return hi;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1 << 16];
        private int curChar;
        private int numChars;

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

        public int read() {
            if (this.numChars == -1) {
                throw new InputMismatchException();
            } else {
                if (this.curChar >= this.numChars) {
                    this.curChar = 0;

                    try {
                        this.numChars = this.stream.read(this.buf);
                    } catch (IOException var2) {
                        throw new InputMismatchException();
                    }

                    if (this.numChars <= 0) {
                        return -1;
                    }
                }

                return this.buf[this.curChar++];
            }
        }

        public int nextInt() {
            int c;
            for (c = this.read(); isSpaceChar(c); c = this.read()) {
                ;
            }

            byte sgn = 1;
            if (c == 45) {
                sgn = -1;
                c = this.read();
            }

            int res = 0;

            while (c >= 48 && c <= 57) {
                res *= 10;
                res += c - 48;
                c = this.read();
                if (isSpaceChar(c)) {
                    return res * sgn;
                }
            }

            throw new InputMismatchException();
        }

        public static boolean isSpaceChar(int c) {
            return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
        }

    }

    static 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 close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class Debug {
        public static boolean DEBUG;

        static {
            try {
                DEBUG = System.getProperty("user.dir").contains("Dropbox");
            } catch (Exception e) {
                DEBUG = false;
            }
        }

        private static ArrayList<String> getParams() {
            StackTraceElement[] t = Thread.currentThread().getStackTrace();
            StackTraceElement up = t[3];

            ArrayList<String> ret = new ArrayList<>();
            String qqq = up.toString();
            ret.add("." + up.getMethodName() + qqq.substring(qqq.indexOf("("), qqq.length()));
            try {
                BufferedReader br = new BufferedReader(new FileReader(
                        new File("src/" + up.getClassName().replaceAll("\\.", "/") + ".java")));
                int g = up.getLineNumber();
                while (--g > 0) br.readLine();
                String q = br.readLine();
                String[] ss = q.split("Debug\\.print\\(");
                String[] qq = ss[1].substring(0, ss[1].lastIndexOf(")")).split(",");
                for (int i = 0; i < qq.length; i++) {
                    ret.add(qq[i].trim());
                }
            } catch (Exception e) {
            }
            for (int i = 0; i < 100; i++) ret.add("???");
            return ret;
        }

        private static String toString(Object o) {
            if (o == null) {
                return "null";
            } else if (o instanceof Object[]) {
                return Arrays.toString((Object[]) o);
            } else if (o instanceof char[]) {
                return new String((char[]) o);
            } else if (o instanceof int[]) {
                return Arrays.toString((int[]) o);
            } else if (o instanceof long[]) {
                return Arrays.toString((long[]) o);
            } else if (o instanceof double[]) {
                return Arrays.toString((double[]) o);
            } else if (o instanceof boolean[]) {
                return Arrays.toString((boolean[]) o);
            } else {
                return o.toString();
            }
        }

        public static void print(Object... x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.print(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.print(s.get(i + 1) + " = " + toString(x[i]));
                if (i != x.length - 1) System.out.print(", ");
            }
            System.out.println();
        }

        public static void print(int[] x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.println(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.println(s.get(1) + "[" + i + "] = " + x[i]);
            }
            System.out.println("***");
        }

    }

    static class AUtils {
        public static void reverse(int[] arr) {
            for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
                int t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
            }
        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public static int INF = 1 << 29;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            int[] psum = new int[m + 1];
            for (int i = 0; i < n; i++) {
                int a = in.nextInt() - 1, b = in.nextInt();
                psum[a]++;
                psum[b]--;
            }
            for (int i = 1; i <= m; i++) {
                psum[i] += psum[i - 1];
            }
            for (int i = 0; i <= m; i++) {
                if (psum[i] == 0) {
                    psum[i] = INF;
                }
            }
            Debug.print(psum);
            int[] a1 = lis(psum);
            for (int i = 1; i <= m; i++) {
                a1[i] = Math.max(a1[i - 1], a1[i]);
            }
            AUtils.reverse(psum);
            int[] a2 = lis(psum);
            for (int i = 1; i <= m; i++) {
                a2[i] = Math.max(a2[i - 1], a2[i]);
            }
            Debug.print(a1, a2);
            int best = 0;
            for (int sp = 0; sp + 1 < m; sp++) {
                best = Math.max(best, a1[sp] + a2[m - sp - 1]);
            }
            out.println(best);
        }

        public static int[] lis(int[] a) {
            int n = a.length;
            int[] tail = new int[n];

            int len = 0;
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                if (a[i] >= INF) continue;
                int pos = lower_bound(a, tail, len, a[i] + 1);
                res[i] = len = Math.max(len, pos + 1);
                tail[pos] = i;
            }
            return res;
        }

        static int lower_bound(int[] a, int[] tail, int len, int key) {
            int lo = -1;
            int hi = len;
            while (hi - lo > 1) {
                int mid = (lo + hi) >>> 1;
                if (a[tail[mid]] < key) {
                    lo = mid;
                } else {
                    hi = mid;
                }
            }
            return hi;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1 << 16];
        private int curChar;
        private int numChars;

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

        public int read() {
            if (this.numChars == -1) {
                throw new InputMismatchException();
            } else {
                if (this.curChar >= this.numChars) {
                    this.curChar = 0;

                    try {
                        this.numChars = this.stream.read(this.buf);
                    } catch (IOException var2) {
                        throw new InputMismatchException();
                    }

                    if (this.numChars <= 0) {
                        return -1;
                    }
                }

                return this.buf[this.curChar++];
            }
        }

        public int nextInt() {
            int c;
            for (c = this.read(); isSpaceChar(c); c = this.read()) {
                ;
            }

            byte sgn = 1;
            if (c == 45) {
                sgn = -1;
                c = this.read();
            }

            int res = 0;

            while (c >= 48 && c <= 57) {
                res *= 10;
                res += c - 48;
                c = this.read();
                if (isSpaceChar(c)) {
                    return res * sgn;
                }
            }

            throw new InputMismatchException();
        }

        public static boolean isSpaceChar(int c) {
            return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
        }

    }

    static 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 close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class Debug {
        public static boolean DEBUG;

        static {
            try {
                DEBUG = System.getProperty("user.dir").contains("Dropbox");
            } catch (Exception e) {
                DEBUG = false;
            }
        }

        private static ArrayList<String> getParams() {
            StackTraceElement[] t = Thread.currentThread().getStackTrace();
            StackTraceElement up = t[3];

            ArrayList<String> ret = new ArrayList<>();
            String qqq = up.toString();
            ret.add("." + up.getMethodName() + qqq.substring(qqq.indexOf("("), qqq.length()));
            try {
                BufferedReader br = new BufferedReader(new FileReader(
                        new File("src/" + up.getClassName().replaceAll("\\.", "/") + ".java")));
                int g = up.getLineNumber();
                while (--g > 0) br.readLine();
                String q = br.readLine();
                String[] ss = q.split("Debug\\.print\\(");
                String[] qq = ss[1].substring(0, ss[1].lastIndexOf(")")).split(",");
                for (int i = 0; i < qq.length; i++) {
                    ret.add(qq[i].trim());
                }
            } catch (Exception e) {
            }
            for (int i = 0; i < 100; i++) ret.add("???");
            return ret;
        }

        private static String toString(Object o) {
            if (o == null) {
                return "null";
            } else if (o instanceof Object[]) {
                return Arrays.toString((Object[]) o);
            } else if (o instanceof char[]) {
                return new String((char[]) o);
            } else if (o instanceof int[]) {
                return Arrays.toString((int[]) o);
            } else if (o instanceof long[]) {
                return Arrays.toString((long[]) o);
            } else if (o instanceof double[]) {
                return Arrays.toString((double[]) o);
            } else if (o instanceof boolean[]) {
                return Arrays.toString((boolean[]) o);
            } else {
                return o.toString();
            }
        }

        public static void print(Object... x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.print(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.print(s.get(i + 1) + " = " + toString(x[i]));
                if (i != x.length - 1) System.out.print(", ");
            }
            System.out.println();
        }

        public static void print(int[] x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.println(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.println(s.get(1) + "[" + i + "] = " + x[i]);
            }
            System.out.println("***");
        }

    }

    static class AUtils {
        public static void reverse(int[] arr) {
            for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
                int t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
            }
        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public static int INF = 1 << 29;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            int[] psum = new int[m + 1];
            for (int i = 0; i < n; i++) {
                int a = in.nextInt() - 1, b = in.nextInt();
                psum[a]++;
                psum[b]--;
            }
            for (int i = 1; i <= m; i++) {
                psum[i] += psum[i - 1];
            }
            for (int i = 0; i <= m; i++) {
                if (psum[i] == 0) {
                    psum[i] = INF;
                }
            }
            Debug.print(psum);
            int[] a1 = lis(psum);
            for (int i = 1; i <= m; i++) {
                a1[i] = Math.max(a1[i - 1], a1[i]);
            }
            AUtils.reverse(psum);
            int[] a2 = lis(psum);
            for (int i = 1; i <= m; i++) {
                a2[i] = Math.max(a2[i - 1], a2[i]);
            }
            Debug.print(a1, a2);
            int best = 0;
            for (int sp = 0; sp < m; sp++) {
                best = Math.max(best, a1[sp] + a2[m - sp - 1]);
            }
            out.println(best);
        }

        public static int[] lis(int[] a) {
            int n = a.length;
            int[] tail = new int[n];

            int len = 0;
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                if (a[i] >= INF) continue;
                int pos = lower_bound(a, tail, len, a[i] + 1);
                res[i] = len = Math.max(len, pos + 1);
                tail[pos] = i;
            }
            return res;
        }

        static int lower_bound(int[] a, int[] tail, int len, int key) {
            int lo = -1;
            int hi = len;
            while (hi - lo > 1) {
                int mid = (lo + hi) >>> 1;
                if (a[tail[mid]] < key) {
                    lo = mid;
                } else {
                    hi = mid;
                }
            }
            return hi;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1 << 16];
        private int curChar;
        private int numChars;

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

        public int read() {
            if (this.numChars == -1) {
                throw new InputMismatchException();
            } else {
                if (this.curChar >= this.numChars) {
                    this.curChar = 0;

                    try {
                        this.numChars = this.stream.read(this.buf);
                    } catch (IOException var2) {
                        throw new InputMismatchException();
                    }

                    if (this.numChars <= 0) {
                        return -1;
                    }
                }

                return this.buf[this.curChar++];
            }
        }

        public int nextInt() {
            int c;
            for (c = this.read(); isSpaceChar(c); c = this.read()) {
                ;
            }

            byte sgn = 1;
            if (c == 45) {
                sgn = -1;
                c = this.read();
            }

            int res = 0;

            while (c >= 48 && c <= 57) {
                res *= 10;
                res += c - 48;
                c = this.read();
                if (isSpaceChar(c)) {
                    return res * sgn;
                }
            }

            throw new InputMismatchException();
        }

        public static boolean isSpaceChar(int c) {
            return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
        }

    }

    static 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 close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class Debug {
        public static boolean DEBUG;

        static {
            try {
                DEBUG = System.getProperty("user.dir").contains("Dropbox");
            } catch (Exception e) {
                DEBUG = false;
            }
        }

        private static ArrayList<String> getParams() {
            StackTraceElement[] t = Thread.currentThread().getStackTrace();
            StackTraceElement up = t[3];

            ArrayList<String> ret = new ArrayList<>();
            String qqq = up.toString();
            ret.add("." + up.getMethodName() + qqq.substring(qqq.indexOf("("), qqq.length()));
            try {
                BufferedReader br = new BufferedReader(new FileReader(
                        new File("src/" + up.getClassName().replaceAll("\\.", "/") + ".java")));
                int g = up.getLineNumber();
                while (--g > 0) br.readLine();
                String q = br.readLine();
                String[] ss = q.split("Debug\\.print\\(");
                String[] qq = ss[1].substring(0, ss[1].lastIndexOf(")")).split(",");
                for (int i = 0; i < qq.length; i++) {
                    ret.add(qq[i].trim());
                }
            } catch (Exception e) {
            }
            for (int i = 0; i < 100; i++) ret.add("???");
            return ret;
        }

        private static String toString(Object o) {
            if (o == null) {
                return "null";
            } else if (o instanceof Object[]) {
                return Arrays.toString((Object[]) o);
            } else if (o instanceof char[]) {
                return new String((char[]) o);
            } else if (o instanceof int[]) {
                return Arrays.toString((int[]) o);
            } else if (o instanceof long[]) {
                return Arrays.toString((long[]) o);
            } else if (o instanceof double[]) {
                return Arrays.toString((double[]) o);
            } else if (o instanceof boolean[]) {
                return Arrays.toString((boolean[]) o);
            } else {
                return o.toString();
            }
        }

        public static void print(Object... x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.print(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.print(s.get(i + 1) + " = " + toString(x[i]));
                if (i != x.length - 1) System.out.print(", ");
            }
            System.out.println();
        }

        public static void print(int[] x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.println(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.println(s.get(1) + "[" + i + "] = " + x[i]);
            }
            System.out.println("***");
        }

    }

    static class AUtils {
        public static void reverse(int[] arr) {
            for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
                int t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
            }
        }

    }
}

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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            int[] psum = new int[m + 1];
            for (int i = 0; i < n; i++) {
                int a = in.nextInt() - 1, b = in.nextInt();
                psum[a]++;
                psum[b]--;
            }
            for (int i = 1; i <= m; i++) {
                psum[i] += psum[i - 1];
            }
            int[] a1 = lis(psum);
            for (int i = 1; i <= m; i++) {
                a1[i] = Math.max(a1[i - 1], a1[i]);
            }
            AUtils.reverse(psum);
            int[] a2 = lis(psum);
            for (int i = 1; i <= m; i++) {
                a2[i] = Math.max(a2[i - 1], a2[i]);
            }
            int best = a2[m];
            for (int sp = 0; sp < m; sp++) {
                best = Math.max(best, a1[sp] + a2[m - sp - 1]);
            }
            out.println(best);
        }

        public static int[] lis(int[] a) {
            int n = a.length;
            int[] tail = new int[n];

            int len = 0;
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                if (a[i] == 0) continue;
                int pos = lower_bound(a, tail, len, a[i] + 1);
                res[i] = len = Math.max(len, pos + 1);
                tail[pos] = i;
            }
            return res;
        }

        static int lower_bound(int[] a, int[] tail, int len, int key) {
            int lo = -1;
            int hi = len;
            while (hi - lo > 1) {
                int mid = (lo + hi) >>> 1;
                if (a[tail[mid]] < key) {
                    lo = mid;
                } else {
                    hi = mid;
                }
            }
            return hi;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1 << 16];
        private int curChar;
        private int numChars;

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

        public int read() {
            if (this.numChars == -1) {
                throw new InputMismatchException();
            } else {
                if (this.curChar >= this.numChars) {
                    this.curChar = 0;

                    try {
                        this.numChars = this.stream.read(this.buf);
                    } catch (IOException var2) {
                        throw new InputMismatchException();
                    }

                    if (this.numChars <= 0) {
                        return -1;
                    }
                }

                return this.buf[this.curChar++];
            }
        }

        public int nextInt() {
            int c;
            for (c = this.read(); isSpaceChar(c); c = this.read()) {
                ;
            }

            byte sgn = 1;
            if (c == 45) {
                sgn = -1;
                c = this.read();
            }

            int res = 0;

            while (c >= 48 && c <= 57) {
                res *= 10;
                res += c - 48;
                c = this.read();
                if (isSpaceChar(c)) {
                    return res * sgn;
                }
            }

            throw new InputMismatchException();
        }

        public static boolean isSpaceChar(int c) {
            return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
        }

    }

    static 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 close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class AUtils {
        public static void reverse(int[] arr) {
            for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
                int t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
            }
        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            int[] psum = new int[m + 1];
            for (int i = 0; i < n; i++) {
                int a = in.nextInt() - 1, b = in.nextInt();
                psum[a]++;
                psum[b]--;
            }
            for (int i = 1; i <= m; i++) {
                psum[i] += psum[i - 1];
            }
            for (int i = 0; i <= m; i++) {
                if (psum[i] == 0) {
                    psum[i] = 1 << 29;
                }
            }
            Debug.print(psum);
            int[] a1 = lis(psum);
            AUtils.reverse(psum);
            int[] a2 = lis(psum);
            Debug.print(a1, a2);
            int best = 0;
            for (int sp = 0; sp + 1 < m; sp++) {
                best = Math.max(best, a1[sp] + a2[m - sp - 1]);
            }
            out.println(best);
        }

        public static int[] lis(int[] a) {
            int n = a.length;
            int[] tail = new int[n];
            int[] prev = new int[n];

            int len = 0;
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                int pos = lower_bound(a, tail, len, a[i] + 1);
                res[i] = len = Math.max(len, pos + 1);
                prev[i] = pos > 0 ? tail[pos - 1] : -1;
                tail[pos] = i;
            }
            return res;
        }

        static int lower_bound(int[] a, int[] tail, int len, int key) {
            if (key > 1 << 29) {
                return 0;
            }
            int lo = -1;
            int hi = len;
            while (hi - lo > 1) {
                int mid = (lo + hi) >>> 1;
                if (a[tail[mid]] < key) {
                    lo = mid;
                } else {
                    hi = mid;
                }
            }
            return hi;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1 << 16];
        private int curChar;
        private int numChars;

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

        public int read() {
            if (this.numChars == -1) {
                throw new InputMismatchException();
            } else {
                if (this.curChar >= this.numChars) {
                    this.curChar = 0;

                    try {
                        this.numChars = this.stream.read(this.buf);
                    } catch (IOException var2) {
                        throw new InputMismatchException();
                    }

                    if (this.numChars <= 0) {
                        return -1;
                    }
                }

                return this.buf[this.curChar++];
            }
        }

        public int nextInt() {
            int c;
            for (c = this.read(); isSpaceChar(c); c = this.read()) {
                ;
            }

            byte sgn = 1;
            if (c == 45) {
                sgn = -1;
                c = this.read();
            }

            int res = 0;

            while (c >= 48 && c <= 57) {
                res *= 10;
                res += c - 48;
                c = this.read();
                if (isSpaceChar(c)) {
                    return res * sgn;
                }
            }

            throw new InputMismatchException();
        }

        public static boolean isSpaceChar(int c) {
            return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
        }

    }

    static 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 close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class Debug {
        public static boolean DEBUG;

        static {
            try {
                DEBUG = System.getProperty("user.dir").contains("Dropbox");
            } catch (Exception e) {
                DEBUG = false;
            }
        }

        private static ArrayList<String> getParams() {
            StackTraceElement[] t = Thread.currentThread().getStackTrace();
            StackTraceElement up = t[3];

            ArrayList<String> ret = new ArrayList<>();
            String qqq = up.toString();
            ret.add("." + up.getMethodName() + qqq.substring(qqq.indexOf("("), qqq.length()));
            try {
                BufferedReader br = new BufferedReader(new FileReader(
                        new File("src/" + up.getClassName().replaceAll("\\.", "/") + ".java")));
                int g = up.getLineNumber();
                while (--g > 0) br.readLine();
                String q = br.readLine();
                String[] ss = q.split("Debug\\.print\\(");
                String[] qq = ss[1].substring(0, ss[1].lastIndexOf(")")).split(",");
                for (int i = 0; i < qq.length; i++) {
                    ret.add(qq[i].trim());
                }
            } catch (Exception e) {
            }
            for (int i = 0; i < 100; i++) ret.add("???");
            return ret;
        }

        private static String toString(Object o) {
            if (o == null) {
                return "null";
            } else if (o instanceof Object[]) {
                return Arrays.toString((Object[]) o);
            } else if (o instanceof char[]) {
                return new String((char[]) o);
            } else if (o instanceof int[]) {
                return Arrays.toString((int[]) o);
            } else if (o instanceof long[]) {
                return Arrays.toString((long[]) o);
            } else if (o instanceof double[]) {
                return Arrays.toString((double[]) o);
            } else if (o instanceof boolean[]) {
                return Arrays.toString((boolean[]) o);
            } else {
                return o.toString();
            }
        }

        public static void print(Object... x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.print(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.print(s.get(i + 1) + " = " + toString(x[i]));
                if (i != x.length - 1) System.out.print(", ");
            }
            System.out.println();
        }

        public static void print(int[] x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.println(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.println(s.get(1) + "[" + i + "] = " + x[i]);
            }
            System.out.println("***");
        }

    }

    static class AUtils {
        public static void reverse(int[] arr) {
            for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
                int t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
            }
        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public static int INF = 1 << 29;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            int[] psum = new int[m + 1];
            for (int i = 0; i < n; i++) {
                int a = in.nextInt() - 1, b = in.nextInt();
                psum[a]++;
                psum[b]--;
            }
            for (int i = 1; i <= m; i++) {
                psum[i] += psum[i - 1];
            }
            for (int i = 0; i <= m; i++) {
                if (psum[i] == 0) {
                    psum[i] = INF;
                }
            }
            Debug.print(psum);
            int[] a1 = lis(psum);
            AUtils.reverse(psum);
            int[] a2 = lis(psum);
            Debug.print(a1, a2);
            int best = 0;
            for (int sp = 0; sp + 1 < m; sp++) {
                best = Math.max(best, a1[sp] + a2[m - sp - 1]);
            }
            out.println(best);
        }

        public static int[] lis(int[] a) {
            int n = a.length;
            int[] tail = new int[n];

            int len = 0;
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                if (a[i] > INF) continue;
                int pos = lower_bound(a, tail, len, a[i] + 1);
                res[i] = len = Math.max(len, pos + 1);
                tail[pos] = i;
            }
            return res;
        }

        static int lower_bound(int[] a, int[] tail, int len, int key) {
            int lo = -1;
            int hi = len;
            while (hi - lo > 1) {
                int mid = (lo + hi) >>> 1;
                if (a[tail[mid]] < key) {
                    lo = mid;
                } else {
                    hi = mid;
                }
            }
            return hi;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1 << 16];
        private int curChar;
        private int numChars;

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

        public int read() {
            if (this.numChars == -1) {
                throw new InputMismatchException();
            } else {
                if (this.curChar >= this.numChars) {
                    this.curChar = 0;

                    try {
                        this.numChars = this.stream.read(this.buf);
                    } catch (IOException var2) {
                        throw new InputMismatchException();
                    }

                    if (this.numChars <= 0) {
                        return -1;
                    }
                }

                return this.buf[this.curChar++];
            }
        }

        public int nextInt() {
            int c;
            for (c = this.read(); isSpaceChar(c); c = this.read()) {
                ;
            }

            byte sgn = 1;
            if (c == 45) {
                sgn = -1;
                c = this.read();
            }

            int res = 0;

            while (c >= 48 && c <= 57) {
                res *= 10;
                res += c - 48;
                c = this.read();
                if (isSpaceChar(c)) {
                    return res * sgn;
                }
            }

            throw new InputMismatchException();
        }

        public static boolean isSpaceChar(int c) {
            return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
        }

    }

    static 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 close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class Debug {
        public static boolean DEBUG;

        static {
            try {
                DEBUG = System.getProperty("user.dir").contains("Dropbox");
            } catch (Exception e) {
                DEBUG = false;
            }
        }

        private static ArrayList<String> getParams() {
            StackTraceElement[] t = Thread.currentThread().getStackTrace();
            StackTraceElement up = t[3];

            ArrayList<String> ret = new ArrayList<>();
            String qqq = up.toString();
            ret.add("." + up.getMethodName() + qqq.substring(qqq.indexOf("("), qqq.length()));
            try {
                BufferedReader br = new BufferedReader(new FileReader(
                        new File("src/" + up.getClassName().replaceAll("\\.", "/") + ".java")));
                int g = up.getLineNumber();
                while (--g > 0) br.readLine();
                String q = br.readLine();
                String[] ss = q.split("Debug\\.print\\(");
                String[] qq = ss[1].substring(0, ss[1].lastIndexOf(")")).split(",");
                for (int i = 0; i < qq.length; i++) {
                    ret.add(qq[i].trim());
                }
            } catch (Exception e) {
            }
            for (int i = 0; i < 100; i++) ret.add("???");
            return ret;
        }

        private static String toString(Object o) {
            if (o == null) {
                return "null";
            } else if (o instanceof Object[]) {
                return Arrays.toString((Object[]) o);
            } else if (o instanceof char[]) {
                return new String((char[]) o);
            } else if (o instanceof int[]) {
                return Arrays.toString((int[]) o);
            } else if (o instanceof long[]) {
                return Arrays.toString((long[]) o);
            } else if (o instanceof double[]) {
                return Arrays.toString((double[]) o);
            } else if (o instanceof boolean[]) {
                return Arrays.toString((boolean[]) o);
            } else {
                return o.toString();
            }
        }

        public static void print(Object... x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.print(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.print(s.get(i + 1) + " = " + toString(x[i]));
                if (i != x.length - 1) System.out.print(", ");
            }
            System.out.println();
        }

        public static void print(int[] x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.println(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.println(s.get(1) + "[" + i + "] = " + x[i]);
            }
            System.out.println("***");
        }

    }

    static class AUtils {
        public static void reverse(int[] arr) {
            for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
                int t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
            }
        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            int[] psum = new int[m + 1];
            for (int i = 0; i < n; i++) {
                int a = in.nextInt() - 1, b = in.nextInt();
                psum[a]++;
                psum[b]--;
            }
            for (int i = 1; i <= m; i++) {
                psum[i] += psum[i - 1];
            }
            for (int i = 0; i <= m; i++) {
                if (psum[i] == 0) {
                    psum[i] = 1 << 29;
                }
            }
            int[] a1 = lis(psum);
            AUtils.reverse(psum);
            int[] a2 = lis(psum);
            Debug.print(a1, a2);
            int best = 0;
            for (int sp = 0; sp + 1 < m; sp++) {
                best = Math.max(best, a1[sp] + a2[m - sp - 1]);
            }
            out.println(best);
        }

        public static int[] lis(int[] a) {
            int n = a.length;
            int[] tail = new int[n];
            int[] prev = new int[n];

            int len = 0;
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                int pos = lower_bound(a, tail, len, a[i] + 1);
                res[i] = len = Math.max(len, pos + 1);
                prev[i] = pos > 0 ? tail[pos - 1] : -1;
                tail[pos] = i;
            }
            return res;
        }

        static int lower_bound(int[] a, int[] tail, int len, int key) {
            int lo = -1;
            int hi = len;
            while (hi - lo > 1) {
                int mid = (lo + hi) >>> 1;
                if (a[tail[mid]] < key) {
                    lo = mid;
                } else {
                    hi = mid;
                }
            }
            return hi;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1 << 16];
        private int curChar;
        private int numChars;

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

        public int read() {
            if (this.numChars == -1) {
                throw new InputMismatchException();
            } else {
                if (this.curChar >= this.numChars) {
                    this.curChar = 0;

                    try {
                        this.numChars = this.stream.read(this.buf);
                    } catch (IOException var2) {
                        throw new InputMismatchException();
                    }

                    if (this.numChars <= 0) {
                        return -1;
                    }
                }

                return this.buf[this.curChar++];
            }
        }

        public int nextInt() {
            int c;
            for (c = this.read(); isSpaceChar(c); c = this.read()) {
                ;
            }

            byte sgn = 1;
            if (c == 45) {
                sgn = -1;
                c = this.read();
            }

            int res = 0;

            while (c >= 48 && c <= 57) {
                res *= 10;
                res += c - 48;
                c = this.read();
                if (isSpaceChar(c)) {
                    return res * sgn;
                }
            }

            throw new InputMismatchException();
        }

        public static boolean isSpaceChar(int c) {
            return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
        }

    }

    static 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 close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class Debug {
        public static boolean DEBUG;

        static {
            try {
                DEBUG = System.getProperty("user.dir").contains("Dropbox");
            } catch (Exception e) {
                DEBUG = false;
            }
        }

        private static ArrayList<String> getParams() {
            StackTraceElement[] t = Thread.currentThread().getStackTrace();
            StackTraceElement up = t[3];

            ArrayList<String> ret = new ArrayList<>();
            String qqq = up.toString();
            ret.add("." + up.getMethodName() + qqq.substring(qqq.indexOf("("), qqq.length()));
            try {
                BufferedReader br = new BufferedReader(new FileReader(
                        new File("src/" + up.getClassName().replaceAll("\\.", "/") + ".java")));
                int g = up.getLineNumber();
                while (--g > 0) br.readLine();
                String q = br.readLine();
                String[] ss = q.split("Debug\\.print\\(");
                String[] qq = ss[1].substring(0, ss[1].lastIndexOf(")")).split(",");
                for (int i = 0; i < qq.length; i++) {
                    ret.add(qq[i].trim());
                }
            } catch (Exception e) {
            }
            for (int i = 0; i < 100; i++) ret.add("???");
            return ret;
        }

        private static String toString(Object o) {
            if (o == null) {
                return "null";
            } else if (o instanceof Object[]) {
                return Arrays.toString((Object[]) o);
            } else if (o instanceof char[]) {
                return new String((char[]) o);
            } else if (o instanceof int[]) {
                return Arrays.toString((int[]) o);
            } else if (o instanceof long[]) {
                return Arrays.toString((long[]) o);
            } else if (o instanceof double[]) {
                return Arrays.toString((double[]) o);
            } else if (o instanceof boolean[]) {
                return Arrays.toString((boolean[]) o);
            } else {
                return o.toString();
            }
        }

        public static void print(Object... x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.print(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.print(s.get(i + 1) + " = " + toString(x[i]));
                if (i != x.length - 1) System.out.print(", ");
            }
            System.out.println();
        }

    }

    static class AUtils {
        public static void reverse(int[] arr) {
            for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
                int t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
            }
        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            int[] psum = new int[m + 1];
            for (int i = 0; i < n; i++) {
                int a = in.nextInt() - 1, b = in.nextInt();
                psum[a]++;
                psum[b]--;
            }
            for (int i = 1; i <= m; i++) {
                psum[i] += psum[i - 1];
            }
            Debug.print(psum);
            int[] a1 = lis(psum);
            for (int i = 1; i <= m; i++) {
                a1[i] = Math.max(a1[i - 1], a1[i]);
            }
            AUtils.reverse(psum);
            int[] a2 = lis(psum);
            for (int i = 1; i <= m; i++) {
                a2[i] = Math.max(a2[i - 1], a2[i]);
            }
            Debug.print(a1, a2);
            int best = Math.max(a1[m], a2[m]);
            for (int sp = 0; sp < m; sp++) {
                best = Math.max(best, a1[sp] + a2[m - sp - 1]);
            }
            out.println(best);
        }

        public static int[] lis(int[] a) {
            int n = a.length;
            int[] tail = new int[n];

            int len = 0;
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                if (a[i] == 0) continue;
                int pos = lower_bound(a, tail, len, a[i] + 1);
                res[i] = len = Math.max(len, pos + 1);
                tail[pos] = i;
            }
            return res;
        }

        static int lower_bound(int[] a, int[] tail, int len, int key) {
            int lo = -1;
            int hi = len;
            while (hi - lo > 1) {
                int mid = (lo + hi) >>> 1;
                if (a[tail[mid]] < key) {
                    lo = mid;
                } else {
                    hi = mid;
                }
            }
            return hi;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1 << 16];
        private int curChar;
        private int numChars;

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

        public int read() {
            if (this.numChars == -1) {
                throw new InputMismatchException();
            } else {
                if (this.curChar >= this.numChars) {
                    this.curChar = 0;

                    try {
                        this.numChars = this.stream.read(this.buf);
                    } catch (IOException var2) {
                        throw new InputMismatchException();
                    }

                    if (this.numChars <= 0) {
                        return -1;
                    }
                }

                return this.buf[this.curChar++];
            }
        }

        public int nextInt() {
            int c;
            for (c = this.read(); isSpaceChar(c); c = this.read()) {
                ;
            }

            byte sgn = 1;
            if (c == 45) {
                sgn = -1;
                c = this.read();
            }

            int res = 0;

            while (c >= 48 && c <= 57) {
                res *= 10;
                res += c - 48;
                c = this.read();
                if (isSpaceChar(c)) {
                    return res * sgn;
                }
            }

            throw new InputMismatchException();
        }

        public static boolean isSpaceChar(int c) {
            return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
        }

    }

    static 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 close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class Debug {
        public static boolean DEBUG;

        static {
            try {
                DEBUG = System.getProperty("user.dir").contains("Dropbox");
            } catch (Exception e) {
                DEBUG = false;
            }
        }

        private static ArrayList<String> getParams() {
            StackTraceElement[] t = Thread.currentThread().getStackTrace();
            StackTraceElement up = t[3];

            ArrayList<String> ret = new ArrayList<>();
            String qqq = up.toString();
            ret.add("." + up.getMethodName() + qqq.substring(qqq.indexOf("("), qqq.length()));
            try {
                BufferedReader br = new BufferedReader(new FileReader(
                        new File("src/" + up.getClassName().replaceAll("\\.", "/") + ".java")));
                int g = up.getLineNumber();
                while (--g > 0) br.readLine();
                String q = br.readLine();
                String[] ss = q.split("Debug\\.print\\(");
                String[] qq = ss[1].substring(0, ss[1].lastIndexOf(")")).split(",");
                for (int i = 0; i < qq.length; i++) {
                    ret.add(qq[i].trim());
                }
            } catch (Exception e) {
            }
            for (int i = 0; i < 100; i++) ret.add("???");
            return ret;
        }

        private static String toString(Object o) {
            if (o == null) {
                return "null";
            } else if (o instanceof Object[]) {
                return Arrays.toString((Object[]) o);
            } else if (o instanceof char[]) {
                return new String((char[]) o);
            } else if (o instanceof int[]) {
                return Arrays.toString((int[]) o);
            } else if (o instanceof long[]) {
                return Arrays.toString((long[]) o);
            } else if (o instanceof double[]) {
                return Arrays.toString((double[]) o);
            } else if (o instanceof boolean[]) {
                return Arrays.toString((boolean[]) o);
            } else {
                return o.toString();
            }
        }

        public static void print(Object... x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.print(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.print(s.get(i + 1) + " = " + toString(x[i]));
                if (i != x.length - 1) System.out.print(", ");
            }
            System.out.println();
        }

        public static void print(int[] x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.println(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.println(s.get(1) + "[" + i + "] = " + x[i]);
            }
            System.out.println("***");
        }

    }

    static class AUtils {
        public static void reverse(int[] arr) {
            for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
                int t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
            }
        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            if (m == 1) {
                out.println(0);
                return;
            }
            int[] psum = new int[m + 1];
            for (int i = 0; i < n; i++) {
                int a = in.nextInt() - 1, b = in.nextInt();
                psum[a]++;
                psum[b]--;
            }
            for (int i = 1; i <= m; i++) {
                psum[i] += psum[i - 1];
            }
            Debug.print(psum);
            int[] a1 = lis(psum);
            for (int i = 1; i <= m; i++) {
                a1[i] = Math.max(a1[i - 1], a1[i]);
            }
            AUtils.reverse(psum);
            int[] a2 = lis(psum);
            for (int i = 1; i <= m; i++) {
                a2[i] = Math.max(a2[i - 1], a2[i]);
            }
            Debug.print(a1, a2);
            int best = Math.max(a1[m], a2[m]);
            for (int sp = 0; sp < m; sp++) {
                best = Math.max(best, a1[sp] + a2[m - sp - 1]);
            }
            out.println(best);
        }

        public static int[] lis(int[] a) {
            int n = a.length;
            int[] tail = new int[n];

            int len = 0;
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                if (a[i] == 0) continue;
                int pos = lower_bound(a, tail, len, a[i] + 1);
                res[i] = len = Math.max(len, pos + 1);
                tail[pos] = i;
            }
            return res;
        }

        static int lower_bound(int[] a, int[] tail, int len, int key) {
            int lo = -1;
            int hi = len;
            while (hi - lo > 1) {
                int mid = (lo + hi) >>> 1;
                if (a[tail[mid]] < key) {
                    lo = mid;
                } else {
                    hi = mid;
                }
            }
            return hi;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1 << 16];
        private int curChar;
        private int numChars;

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

        public int read() {
            if (this.numChars == -1) {
                throw new InputMismatchException();
            } else {
                if (this.curChar >= this.numChars) {
                    this.curChar = 0;

                    try {
                        this.numChars = this.stream.read(this.buf);
                    } catch (IOException var2) {
                        throw new InputMismatchException();
                    }

                    if (this.numChars <= 0) {
                        return -1;
                    }
                }

                return this.buf[this.curChar++];
            }
        }

        public int nextInt() {
            int c;
            for (c = this.read(); isSpaceChar(c); c = this.read()) {
                ;
            }

            byte sgn = 1;
            if (c == 45) {
                sgn = -1;
                c = this.read();
            }

            int res = 0;

            while (c >= 48 && c <= 57) {
                res *= 10;
                res += c - 48;
                c = this.read();
                if (isSpaceChar(c)) {
                    return res * sgn;
                }
            }

            throw new InputMismatchException();
        }

        public static boolean isSpaceChar(int c) {
            return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
        }

    }

    static 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 close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class Debug {
        public static boolean DEBUG;

        static {
            try {
                DEBUG = System.getProperty("user.dir").contains("Dropbox");
            } catch (Exception e) {
                DEBUG = false;
            }
        }

        private static ArrayList<String> getParams() {
            StackTraceElement[] t = Thread.currentThread().getStackTrace();
            StackTraceElement up = t[3];

            ArrayList<String> ret = new ArrayList<>();
            String qqq = up.toString();
            ret.add("." + up.getMethodName() + qqq.substring(qqq.indexOf("("), qqq.length()));
            try {
                BufferedReader br = new BufferedReader(new FileReader(
                        new File("src/" + up.getClassName().replaceAll("\\.", "/") + ".java")));
                int g = up.getLineNumber();
                while (--g > 0) br.readLine();
                String q = br.readLine();
                String[] ss = q.split("Debug\\.print\\(");
                String[] qq = ss[1].substring(0, ss[1].lastIndexOf(")")).split(",");
                for (int i = 0; i < qq.length; i++) {
                    ret.add(qq[i].trim());
                }
            } catch (Exception e) {
            }
            for (int i = 0; i < 100; i++) ret.add("???");
            return ret;
        }

        private static String toString(Object o) {
            if (o == null) {
                return "null";
            } else if (o instanceof Object[]) {
                return Arrays.toString((Object[]) o);
            } else if (o instanceof char[]) {
                return new String((char[]) o);
            } else if (o instanceof int[]) {
                return Arrays.toString((int[]) o);
            } else if (o instanceof long[]) {
                return Arrays.toString((long[]) o);
            } else if (o instanceof double[]) {
                return Arrays.toString((double[]) o);
            } else if (o instanceof boolean[]) {
                return Arrays.toString((boolean[]) o);
            } else {
                return o.toString();
            }
        }

        public static void print(Object... x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.print(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.print(s.get(i + 1) + " = " + toString(x[i]));
                if (i != x.length - 1) System.out.print(", ");
            }
            System.out.println();
        }

        public static void print(int[] x) {
            if (!DEBUG) return;
            ArrayList<String> s = getParams();
            System.out.println(s.get(0) + ": ");
            for (int i = 0; i < x.length; i++) {
                System.out.println(s.get(1) + "[" + i + "] = " + x[i]);
            }
            System.out.println("***");
        }

    }

    static class AUtils {
        public static void reverse(int[] arr) {
            for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
                int t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
            }
        }

    }
}

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

public class C {

	int[][] go(int[] arr) {
		int n = arr.length;
		int[] aux = new int[n + 1];
		Arrays.fill(aux, Integer.MAX_VALUE);
		aux[0] = Integer.MIN_VALUE;

		int[] justAns = new int[n];
		int[] takeAns = new int[n];
		int ans = 0;

		for (int i = 0; i < n; i++) {
			int x = arr[i];
			int low = 0; // <=
			int high = ans + 1; // >
			while (high - low > 1) {
				int mid = (low + high) >> 1;
				if (aux[mid] > x) {
					high = mid;
				} else {
					low = mid;
				}
			}
			aux[high] = x;
			if (high > ans) {
				ans = high;
			}
			justAns[i] = ans;
			takeAns[i] = high;
		}
		return new int[][] { justAns, takeAns };
	}

	void submit() {
		int n = nextInt();
		int m = nextInt();
		int[] arr = new int[m];
		for (int i = 0; i < n; i++) {
			int x = nextInt() - 1;
			int y = nextInt() - 1;
			arr[x]++;
			if (y < m - 1) {
				arr[y + 1]--;
			}
		}

		for (int i = 1; i < m; i++) {
			arr[i] += arr[i - 1];
		}

//		System.err.println(Arrays.toString(arr));
		int[][] inc = go(arr);
		int[][] dec = go(reverse(arr));
		dec[0] = reverse(dec[0]);
		dec[1] = reverse(dec[1]);

		// System.err.println(Arrays.toString(inc));
		// System.err.println(Arrays.toString(dec));

		int ans = 0;
		for (int mid = 0; mid < n; mid++) {
			int skipMid = 0;
			skipMid += mid > 0 ? inc[0][mid - 1] : 0;
			skipMid += mid < n - 1 ? dec[0][mid + 1] : 0;
			ans = Math.max(ans, skipMid);

			int takeMid = inc[1][mid] + dec[1][mid] - 1;
			ans = Math.max(ans, takeMid);
		}

		out.println(ans);
	}

	int[] reverse(int[] arr) {
		int[] ret = new int[arr.length];
		for (int i = 0, j = arr.length - 1; i < arr.length; i++, j--) {
			ret[j] = arr[i];
		}
		return ret;
	}

	void preCalc() {

	}

	void test() {

	}

	void stress() {
		for (int tst = 0;; tst++) {
			if (false) {
				throw new AssertionError();
			}
			System.err.println(tst);
		}
	}

	C() throws IOException {
		is = System.in;
		out = new PrintWriter(System.out);
		preCalc();
		submit();
		// stress();
		// test();
		out.close();
	}

	static final Random rng = new Random();
	static final int C = 5;

	static int rand(int l, int r) {
		return l + rng.nextInt(r - l + 1);
	}

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

	private InputStream is;
	PrintWriter out;

	private byte[] buf = new byte[1 << 14];
	private int bufSz = 0, bufPtr = 0;

	private int readByte() {
		if (bufSz == -1)
			throw new RuntimeException("Reading past EOF");
		if (bufPtr >= bufSz) {
			bufPtr = 0;
			try {
				bufSz = is.read(buf);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
			if (bufSz <= 0)
				return -1;
		}
		return buf[bufPtr++];
	}

	private boolean isTrash(int c) {
		return c < 33 || c > 126;
	}

	private int skip() {
		int b;
		while ((b = readByte()) != -1 && isTrash(b))
			;
		return b;
	}

	String nextToken() {
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while (!isTrash(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	String nextString() {
		int b = readByte();
		StringBuilder sb = new StringBuilder();
		while (!isTrash(b) || b == ' ') {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

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

	char nextChar() {
		return (char) skip();
	}

	int nextInt() {
		int ret = 0;
		int b = skip();
		if (b != '-' && (b < '0' || b > '9')) {
			throw new InputMismatchException();
		}
		boolean neg = false;
		if (b == '-') {
			neg = true;
			b = readByte();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				ret = ret * 10 + (b - '0');
			} else {
				if (b != -1 && !isTrash(b)) {
					throw new InputMismatchException();
				}
				return neg ? -ret : ret;
			}
			b = readByte();
		}
	}

	long nextLong() {
		long ret = 0;
		int b = skip();
		if (b != '-' && (b < '0' || b > '9')) {
			throw new InputMismatchException();
		}
		boolean neg = false;
		if (b == '-') {
			neg = true;
			b = readByte();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				ret = ret * 10 + (b - '0');
			} else {
				if (b != -1 && !isTrash(b)) {
					throw new InputMismatchException();
				}
				return neg ? -ret : ret;
			}
			b = readByte();
		}
	}
}

import java.text.DecimalFormat;
import java.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) throws Exception{
        IO io = new IO(null,null);
        int n = io.getNextInt(),m = io.getNextInt();
        int [] cnt = new int[m + 2];
        for (int i = 0;i < n;i++) {
            int l = io.getNextInt(),r = io.getNextInt();
            cnt[l]++;
            cnt[r + 1]--;
        }
        int mn = Integer.MAX_VALUE;
        for (int i = 1,s = 0;i <= m;i++) {
            s += cnt[i];
            cnt[i] = s;
            if (s < mn) mn = s;
        }

        boolean [] prv = new boolean[m+1];
        boolean [] nxt = new boolean[m+1];
        boolean f = false;
        for (int i = 1;i <= m;i++) {
            prv[i] = f;
            if (cnt[i] > mn) f = true;
        }
        f = false;
        for (int i = m;i > 0;i--) {
            nxt[i] = f;
            if (cnt[i] > mn) f = true;
        }

        int ans = m;
        for (int i = 1;i <= m;i++)
            ans -= ((cnt[i] == mn) && prv[i] && nxt[i]) ? 1 : 0;

        io.println(ans);

        io.close();
    }

}

class IO{
    private BufferedReader br;
    private StringTokenizer st;
    private PrintWriter writer;
    private String inputFile,outputFile;

    public boolean hasMore() throws IOException{
        if(st != null && st.hasMoreTokens()) return true;
        if(br != null && br.ready()) return true;
        return false;
    }

    public String getNext() throws FileNotFoundException, IOException{
        while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }

    public String getNextLine() throws FileNotFoundException, IOException{
        return br.readLine().trim();
    }

    public int getNextInt() throws FileNotFoundException, IOException{
        return Integer.parseInt(getNext());
    }
    public long getNextLong() throws FileNotFoundException, IOException{
        return Long.parseLong(getNext());
    }

    public void print(double x,int num_digits) throws  IOException{
        writer.printf("%." + num_digits + "f" ,x);
    }
    public void println(double x,int num_digits) throws  IOException{
        writer.printf("%." + num_digits + "f\n" ,x);
    }
    public void print(Object o) throws  IOException{
        writer.print(o.toString());
    }

    public void println(Object o) throws  IOException{
        writer.println(o.toString());
    }
    public IO(String x,String y) throws FileNotFoundException, IOException{
        inputFile = x;
        outputFile = y;
        if(x != null) br = new BufferedReader(new FileReader(inputFile));
        else br = new BufferedReader(new InputStreamReader(System.in));
        if(y != null) writer = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
        else writer = new PrintWriter(new OutputStreamWriter(System.out));
    }

    protected void close() throws IOException{
        br.close();
        writer.close();
    }
    public void outputArr(Object [] A) throws IOException{
        int L = A.length;
        for (int i = 0;i < L;i++) {
            if(i > 0) writer.print(" ");
            writer.print(A[i]);
        }
        writer.print("\n");
    }
}

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

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

	FastReader in = new FastReader();
	PrintWriter out = new PrintWriter(System.out);
	void run(){
		work();
		out.flush();
	}
	long mod=1000000007;
	long gcd(long a,long b) {
		return a==0?b:b>=a?gcd(b%a,a):gcd(b,a);
	}
	void work() {
		int m=in.nextInt(),n=in.nextInt();
		int[] A=new int[n];
		for(int i=0;i<m;i++) {
			int s=in.nextInt()-1,e=in.nextInt()-1;
			A[s]++;
			if(e+1<n)A[e+1]--;
		}
		for(int i=1;i<n;i++) {
			A[i]+=A[i-1];
		}
		int[] r1=find1(A);
		int[] r2=find2(A);
		int max=0;
		for(int i=0;i<n-1;i++) {
			max=Math.max(max, r1[i]+r2[i+1]);
		}
		out.println(max);
	}
	private int[] find1(int[] A) {
		int n=A.length;
		int[] rec=new int[n];
		int[] ret=new int[n];
		int p=0;
		for(int i=0;i<n;i++) {
			int l=0,r=p;
			while(l<r) {
				int m=(l+r)/2;
				if(A[i]<=rec[m]) {//1:递增
					l=m+1;
				}else {
					r=m;
				}
			}
			rec[l]=A[i];
			if(l==p) {
				p++;
			}
			ret[i]=p;
		}
		return ret;
	}
	private int[] find2(int[] A) {
		int n=A.length;
		int[] rec=new int[n];
		int[] ret=new int[n];
		int p=0;
		for(int i=n-1;i>=0;i--) {
			int l=0,r=p;
			while(l<r) {
				int m=(l+r)/2;
				if(A[i]<=rec[m]) {//1:递增
					l=m+1;
				}else {
					r=m;
				}
			}
			rec[l]=A[i];
			if(l==p) {
				p++;
			}
			ret[i]=p;
		}
		return ret;
	}
}

class FastReader
{
	BufferedReader br;
	StringTokenizer st;

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

	public String next()
	{
		while(st==null || !st.hasMoreElements())//回车，空行情况
		{
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}

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

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

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

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

	FastReader in = new FastReader();
	PrintWriter out = new PrintWriter(System.out);
	void run(){
		work();
		out.flush();
	}
	long mod=1000000007;
	long gcd(long a,long b) {
		return a==0?b:b>=a?gcd(b%a,a):gcd(b,a);
	}
	void work() {
		int m=in.nextInt(),n=in.nextInt();
		int[] A=new int[n];
		for(int i=0;i<m;i++) {
			int s=in.nextInt()-1,e=in.nextInt()-1;
			A[s]++;
			if(e+1<n)A[e+1]--;
		}
		for(int i=1;i<n;i++) {
			A[i]+=A[i-1];
		}
		out.println(Math.max(find(A,-1), find(A,1)));
	}
	private int find(int[] A, int f) {
		int n=A.length;
		int[] rec=new int[n];
		int p=0;
		for(int i=0;i<n;i++) {
			int l=0,r=p;
			while(l<r) {
				int m=(l+r)/2;
				if(compare(rec[m],A[i],f)) {//1:递增
					l=m+1;
				}else {
					r=m;
				}
			}
			rec[l]=A[i];
			if(l==p) {
				p++;
			}
		}
		return p;
	}
	private boolean compare(int a, int b, int f) {
		if(f==1) {
			return a<=b;
		}else {
			return a>=b;
		}
	}
}

class FastReader
{
	BufferedReader br;
	StringTokenizer st;

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

	public String next()
	{
		while(st==null || !st.hasMoreElements())//回车，空行情况
		{
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}

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

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

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

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

	FastReader in = new FastReader();
	PrintWriter out = new PrintWriter(System.out);
	void run(){
		work();
		out.flush();
	}
	long mod=1000000007;
	long gcd(long a,long b) {
		return a==0?b:b>=a?gcd(b%a,a):gcd(b,a);
	}
	void work() {
		int m=in.nextInt(),n=in.nextInt();
		int[] A=new int[n];
		for(int i=0;i<m;i++) {
			int s=in.nextInt()-1,e=in.nextInt();
			A[s]++;
			if(e+1<n)A[e+1]--;
		}
		for(int i=1;i<n;i++) {
			A[i]+=A[i-1];
		}
		out.println(Math.max(find(A,-1), find(A,1)));
	}
	private int find(int[] A, int f) {
		int n=A.length;
		int[] rec=new int[n];
		int p=0;
		for(int i=0;i<n;i++) {
			int l=0,r=p;
			while(l<r) {
				int m=(l+r)/2;
				if(compare(rec[m],A[i],f)) {//1:递增
					l=m+1;
				}else {
					r=m;
				}
			}
			rec[l]=A[i];
			if(l==p) {
				p++;
			}
		}
		return p;
	}
	private boolean compare(int a, int b, int f) {
		if(f==1) {
			return a<=b;
		}else {
			return a>=b;
		}
	}
}

class FastReader
{
	BufferedReader br;
	StringTokenizer st;

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

	public String next()
	{
		while(st==null || !st.hasMoreElements())//回车，空行情况
		{
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}

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

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

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

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

	FastReader in = new FastReader();
	PrintWriter out = new PrintWriter(System.out);
	void run(){
		work();
		out.flush();
	}
	long mod=1000000007;
	long gcd(long a,long b) {
		return a==0?b:b>=a?gcd(b%a,a):gcd(b,a);
	}
	void work() {
		int m=in.nextInt(),n=in.nextInt();
		int[] A=new int[n+1];
		for(int i=0;i<m;i++) {
			int s=in.nextInt()-1,e=in.nextInt();
			A[s]++;
			if(e+1<n)A[e+1]--;
		}
		for(int i=1;i<n;i++) {
			A[i]+=A[i-1];
		}
		out.println(Math.max(find(A,-1), find(A,1)));
	}
	private int find(int[] A, int f) {
		int n=A.length;
		int[] rec=new int[n];
		int p=0;
		for(int i=0;i<n;i++) {
			int l=0,r=p;
			while(l<r) {
				int m=(l+r)/2;
				if(compare(rec[m],A[i],f)) {//1:递增
					l=m+1;
				}else {
					r=m;
				}
			}
			rec[l]=A[i];
			if(l==p) {
				p++;
			}
		}
		return p;
	}
	private boolean compare(int a, int b, int f) {
		if(f==1) {
			return a<=b;
		}else {
			return a>=b;
		}
	}
}

class FastReader
{
	BufferedReader br;
	StringTokenizer st;

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

	public String next()
	{
		while(st==null || !st.hasMoreElements())//回车，空行情况
		{
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}

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

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

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

public class C {

	static int m;
	static int[] inc1, inc2;

	static void lis(int[] A, int n, int[] ans)		// Can be implemented with TreeSet (lower, remove, add)
	{
		ArrayList<Integer> L = new ArrayList<Integer>();

		for(int i = 0; i < n; ++i)
		{
			int pos = Collections.binarySearch(L, A[i] + 1);
			if (pos < 0) pos = -(pos + 1);

			if(pos >= L.size()) L.add(A[i]);
			else                 L.set(pos, A[i]);

			ans[i]  = pos + 1;

		}

	}

	static class SegmentTree {		// 1-based DS, OOP

		int N; 			//the number of elements in the array as a power of 2 (i.e. after padding)
		int[] array, sTree, lazy;

		SegmentTree(int[] in)
		{
			array = in; N = in.length - 1;
			sTree = new int[N<<1];		//no. of nodes = 2*N - 1, we add one to cross out index zero
			lazy = new int[N<<1];
			build(1,1,N);
		}

		void build(int node, int b, int e)	// O(n)
		{
			if(b == e)
				sTree[node] = array[b];
			else
			{
				int mid = b + e >> 1;
			build(node<<1,b,mid);
			build(node<<1|1,mid+1,e);
			sTree[node] = sTree[node<<1]+sTree[node<<1|1];
			}
		}

		void update_range(int i, int j, int val)		// O(log n)
		{
			update_range(1,1,N,i,j,val);
		}

		void update_range(int node, int b, int e, int i, int j, int val)
		{
			if(i > e || j < b)
				return;
			if(b >= i && e <= j)
			{
				sTree[node] += (e-b+1)*val;
				lazy[node] += val;
			}
			else
			{
				int mid = b + e >> 1;
			propagate(node, b, mid, e);
			update_range(node<<1,b,mid,i,j,val);
			update_range(node<<1|1,mid+1,e,i,j,val);
			sTree[node] = sTree[node<<1] + sTree[node<<1|1];
			}

		}
		void propagate(int node, int b, int mid, int e)
		{
			lazy[node<<1] += lazy[node];
			lazy[node<<1|1] += lazy[node];
			sTree[node<<1] += (mid-b+1)*lazy[node];
			sTree[node<<1|1] += (e-mid)*lazy[node];
			lazy[node] = 0;
		}

		int query(int i, int j)
		{
			return query(1,1,N,i,j);
		}

		int query(int node, int b, int e, int i, int j)	// O(log n)
		{
			if(i>e || j <b)
				return 0;
			if(b>= i && e <= j)
				return sTree[node];
			int mid = b + e >> 1;
			propagate(node, b, mid, e);
			int q1 = query(node<<1,b,mid,i,j);
			int q2 = query(node<<1|1,mid+1,e,i,j);
			return q1 + q2;

		}
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = sc.nextInt();
		int m = sc.nextInt();

		int M = 1; while(M < m) M <<= 1; //padding

		int[] in = new int[M + 1];
		SegmentTree st = new SegmentTree(in);

		for (int i = 0; i < n; i++) {
			st.update_range(sc.nextInt(), sc.nextInt(), 1);
		}

		int[] arr1 = new int[m];
		int[] arr2 = new int[m];
		for (int i = 0; i < m; i++) {
			arr1[i] = st.query(i + 1, i + 1);
			arr2[m - i - 1] = arr1[i];
		}

		inc1 = new int[m];
		inc2 = new int[m];

		lis(arr1, m, inc1);
		lis(arr2, m, inc2);

		int ans = 0;

		for (int i = 0; i < m; i++) {
			ans = Math.max(inc1[i] + inc2[m - i - 1] - 1, ans);
		}

		out.println(ans);
		out.close();

	}

	static class Scanner
	{
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s)
		{
			br = new BufferedReader(new InputStreamReader(s));
		}

		public String next() throws IOException
		{
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

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

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

		public String nextLine() throws IOException
		{
			return br.readLine();
		}

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

		public boolean ready() throws IOException
		{
			return br.ready();
		}
	}
}

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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Jay Leeds
 */
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);
        TaskF solver = new TaskF();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskF {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] prefix = new int[m];
            for (int i = 0; i < n; i++) {
                int a = in.nextInt() - 1;
                int b = in.nextInt();
                prefix[a]++;
                if (b < m) prefix[b]--;
            }

            int[] data = new int[m];
            int count = 0;
            for (int i = 0; i < m; i++) {
                count += prefix[i];
                data[i] = count;
            }

            int[] increasingIndices = new int[m + 2];
            for (int i = 0; i < m + 2; i++) {
                increasingIndices[i] = n + 1;
            }

            increasingIndices[0] = -1;
            int[] increaseData = new int[m];
            for (int i = 0; i < m; i++) {
                int lo = 0;
                int hi = i;
                if (data[i] == 0) {
                    increaseData[i] = 0;
                    continue;
                }
                while (lo <= hi) {
                    int mid = (lo + hi) / 2;
                    if (increasingIndices[mid] <= data[i]) {
                        lo = mid + 1;
                    } else {
                        hi = mid - 1;
                    }
                }
                //lo--;
                increaseData[i] = lo;
                increasingIndices[lo] = Math.min(increasingIndices[lo], data[i]);
            }

            int[] decreasingIndices = new int[m + 2];
            for (int i = 0; i < m + 2; i++) decreasingIndices[i] = n + 1;
            decreasingIndices[0] = -1;
            int[] decreaseData = new int[m];
            for (int i = m - 1; i >= 0; i--) {
                if (data[i] == 0) {
                    decreaseData[i] = 0;
                    continue;
                }
                int lo = 0;
                int hi = m - i;
                while (lo <= hi) {
                    int mid = (lo + hi) / 2;
                    if (decreasingIndices[mid] <= data[i]) {
                        lo = mid + 1;
                    } else hi = mid - 1;
                }
                decreaseData[i] = lo;
                decreasingIndices[lo] = Math.min(decreasingIndices[lo], data[i]);
            }

            int max = 0;
            for (int i = 0; i < m; i++) {
                max = Math.max(max, decreaseData[i] + increaseData[i] - 1);
            }
            out.printLine(max);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream; //Fast IO by Egor Kulikov, modified by myself
        }

        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 int nextInt() {
            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);

        }

    }

    static 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 printLine(int i) {
            writer.println(i);
        }

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

    }
}

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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Jay Leeds
 */
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);
        TaskF solver = new TaskF();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskF {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] prefix = new int[m];
            for (int i = 0; i < n; i++) {
                int a = in.nextInt() - 1;
                int b = in.nextInt();
                prefix[a]++;
                if (b < m) prefix[b]--;
            }

            int[] data = new int[m];
            int count = 0;
            for (int i = 0; i < m; i++) {
                count += prefix[i];
                data[i] = count;
            }

            int[] increasingIndices = new int[m + 2];
            for (int i = 0; i < m + 2; i++) {
                increasingIndices[i] = m + 1;
            }

            increasingIndices[0] = -1;
            int[] increaseData = new int[m];
            for (int i = 0; i < m; i++) {
                int lo = 0;
                int hi = i;
                if (data[i] == 0) {
                    increaseData[i] = 0;
                    continue;
                }
                while (lo <= hi) {
                    int mid = (lo + hi) / 2;
                    if (increasingIndices[mid] <= data[i]) {
                        lo = mid + 1;
                    } else {
                        hi = mid - 1;
                    }
                }
                //lo--;
                increaseData[i] = lo;
                increasingIndices[lo] = Math.min(increasingIndices[lo], data[i]);
            }

            int[] decreasingIndices = new int[m + 2];
            for (int i = 0; i < m + 2; i++) decreasingIndices[i] = -1;
            decreasingIndices[0] = m - 1;
            int[] decreaseData = new int[m];
            for (int i = m - 1; i >= 0; i--) {
                if (data[i] == 0) {
                    decreaseData[i] = 0;
                    continue;
                }
                int lo = 0;
                int hi = i;
                while (lo <= hi) {
                    int mid = (lo + hi) / 2;
                    if (decreasingIndices[mid] >= data[i]) {
                        lo = mid + 1;
                    } else hi = mid - 1;
                }
                decreaseData[i] = lo;
                decreasingIndices[lo] = Math.max(decreasingIndices[lo], data[i]);
            }

            int max = 0;
            for (int i = 0; i < m; i++) {
                max = Math.max(max, decreaseData[i] + increaseData[i] - 1);
            }
            out.printLine(max);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream; //Fast IO by Egor Kulikov, modified by myself
        }

        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 int nextInt() {
            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);

        }

    }

    static 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 printLine(int i) {
            writer.println(i);
        }

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

    }
}

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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Jay Leeds
 */
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);
        TaskF solver = new TaskF();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskF {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] prefix = new int[m];
            for (int i = 0; i < n; i++) {
                int a = in.nextInt() - 1;
                int b = in.nextInt();
                prefix[a]++;
                if (b < m) prefix[b]--;
            }

            int[] data = new int[m];
            int count = 0;
            for (int i = 0; i < m; i++) {
                count += prefix[i];
                data[i] = count;
            }

            int[] increasingIndices = new int[m + 2];
            for (int i = 0; i < m + 2; i++) {
                increasingIndices[i] = n + 1;
            }

            increasingIndices[0] = -1;
            int[] increaseData = new int[m];
            for (int i = 0; i < m; i++) {
                int lo = 0;
                int hi = m;
                if (data[i] == 0) {
                    increaseData[i] = 0;
                    continue;
                }
                while (lo <= hi) {
                    int mid = (lo + hi + 1) / 2;
                    if (increasingIndices[mid] <= data[i]) {
                        lo = mid + 1;
                    } else {
                        hi = mid - 1;
                    }
                }
                increaseData[i] = lo;
                increasingIndices[lo] = Math.min(increasingIndices[lo], data[i]);
            }

            int[] decreasingIndices = new int[m + 2];
            for (int i = 0; i < m + 2; i++) decreasingIndices[i] = n + 1;
            decreasingIndices[0] = -1;
            int[] decreaseData = new int[m];
            for (int i = m - 1; i >= 0; i--) {
                if (data[i] == 0) {
                    decreaseData[i] = 0;
                    continue;
                }
                int lo = 0;
                int hi = m;
                while (lo <= hi) {
                    int mid = (lo + hi + 1) / 2;
                    if (decreasingIndices[mid] <= data[i]) {
                        lo = mid + 1;
                    } else hi = mid - 1;
                }

                decreaseData[i] = lo;
                decreasingIndices[lo] = Math.min(decreasingIndices[lo], data[i]);
            }

            int max = 0;
            for (int i = 0; i < m; i++) {
                max = Math.max(max, decreaseData[i] + increaseData[i] - 1);
            }
            out.printLine(max);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream; //Fast IO by Egor Kulikov, modified by myself
        }

        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 int nextInt() {
            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);

        }

    }

    static 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 printLine(int i) {
            writer.println(i);
        }

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

    }
}

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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author palayutm
 */
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);
        ProblemFTeodorIsNotALiar solver = new ProblemFTeodorIsNotALiar();
        solver.solve(1, in, out);
        out.close();
    }

    static class ProblemFTeodorIsNotALiar {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            int[] a = new int[m + 2];
            for (int i = 0; i < n; i++) {
                int L = in.nextInt(), R = in.nextInt();
                a[L]++;
                a[R + 1]--;
            }
            for (int i = 1; i <= m; i++) {
                a[i] += a[i - 1];
            }
            int[] f = new int[m + 1];
            ArrayList<Integer> arr = new ArrayList<>();
            for (int i = 1; i <= m; i++) {
                add(arr, a[i]);
                f[i] = arr.size();
            }
            arr.clear();
            int ans = 0;
            for (int i = m; i >= 1; i--) {
                add(arr, a[i]);
                ans = Math.max(ans, f[i - 1] + arr.size());
            }
            out.println(ans);
        }

        private void add(ArrayList<Integer> arr, int val) {
            if (arr.isEmpty() || val >= arr.get(arr.size() - 1)) {
                arr.add(val);
            } else {
                int p = Collections.binarySearch(arr, val + 1);
                if (p >= 0) arr.set(p, val);
                else arr.set(-p - 1, val);
            }
        }

    }

    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream out) {
            super(out);
        }

        public OutputWriter(Writer out) {
            super(out);
        }

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

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

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

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

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

    }
}

import java.util.*;
import java.io.*;
import java.math.*;
public class code7 {
	InputStream is;
	PrintWriter out;
	static long mod=pow(10,9)+7;
	int dx[]= {0,0,1,-1},dy[]={+1,-1,0,0};
	void solve()
	{
		int n=ni();
		int m=ni();
		int seg[]=new int[m+2];
		for(int i=0;i<n;i++)
		{
			int l=ni();
			int r=ni();
			seg[l]++;
			seg[r+1]--;
		}
		int count=0;
		for(int i=1;i<=m;i++)
		{
			count+=seg[i];
			seg[i]=count;
		}
		seg[0]=Integer.MAX_VALUE;
		seg[m+1]=Integer.MAX_VALUE;
		int segrev[]=new int[m+2];
		for(int i=0;i<=m+1;i++)
			segrev[i]=seg[m+1-i];
		inc=new int[m+2];
		incr=new int[m+2];
		lis(seg);
		lisr(segrev);
		int max=0;
		for(int i=1;i<=m;i++)
		{
			max=Math.max(max,inc[i]+incr[i]-1);
		}
		System.out.println(max);
	}
	int inc[],incr[];
	int lis(int a[])
	{
		int n=a.length;
		int dp[]=new int[n];
		dp[0]=a[0];
		int len=1;
		inc[0]=len;
		for(int i=1;i<n;i++)
		{
			if(a[i]<dp[0])
				dp[0]=a[i];
			else if(a[i]>=dp[len-1])
				dp[len++]=a[i];
			else
				dp[ceil(dp,0,len-1,a[i])]=a[i];
			inc[i]=len;

		}
		return  len;
	}
	int ceil(int a[],int l,int r,int key)
	{
		int ans=-1;
		while(l<=r)
		{
			int m=l+(r-l)/2;
			if(key<a[m])
			{
				ans=m;
				r=m-1;
			}
			else
				l=m+1;
		}
		return ans;
	}
	int lisr(int a[])
	{
		int n=a.length;
		int dp[]=new int[n];
		dp[0]=a[n-1];
		int len=1;
		incr[n-1]=len;
		for(int i=n-2;i>=0;i--)
		{
			if(a[i]<dp[0])
				dp[0]=a[i];
			else if(a[i]>=dp[len-1])
				dp[len++]=a[i];
			else
				dp[ceil(dp,0,len-1,a[i])]=a[i];
			incr[i]=len;
		}
		return  len;
	}
	int small[];
	void pre(int n)
	{
		small=new int[n+1];
		for(int i=2;i*i<=n;i++)
		{
			for(int j=i;j*i<=n;j++)
			{
				if(small[i*j]==0)
					small[i*j]=i;
			}
		}
		for(int i=0;i<=n;i++)
		{
			if(small[i]==0)
				small[i]=i;
		}
	}
	public static int count(int x)
	{
		int num=0;
		while(x!=0)
		{
			x=x&(x-1);
			num++;
		}
		return num;
	}
	static long d, x, y;
	void extendedEuclid(long A, long B) {
	    if(B == 0) {
	        d = A;
	        x = 1;
	        y = 0;
	    }
	    else {
	        extendedEuclid(B, A%B);
	        long temp = x;
	        x = y;
	        y = temp - (A/B)*y;
	    }
	}
	long modInverse(long A,long M) //A and M are coprime
	{
	    extendedEuclid(A,M);
	    return (x%M+M)%M;    //x may be negative
	}
	public static void mergeSort(int[] arr, int l ,int r){
		if((r-l)>=1){
			int mid = (l+r)/2;
			mergeSort(arr,l,mid);
			mergeSort(arr,mid+1,r);
			merge(arr,l,r,mid);
		}
	}
	public static void merge(int arr[], int l, int r, int mid){
		int n1 = (mid-l+1), n2 = (r-mid);
		int left[] = new int[n1];
		int right[] = new int[n2];
		for(int i =0 ;i<n1;i++) left[i] = arr[l+i];
		for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i];
		int i =0, j =0, k = l;
		while(i<n1 && j<n2){
			if(left[i]>right[j]){
				arr[k++] = right[j++];
			}
			else{
				arr[k++] = left[i++];
			}
		}
		while(i<n1) arr[k++] = left[i++];
		while(j<n2) arr[k++] = right[j++];
	}
	public static void mergeSort(long[] arr, int l ,int r){
		if((r-l)>=1){
			int mid = (l+r)/2;
			mergeSort(arr,l,mid);
			mergeSort(arr,mid+1,r);
			merge(arr,l,r,mid);
		}
	}
	public static void merge(long arr[], int l, int r, int mid){
		int n1 = (mid-l+1), n2 = (r-mid);
		long left[] = new long[n1];
		long right[] = new long[n2];
		for(int i =0 ;i<n1;i++) left[i] = arr[l+i];
		for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i];
		int i =0, j =0, k = l;
		while(i<n1 && j<n2){
			if(left[i]>right[j]){
				arr[k++] = right[j++];
			}
			else{
				arr[k++] = left[i++];
			}
		}
		while(i<n1) arr[k++] = left[i++];
		while(j<n2) arr[k++] = right[j++];
	}
	 static class Pair implements Comparable<Pair>{

	      int x,y,k,i;
		Pair (int x,int y,int k){
			this.x=x;
			this.y=y;
			this.k=k;
		}

		public int compareTo(Pair o) {
			return this.y-o.y;
		}
		public boolean equals(Object o) {
			if (o instanceof Pair) {
				Pair p = (Pair)o;
				return p.x == x && p.y == y;
			}
			return false;
		}
		public int hashCode() {
			return new Long(x).hashCode()*31 + new Long(y).hashCode() ;
		}

	        @Override
	        public String toString() {
	            return  "("+x + " " + y +" "+k+" "+i+" )";
	        }

	    }

	    public static boolean isPal(String s){
	        for(int i=0, j=s.length()-1;i<=j;i++,j--){
	                if(s.charAt(i)!=s.charAt(j)) return false;
	        }
	        return true;
	    }
	    public static String rev(String s){
			StringBuilder sb=new StringBuilder(s);
			sb.reverse();
			return sb.toString();
	    }

	    public static long gcd(long x,long y){
		if(x%y==0)
			return y;
		else
			return gcd(y,x%y);
	    }

	    public static int gcd(int x,int y){
	    	if(y==0)
	    		return x;
	    	return gcd(y,x%y);
	    }

	    public static long gcdExtended(long a,long b,long[] x){

	        if(a==0){
	            x[0]=0;
	            x[1]=1;
	            return b;
	        }
	        long[] y=new long[2];
	        long gcd=gcdExtended(b%a, a, y);

	        x[0]=y[1]-(b/a)*y[0];
	        x[1]=y[0];

	        return gcd;
	    }

	    public static int abs(int a,int b){
		return (int)Math.abs(a-b);
	    }

	    public static long abs(long a,long b){
		return (long)Math.abs(a-b);
	    }

	    public static int max(int a,int b){
		if(a>b)
			return a;
		else
			return b;
	    }

	    public static int min(int a,int b){
		if(a>b)
			return b;
		else
			return a;
	    }

	    public static long max(long a,long b){
		if(a>b)
			return a;
		else
			return b;
	    }

	    public static long min(long a,long b){
		if(a>b)
			return b;
		else
			return a;
	    }

	    public static long pow(long n,long p,long m){
		 long  result = 1;
		  if(p==0)
		    return 1;
		if (p==1)
		    return n;
		while(p!=0)
		{
		    if(p%2==1)
		        result *= n;
		    if(result>=m)
		    result%=m;
		    p >>=1;
		    n*=n;
		    if(n>=m)
		    n%=m;
		}
		return result;
	    }

	    public static long pow(long n,long p){
		long  result = 1;
		  if(p==0)
		    return 1;
		if (p==1)
		    return n;
		while(p!=0)
		{
		    if(p%2==1)
		        result *= n;
		    p >>=1;
		    n*=n;
		}
		return result;
	    }
	    public static void debug(Object... o) {
			System.out.println(Arrays.deepToString(o));
		}
	    void run() throws Exception {
			is = System.in;
			out = new PrintWriter(System.out);
			solve();
			out.flush();
		}

	    public static void main(String[] args) throws Exception {
			new Thread(null, new Runnable() {
				public void run() {
					try {
						new code7().run();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}, "1", 1 << 26).start();

		}
	    private byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		private int readByte() {
			if (lenbuf == -1)
				throw new InputMismatchException();
			if (ptrbuf >= lenbuf) {
				ptrbuf = 0;
				try {
					lenbuf = is.read(inbuf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return -1;
			}
			return inbuf[ptrbuf++];
		}

		private boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		private int skip() {
			int b;
			while ((b = readByte()) != -1 && isSpaceChar(b));
			return b;
		}

		private double nd() {
			return Double.parseDouble(ns());
		}

		private char nc() {
			return (char) skip();
		}

		private String ns() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		private char[] ns(int n) {
			char[] buf = new char[n];
			int b = skip(), p = 0;
			while (p < n && !(isSpaceChar(b))) {
				buf[p++] = (char) b;
				b = readByte();
			}
			return n == p ? buf : Arrays.copyOf(buf, p);
		}

		private char[][] nm(int n, int m) {
			char[][] map = new char[n][];
			for (int i = 0; i < n; i++)
				map[i] = ns(m);
			return map;
		}

		private int[] na(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = ni();
			return a;
		}

		private int ni() {
			int num = 0, b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		private long nl() {
			long num = 0;
			int b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

}

import java.util.*;
import java.io.*;
import java.math.*;
public class code7 {
	InputStream is;
	PrintWriter out;
	static long mod=pow(10,9)+7;
	int dx[]= {0,0,1,-1},dy[]={+1,-1,0,0};
	void solve()
	{
		int n=ni();
		int m=ni();
		int seg[]=new int[m+2];
		for(int i=0;i<n;i++)
		{
			int l=ni();
			int r=ni();
			seg[l]++;
			seg[r+1]--;
		}
		int count=0;
		for(int i=1;i<=m;i++)
		{
			count+=seg[i];
			seg[i]=count;
		}
		seg[0]=Integer.MAX_VALUE;
		seg[m+1]=Integer.MAX_VALUE;
		int segrev[]=new int[m+2];
		for(int i=0;i<=m+1;i++)
			segrev[i]=seg[m+1-i];
		out.print(Math.max(lis(seg),lis(segrev))-1);
	}
	int lis(int a[])
	{
		int n=a.length;
		int dp[]=new int[n];
		dp[0]=a[0];
		int len=1;
		for(int i=1;i<n;i++)
		{
			if(a[i]<dp[0])
				dp[0]=a[i];
			else if(a[i]>=dp[len-1])
				dp[len++]=a[i];
			else
				dp[ceil(dp,0,len-1,a[i])]=a[i];

		}
		return  len;
	}
	int ceil(int a[],int l,int r,int key)
	{
		int ans=-1;
		while(l<=r)
		{
			int m=l+(r-l)/2;
			if(key<a[m])
			{
				ans=m;
				r=m-1;
			}
			else
				l=m+1;
		}
		return ans;
	}

	int small[];
	void pre(int n)
	{
		small=new int[n+1];
		for(int i=2;i*i<=n;i++)
		{
			for(int j=i;j*i<=n;j++)
			{
				if(small[i*j]==0)
					small[i*j]=i;
			}
		}
		for(int i=0;i<=n;i++)
		{
			if(small[i]==0)
				small[i]=i;
		}
	}
	public static int count(int x)
	{
		int num=0;
		while(x!=0)
		{
			x=x&(x-1);
			num++;
		}
		return num;
	}
	static long d, x, y;
	void extendedEuclid(long A, long B) {
	    if(B == 0) {
	        d = A;
	        x = 1;
	        y = 0;
	    }
	    else {
	        extendedEuclid(B, A%B);
	        long temp = x;
	        x = y;
	        y = temp - (A/B)*y;
	    }
	}
	long modInverse(long A,long M) //A and M are coprime
	{
	    extendedEuclid(A,M);
	    return (x%M+M)%M;    //x may be negative
	}
	public static void mergeSort(int[] arr, int l ,int r){
		if((r-l)>=1){
			int mid = (l+r)/2;
			mergeSort(arr,l,mid);
			mergeSort(arr,mid+1,r);
			merge(arr,l,r,mid);
		}
	}
	public static void merge(int arr[], int l, int r, int mid){
		int n1 = (mid-l+1), n2 = (r-mid);
		int left[] = new int[n1];
		int right[] = new int[n2];
		for(int i =0 ;i<n1;i++) left[i] = arr[l+i];
		for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i];
		int i =0, j =0, k = l;
		while(i<n1 && j<n2){
			if(left[i]>right[j]){
				arr[k++] = right[j++];
			}
			else{
				arr[k++] = left[i++];
			}
		}
		while(i<n1) arr[k++] = left[i++];
		while(j<n2) arr[k++] = right[j++];
	}
	public static void mergeSort(long[] arr, int l ,int r){
		if((r-l)>=1){
			int mid = (l+r)/2;
			mergeSort(arr,l,mid);
			mergeSort(arr,mid+1,r);
			merge(arr,l,r,mid);
		}
	}
	public static void merge(long arr[], int l, int r, int mid){
		int n1 = (mid-l+1), n2 = (r-mid);
		long left[] = new long[n1];
		long right[] = new long[n2];
		for(int i =0 ;i<n1;i++) left[i] = arr[l+i];
		for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i];
		int i =0, j =0, k = l;
		while(i<n1 && j<n2){
			if(left[i]>right[j]){
				arr[k++] = right[j++];
			}
			else{
				arr[k++] = left[i++];
			}
		}
		while(i<n1) arr[k++] = left[i++];
		while(j<n2) arr[k++] = right[j++];
	}
	 static class Pair implements Comparable<Pair>{

	      int x,y,k,i;
		Pair (int x,int y,int k){
			this.x=x;
			this.y=y;
			this.k=k;
		}

		public int compareTo(Pair o) {
			return this.y-o.y;
		}
		public boolean equals(Object o) {
			if (o instanceof Pair) {
				Pair p = (Pair)o;
				return p.x == x && p.y == y;
			}
			return false;
		}
		public int hashCode() {
			return new Long(x).hashCode()*31 + new Long(y).hashCode() ;
		}

	        @Override
	        public String toString() {
	            return  "("+x + " " + y +" "+k+" "+i+" )";
	        }

	    }

	    public static boolean isPal(String s){
	        for(int i=0, j=s.length()-1;i<=j;i++,j--){
	                if(s.charAt(i)!=s.charAt(j)) return false;
	        }
	        return true;
	    }
	    public static String rev(String s){
			StringBuilder sb=new StringBuilder(s);
			sb.reverse();
			return sb.toString();
	    }

	    public static long gcd(long x,long y){
		if(x%y==0)
			return y;
		else
			return gcd(y,x%y);
	    }

	    public static int gcd(int x,int y){
	    	if(y==0)
	    		return x;
	    	return gcd(y,x%y);
	    }

	    public static long gcdExtended(long a,long b,long[] x){

	        if(a==0){
	            x[0]=0;
	            x[1]=1;
	            return b;
	        }
	        long[] y=new long[2];
	        long gcd=gcdExtended(b%a, a, y);

	        x[0]=y[1]-(b/a)*y[0];
	        x[1]=y[0];

	        return gcd;
	    }

	    public static int abs(int a,int b){
		return (int)Math.abs(a-b);
	    }

	    public static long abs(long a,long b){
		return (long)Math.abs(a-b);
	    }

	    public static int max(int a,int b){
		if(a>b)
			return a;
		else
			return b;
	    }

	    public static int min(int a,int b){
		if(a>b)
			return b;
		else
			return a;
	    }

	    public static long max(long a,long b){
		if(a>b)
			return a;
		else
			return b;
	    }

	    public static long min(long a,long b){
		if(a>b)
			return b;
		else
			return a;
	    }

	    public static long pow(long n,long p,long m){
		 long  result = 1;
		  if(p==0)
		    return 1;
		if (p==1)
		    return n;
		while(p!=0)
		{
		    if(p%2==1)
		        result *= n;
		    if(result>=m)
		    result%=m;
		    p >>=1;
		    n*=n;
		    if(n>=m)
		    n%=m;
		}
		return result;
	    }

	    public static long pow(long n,long p){
		long  result = 1;
		  if(p==0)
		    return 1;
		if (p==1)
		    return n;
		while(p!=0)
		{
		    if(p%2==1)
		        result *= n;
		    p >>=1;
		    n*=n;
		}
		return result;
	    }
	    public static void debug(Object... o) {
			System.out.println(Arrays.deepToString(o));
		}
	    void run() throws Exception {
			is = System.in;
			out = new PrintWriter(System.out);
			solve();
			out.flush();
		}

	    public static void main(String[] args) throws Exception {
			new Thread(null, new Runnable() {
				public void run() {
					try {
						new code7().run();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}, "1", 1 << 26).start();

		}
	    private byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		private int readByte() {
			if (lenbuf == -1)
				throw new InputMismatchException();
			if (ptrbuf >= lenbuf) {
				ptrbuf = 0;
				try {
					lenbuf = is.read(inbuf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return -1;
			}
			return inbuf[ptrbuf++];
		}

		private boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		private int skip() {
			int b;
			while ((b = readByte()) != -1 && isSpaceChar(b));
			return b;
		}

		private double nd() {
			return Double.parseDouble(ns());
		}

		private char nc() {
			return (char) skip();
		}

		private String ns() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		private char[] ns(int n) {
			char[] buf = new char[n];
			int b = skip(), p = 0;
			while (p < n && !(isSpaceChar(b))) {
				buf[p++] = (char) b;
				b = readByte();
			}
			return n == p ? buf : Arrays.copyOf(buf, p);
		}

		private char[][] nm(int n, int m) {
			char[][] map = new char[n][];
			for (int i = 0; i < n; i++)
				map[i] = ns(m);
			return map;
		}

		private int[] na(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = ni();
			return a;
		}

		private int ni() {
			int num = 0, b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		private long nl() {
			long num = 0;
			int b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

}

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

public class E {

	public static class RT {

		int st;
		int en;
		int val;
		RT l;
		RT r;
		boolean term;

		public RT(int st, int en) {
			this.st = st;
			this.en = en;
			this.val = 0;
			if(st == en) term = true;
			else {
				term = false;
				int hal = (st + en)/2;
				l = new RT(st, hal);
				r = new RT(hal+1, en);
			}
		}

		public void inc(int st, int en, int amn) {
			//System.err.println(st + " " + en + " ");
			//System.err.println("comp to " + this.st + " " + this.en);
			if(st == this.st && en == this.en) {
				this.val += amn;
				return;
			} else if(en <= l.en) {
				l.inc(st, en, amn);
			} else if(st >= r.st) {
				r.inc(st, en, amn);
			} else {
				l.inc(st, l.en, amn);
				r.inc(r.st, en, amn);
			}
		}

		public int val(int i) {
			//System.out.println("I am " + st + " " + en);
			//System.out.println("My val is " + val);
			//System.out.println();
			if(this.term) return val;
			else if(i <= l.en) return val + l.val(i);
			else return val + r.val(i);
		}
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
		String[] ree = in.readLine().split("\\s++");
		int n = Integer.parseInt(ree[0]);
		int r = Integer.parseInt(ree[1]);
		int no = r;
		RT rt = new RT(1, r);
		while(n-- > 0) {
			ree = in.readLine().split("\\s++");
			int a = Integer.parseInt(ree[0]);
			int b = Integer.parseInt(ree[1]);
			rt.inc(a, b, 1);
		}
		int[] arr = new int[no];
		for(int i = 0; i < no; i++) {
			arr[i] = rt.val(i + 1);
			//System.err.println(i + "> " + arr[i]);
		}
		//System.out.println("RT DONE");
		ArrayList<Integer> list = new ArrayList<Integer>();
		for(int i = 0; i < no; i++) {
			//System.err.println("list " + i);
			if(i == 0 || arr[i] >= list.get(list.size()-1)) list.add(arr[i]);
			else {
				//binary search
				int min = 0;
				int max = list.size()-1;
				while(min < max) {
					int test = (min + max)/2;
					if(list.get(test) < arr[i]) {
						min = test + 1;
					} else {
						max = test;
					}
				}
				list.set(max, arr[i]);
			}
		}
		ArrayList<Integer> blist = new ArrayList<Integer>();
		for(int i = no - 1; i >= 0; i--) {
			//System.err.println("blist " + i);
			if(i ==(no - 1) || arr[i] >= blist.get(blist.size()-1)) blist.add(arr[i]);
			else {
				//System.err.println("bs");
				//binary search
				int min = 0;
				int max = blist.size()-1;
				while(min < max) {
					int test = (min + max)/2;
				//System.err.println(min + " m|M " + max + " : " + test);
					if(blist.get(test) < arr[i]) {
						min = test + 1;
					} else {
						max = test;
					}
				}
				blist.set(max, arr[i]);
			}
		}
		int ans = list.size() > blist.size() ? list.size() : blist.size();
		out.write(ans + "\n");
		out.flush();
	}
}

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

public class E {

	public static class RT {

		int st;
		int en;
		int val;
		RT l;
		RT r;
		boolean term;

		public RT(int st, int en) {
			this.st = st;
			this.en = en;
			this.val = 0;
			if(st == en) term = true;
			else {
				term = false;
				int hal = (st + en)/2;
				l = new RT(st, hal);
				r = new RT(hal+1, en);
			}
		}

		public void inc(int st, int en, int amn) {
			//System.err.println(st + " " + en + " ");
			//System.err.println("comp to " + this.st + " " + this.en);
			if(st == this.st && en == this.en) {
				this.val += amn;
				return;
			} else if(en <= l.en) {
				l.inc(st, en, amn);
			} else if(st >= r.st) {
				r.inc(st, en, amn);
			} else {
				l.inc(st, l.en, amn);
				r.inc(r.st, en, amn);
			}
		}

		public int val(int i) {
			//System.out.println("I am " + st + " " + en);
			//System.out.println("My val is " + val);
			//System.out.println();
			if(this.term) return val;
			else if(i <= l.en) return val + l.val(i);
			else return val + r.val(i);
		}
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
		String[] ree = in.readLine().split("\\s++");
		int n = Integer.parseInt(ree[0]);
		int r = Integer.parseInt(ree[1]);
		int no = r;
		RT rt = new RT(1, r);
		while(n-- > 0) {
			ree = in.readLine().split("\\s++");
			int a = Integer.parseInt(ree[0]);
			int b = Integer.parseInt(ree[1]);
			rt.inc(a, b, 1);
		}
		int[] arr = new int[no];
		for(int i = 0; i < no; i++) {
			arr[i] = rt.val(i + 1);
			//System.err.println(i + "> " + arr[i]);
		}
		//System.out.println("RT DONE");
		ArrayList<Integer> list = new ArrayList<Integer>();
		int[] lisrec = new int[no];
		boolean[] lisinc = new boolean[no];
		for(int i = 0; i < no; i++) {
			//System.err.println("list " + i);
			if(i == 0 || arr[i] >= list.get(list.size()-1)) {
				list.add(arr[i]);
				lisinc[i] = true;
			}
			else {
				//binary search
				int min = 0;
				int max = list.size()-1;
				while(min < max) {
					int test = (min + max)/2;
					if(list.get(test) < arr[i]) {
						min = test + 1;
					} else {
						max = test;
					}
				}
				list.set(max, arr[i]);
			}
			lisrec[i] = list.size();
		}
		ArrayList<Integer> blist = new ArrayList<Integer>();
		int[] blisrec = new int[no];
		boolean[] blisinc = new boolean[no];
		for(int i = no - 1; i >= 0; i--) {
			//System.err.println("blist " + i);
			if(i ==(no - 1) || arr[i] >= blist.get(blist.size()-1)) {
				blist.add(arr[i]);
				blisinc[i] = true;
			}
			else {
				//System.err.println("bs");
				//binary search
				int min = 0;
				int max = blist.size()-1;
				while(min < max) {
					int test = (min + max)/2;
				//System.err.println(min + " m|M " + max + " : " + test);
					if(blist.get(test) < arr[i]) {
						min = test + 1;
					} else {
						max = test;
					}
				}
				blist.set(max, arr[i]);
			}
			blisrec[i] = blist.size();
		}
		int ans = 0;
		for(int i = 0; i < no; i++) {
			int c = lisrec[i] + blisrec[i];
			if(lisinc[i] && blisinc[i]) c--;
			if(c > ans) ans = c;
		}
		out.write(ans + "\n");
		out.flush();
	}
}

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

public class E {

	public static class RT {

		int st;
		int en;
		int val;
		RT l;
		RT r;
		boolean term;

		public RT(int st, int en) {
			this.st = st;
			this.en = en;
			this.val = 0;
			if(st == en) term = true;
			else {
				term = false;
				int hal = (st + en)/2;
				l = new RT(st, hal);
				r = new RT(hal+1, en);
			}
		}

		public void inc(int st, int en, int amn) {
			//System.err.println(st + " " + en + " ");
			//System.err.println("comp to " + this.st + " " + this.en);
			if(st == this.st && en == this.en) {
				this.val += amn;
				return;
			} else if(en <= l.en) {
				l.inc(st, en, amn);
			} else if(st >= r.st) {
				r.inc(st, en, amn);
			} else {
				l.inc(st, l.en, amn);
				r.inc(r.st, en, amn);
			}
		}

		public int val(int i) {
			//System.out.println("I am " + st + " " + en);
			//System.out.println("My val is " + val);
			//System.out.println();
			if(this.term) return val;
			else if(i <= l.en) return val + l.val(i);
			else return val + r.val(i);
		}
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
		String[] ree = in.readLine().split("\\s++");
		int n = Integer.parseInt(ree[0]);
		int r = Integer.parseInt(ree[1]);
		int no = r;
		RT rt = new RT(1, r);
		while(n-- > 0) {
			ree = in.readLine().split("\\s++");
			int a = Integer.parseInt(ree[0]);
			int b = Integer.parseInt(ree[1]);
			rt.inc(a, b, 1);
		}
		int[] arr = new int[no];
		for(int i = 0; i < no; i++) {
			arr[i] = rt.val(i + 1);
			//System.err.println(i + "> " + arr[i]);
		}
		//System.out.println("RT DONE");
		ArrayList<Integer> list = new ArrayList<Integer>();
		for(int i = 0; i < no; i++) {
			//System.err.println("list " + i);
			if(i == 0 || arr[i] >= list.get(list.size()-1)) list.add(arr[i]);
			else {
				//binary search
				int min = 0;
				int max = list.size()-1;
				while(min < max) {
					int test = (min + max)/2;
					if(list.get(test) <= arr[i]) {
						min = test + 1;
					} else {
						max = test;
					}
				}
				list.set(max, arr[i]);
			}
		}
		ArrayList<Integer> blist = new ArrayList<Integer>();
		for(int i = no - 1; i >= 0; i--) {
			//System.err.println("blist " + i);
			if(i ==(no - 1) || arr[i] >= blist.get(blist.size()-1)) blist.add(arr[i]);
			else {
				//System.err.println("bs");
				//binary search
				int min = 0;
				int max = blist.size()-1;
				while(min < max) {
					int test = (min + max)/2;
				//System.err.println(min + " m|M " + max + " : " + test);
					if(blist.get(test) <= arr[i]) {
						min = test + 1;
					} else {
						max = test;
					}
				}
				blist.set(max, arr[i]);
			}
		}
		int ans = list.size() > blist.size() ? list.size() : blist.size();
		out.write(ans + "\n");
		out.flush();
	}
}

import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;

import static java.lang.Math.*;

public class Main implements Runnable {
    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        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 nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
        public int nextInt()
        {
            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 long nextLong()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            long 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 double nextDouble()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.')
            {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.')
            {
                c = read();
                double m = 1;
                while (!isSpaceChar(c))
                {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        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 boolean isSpaceChar(int c)
        {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

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

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

    }
    public static void main(String args[]) throws Exception
    {
        new Thread(null, new Main(),"Main",1<<26).start();
    }
    public void run() {
        InputReader sc= new InputReader(System.in);
        PrintWriter w= new PrintWriter(System.out);

        int m = sc.nextInt();
        int n = sc.nextInt();

        int a[] = new int[n];

        for(int i = 0; i < m; ++i) {
            int l = sc.nextInt() - 1;
            int r = sc.nextInt();
            a[l]++;
            if(r < n)
                a[r]--;
        }

        for(int i = 1; i < n; ++i)
            a[i] = a[i - 1] + a[i];

        int smallestEle[] = new int[n];
        smallestEle[0] = a[0];
        int lis[] = new int[n];
        lis[0] = 1;
        int curLength = 1;

        for(int i = 1; i < n; ++i) {
            if(a[i] >= smallestEle[curLength - 1])
                smallestEle[curLength++] = a[i];
            else {
                int start = 0, end = curLength - 2, pos = -1;
                while(start <= end) {
                    int mid = (start + end) >> 1;
                    if(a[i] >= mid) {
                        pos = mid;
                        start = mid + 1;
                    }
                    else
                        end = mid - 1;
                }
                smallestEle[pos + 1] = a[i];
            }
            lis[i] = curLength;
        }

        smallestEle = new int[n];
        smallestEle[0] = a[n - 1];
        int lds[] = new int[n];
        lds[n - 1] = 1;
        curLength = 1;

        for(int i = n - 2; i >= 0; --i) {
            if(a[i] >= smallestEle[curLength - 1])
                smallestEle[curLength++] = a[i];
            else {
                int start = 0, end = curLength - 2, pos = -1;
                while(start <= end) {
                    int mid = (start + end) >> 1;
                    if(a[i] >= mid) {
                        pos = mid;
                        start = mid + 1;
                    }
                    else
                        end = mid - 1;
                }
                smallestEle[pos + 1] = a[i];
            }
            lds[i] = curLength;
        }

        int maxid = lds[0];

        for(int i = 1; i < n; ++i)
            maxid = max(maxid, lis[i - 1] + lds[i]);

        w.print(maxid);

        w.close();
    }
}

import java.util.ArrayList;
import java.util.Scanner;
import java.lang.StringBuilder;
import java.util.Arrays;
import java.util.Stack;
import java.util.TreeMap;
public class Test10   {
    public static void main(String[] Args)   {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] a = new int[m + 1];
        int[] diff = new int[m + 2]; // diff[l] = cnt[l] - cnt[l - 1]
        for (int i = 0; i < n; i++)   {
            int l = sc.nextInt();
            int r = sc.nextInt();
            diff[l]++;
            diff[r + 1]--;
        }
        a[0] = diff[0];
        for (int i = 1; i <= m; i++)
            a[i] = a[i - 1] + diff[i];
        //dp + IST
        int[] mx1 = new int[m + 1];
        int pw2 = 1 << 20;
        int[] ist = new int[pw2 + 1]; //ist : 1100000 - 1000000
        for (int i = 0; i <= m; i++)   {
            //IST search
            int j = a[i];
            int k = 0;
            pw2 = 1 << 20;
            int mx = 0;
            for (int l = 19; l >= 0; l--)   {
                pw2 /= 2;
                if (j >= pw2)   {
                    k += pw2;
                    mx = Math.max(mx, ist[k]);
                    j -= pw2;
                }
            }
            mx1[i] = mx + 1;
            //update ist
            int s = a[i];
            ist[s] = Math.max(ist[s], mx1[i]);
            int huge = 1;
            for (int l = 0; l <= 20; l++)   {
                huge *= 2;
                if (s % 2 == 0)   {
                    int o = (s + 1) << l;
                    ist[o] = Math.max(ist[o], mx1[i]);
                }
                s = s / 2;
            }
        }
        //do everything backward
        int[] b = new int[m + 1];
        for (int i = 0; i <= m; i++) b[i] = a[m - i];
        //dp + IST
        int[] mx2 = new int[m + 1];
        pw2 = 1 << 20;
        ist = new int[pw2 + 1]; //ist : 1100000 - 1000000
        for (int i = 0; i <= m; i++)   {
            //IST search
            int j = b[i];
            int k = 0;
            pw2 = 1 << 20;
            int mx = 0;
            for (int l = 19; l >= 0; l--)   {
                pw2 /= 2;
                if (j >= pw2)   {
                    k += pw2;
                    mx = Math.max(mx, ist[k]);
                    j -= pw2;
                }
            }
            mx2[i] = mx + 1;
            //update ist
            int s = b[i];
            ist[s] = Math.max(ist[s], mx2[i]);
            int huge = 1;
            for (int l = 0; l <= 20; l++)   {
                huge *= 2;
                if (s % 2 == 0)   {
                    int o = (s + 1) << l;
                    ist[o] = Math.max(ist[o], mx2[i]);
                }
                s = s / 2;
            }
        }
        //get result
        int mx = 0;
        for (int i = 0; i <= m; i++)
            if (mx1[i] + mx2[m - i] > mx) mx = mx1[i] + mx2[m - i];
        System.out.println(mx - 2);
   }
}

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

public class utkarsh {

    InputStream is;
    PrintWriter out;

    void solve() {
        //Enter code here utkarsh
        int n = ni();
        int m = ni();
        int a[] = new int[m+1];
        for(int i = 0; i < n; i++) {
            a[ni() - 1]++;
            a[ni()]--;
        }
        for(int i = 1; i <= m; i++) {
            a[i] += a[i-1];
        }
        if(m <= 2) {
            out.println(m);
            return;
        }
        int d[] = new int[m];
        int x[] = new int[m];
        int y[] = new int[m];
        x[0] = y[m-1] = 1;
        int z;

        d[0] = a[0];
        z = 1;
        for(int i = 1; i < m; i++) {
            int j = Arrays.binarySearch(d, 0, z, a[i]+1);
            if(j < 0)   j = -j - 1;
            if(j == z)  d[z++] = a[i];
            else    d[j] = a[i];
            x[i] = z;
        }

        d[0] = a[m-1];
        z = 1;
        for(int i = m-2; i >= 0; i--) {
            int j = Arrays.binarySearch(d, 0, z, a[i]+1);
            if(j < 0)   j = -j - 1;
            if(j == z)  d[z++] = a[i];
            else    d[j] = a[i];
            y[i] = z;
        }

        int ans = 0;
        for(int i = 0; i < m; i++) {
            //out.println(x[i] +" "+ y[i]);
            ans = Math.max(ans, x[i] + y[i] - 1);
        }
        out.println(ans);
    }

    //---------- I/O Template ----------

    public static void main(String[] args) { new utkarsh().run(); }
    void run() {
        is = System.in;
        out = new PrintWriter(System.out);
        solve();
        out.flush();
    }

    byte input[] = new byte[1024];
    int len = 0, ptr = 0;

    int readByte() {
        if(ptr >= len) { ptr = 0;
            try { len = is.read(input); }
            catch(IOException e) { throw new InputMismatchException(); }
            if(len <= 0) { return -1; }
        } return input[ptr++];
    }
    boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); }
    int skip() {
        int b = readByte();
        while(b != -1 && isSpaceChar(b)) { b = readByte(); }
        return b;
    }

    int ni() {
        int n = 0, b = readByte();
        boolean minus = false;
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
        if(b == '-') { minus = true; b = readByte(); }
        if(b == -1) { return -1; }  //no input
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
        return minus ? -n : n;
    }
}