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.*;

public class Main3 {
    public static void main(String[] args) throws IOException {
        long n = nextInt();
        long k = nextInt();
        long c = 1;
        long h = 1;
        for (int i = 1; i <= k; i += 1) {
            c = (c*(n - i + 1)) % mod;
            c = (c * step(i, mod - 2) % mod);
            h = (h+c)%mod;
        }
        out.print(h%mod);
        out.close();
    }

    static long step(int a, int b) {
        if (b == 0) {
            return 1;
        } else if (b % 2 == 0) {
            long s = step(a, b/2);
            return s % mod * s % mod;
        } else {
            long s = step(a, b / 2);
            return s % mod * s % mod * a % mod;
        }
    }

    static int mod = (int) (1e9 + 7);

    static class Pair {
        int x, type;

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

    static class PairComparator implements Comparator<Pair> {
        @Override
        public int compare(Pair o1, Pair o2) {
            if (o1.x != o2.x) return Integer.compare(o1.x, o2.x);
            return Integer.compare(o1.type, o2.type);
        }
    }

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(System.out);
    static StringTokenizer in = new StringTokenizer("");

    public static boolean hasNext() throws IOException {
        if (in.hasMoreTokens()) return true;
        String s;
        while ((s = br.readLine()) != null) {
            in = new StringTokenizer(s);
            if (in.hasMoreTokens()) return true;
        }
        return false;
    }

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

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

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

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


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

public class A {

	static final int mod = (int)1e9 + 9;

	static int mult(long x, long y) { return (int)(x * y % mod); }

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

		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int[] fac = new int[4001];
		fac[0] = 1;
		for(int i = 1; i <= 4000; ++i)
			fac[i] = mult(fac[i - 1], i);

		int[][][] dp = new int[2][4001][4001];
		dp[0][0][0] = 1;
		for(int days = 1; days <= 4000; ++days)
			for(int items = 0; items <= 4000; ++items)
				for(int canSkip = 0; canSkip < 2; ++canSkip)
				{
					int ret = 0;
					if(items > 0)
						ret += dp[1][days][items - 1];
					if(canSkip == 1)
						ret += dp[0][days - 1][items];
					ret %= mod;
					dp[canSkip][days][items] = ret;
				}
		int n = sc.nextInt(), w = sc.nextInt(), b = sc.nextInt();
		int ans = 0;
		for(int before = 1; before < n && before < w; ++before)
			for(int block = 1; before + block < n && block <= b; ++block)
			{
				int after = n - before - block;
				ans += mult(dp[0][before + after][w], dp[0][block][b]);
				ans %= mod;
			}
		out.println(mult(mult(fac[b], fac[w]), ans));
		out.close();
	}

	static class Scanner
	{
		StringTokenizer st;
		BufferedReader br;

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

		public Scanner(FileReader s) throws FileNotFoundException {	br = new BufferedReader(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.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CF1139D {
    public static void main(String[] args) throws Exception {
        boolean local = System.getProperty("ONLINE_JUDGE") == null;
        boolean async = false;

        Charset charset = Charset.forName("ascii");

        FastIO io = local ? new FastIO(new FileInputStream("D:\\DATABASE\\TESTCASE\\Code.in"), System.out, charset) : new FastIO(System.in, System.out, charset);
        Task task = new Task(io, new Debug(local));

        if (async) {
            Thread t = new Thread(null, task, "dalt", 1 << 27);
            t.setPriority(Thread.MAX_PRIORITY);
            t.start();
            t.join();
        } else {
            task.run();
        }

        if (local) {
            io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M");
        }

        io.flush();
    }

    public static class Task implements Runnable {
        final FastIO io;
        final Debug debug;
        int inf = (int) 1e8;
        int mod = (int) 1e9 + 7;
        int[] mu = new int[100001];

        void init() {
            int limit = 100000;
            int[] primes = new int[limit + 1];
            boolean[] isComp = new boolean[limit + 1];
            int wpos = 0;
            mu[1] = 1;
            int[] smallestFactor = new int[limit + 1];
            int[] smallestFactorExp = new int[limit + 1];
            for (int i = 2; i <= limit; i++) {
                if (!isComp[i]) {
                    primes[wpos++] = i;
                    mu[i] = -1;
                    smallestFactor[i] = i;
                    smallestFactorExp[i] = 1;
                } else {
                    if (smallestFactorExp[i] > 1) {
                        mu[i] = 0;
                    } else {
                        mu[i] = -mu[i / smallestFactor[i]];
                    }
                }
                for (int j = 0, until = limit / i; j < wpos && primes[j] <= until; j++) {
                    int pi = primes[j] * i;
                    smallestFactor[pi] = primes[j];
                    smallestFactorExp[pi] = i % primes[j] == 0 ?
                            smallestFactorExp[i] + 1 : 1;
                    isComp[pi] = true;
                    if (i % primes[j] == 0) {
                        break;
                    }
                }
            }
        }

        public int mod(int val) {
            val %= mod;
            if (val < 0) {
                val += mod;
            }
            return val;
        }

        public int mod(long val) {
            val %= mod;
            if (val < 0) {
                val += mod;
            }
            return (int) val;
        }

        int bitAt(int x, int i) {
            return (x >> i) & 1;
        }

        int bitAt(long x, int i) {
            return (int) ((x >> i) & 1);
        }

        public Task(FastIO io, Debug debug) {
            this.io = io;
            this.debug = debug;
            init();
        }

        @Override
        public void run() {
            solve();
        }

        public void bruteForce() {
            int m = io.readInt();
            long[] dp = new long[m + 1];
            dp[1] = 0;
            int[] count = new int[m + 1];
            long inv = Mathematics.inverse(m, mod);
            long sum = 0;
            for (int i = 2; i <= m; i++) {
                long total = 0;
                long coe = 0;
                Arrays.fill(count, 0);
                for (int j = 1; j <= m; j++) {
                    int g = Mathematics.gcd(i, j);

                    count[g]++;
                    if (g == i) {
                        coe = mod(coe + inv);
                    } else {
                        total = mod(total + dp[g] * inv);
                    }
                }
                total = mod(total + 1);
                coe = mod(1 - coe);
                dp[i] = mod(total * Mathematics.inverse((int) coe, mod));
                sum = mod(sum + dp[i] * inv);
            }

            sum = mod(sum + 1);
            io.cache.append(sum).append('\n');

        }

        public void solve() {
            int m = io.readInt();
            List<Integer>[] factors = new List[m + 1];
            for (int i = 1; i <= m; i++) {
                factors[i] = new ArrayList<>();
            }
            for (int i = 1; i <= m; i++) {
                for (int j = i; j <= m; j += i) {
                    factors[j].add(i);
                }
            }

            int[] dp1 = new int[m + 1];
            dp1[1] = 0;
            long inv = Mathematics.inverse(m, mod);
            long expect = 0;
            for (int i = 2; i <= m; i++) {
                dp1[i] = 0;
                long total = 0;
                long coe = 0;
                for (int x : factors[i]) {
                    long count = 0;
                    for (int d : factors[i / x]) {
                        count = mod(count + mu[d] * ((m / x) / d));
                    }
                    if (x < i) {
                        total = mod(total + count * dp1[x]);
                    } else {
                        coe = mod(1 - inv * count);
                    }
                }
                dp1[i] = mod((long) total * inv + 1);
                dp1[i] = mod((long) dp1[i] * Mathematics.inverse((int) coe, mod));
                expect = mod(expect + dp1[i]);
            }

            expect = mod(expect * inv + 1);

            io.cache.append(expect);
        }
    }

    public static class Mathematics {

        public static int ceilPowerOf2(int x) {
            return 32 - Integer.numberOfLeadingZeros(x - 1);
        }

        public static int floorPowerOf2(int x) {
            return 31 - Integer.numberOfLeadingZeros(x);
        }

        public static long modmul(long a, long b, long mod) {
            return b == 0 ? 0 : ((modmul(a, b >> 1, mod) << 1) % mod + a * (b & 1)) % mod;
        }

        /**
         * Get the greatest common divisor of a and b
         */
        public static int gcd(int a, int b) {
            return a >= b ? gcd0(a, b) : gcd0(b, a);
        }

        private static int gcd0(int a, int b) {
            return b == 0 ? a : gcd0(b, a % b);
        }

        public static int extgcd(int a, int b, int[] coe) {
            if (a >= b) {
                return extgcd0(a, b, coe);
            } else {
                int g = extgcd0(b, a, coe);
                int tmp = coe[0];
                coe[0] = coe[1];
                coe[1] = tmp;
                return g;
            }
        }

        private static int extgcd0(int a, int b, int[] coe) {
            if (b == 0) {
                coe[0] = 1;
                coe[1] = 0;
                return a;
            }
            int g = extgcd0(b, a % b, coe);
            int n = coe[0];
            int m = coe[1];
            coe[0] = m;
            coe[1] = n - m * (a / b);
            return g;
        }

        /**
         * Get the greatest common divisor of a and b
         */
        public static long gcd(long a, long b) {
            return a >= b ? gcd0(a, b) : gcd0(b, a);
        }

        private static long gcd0(long a, long b) {
            return b == 0 ? a : gcd0(b, a % b);
        }

        public static long extgcd(long a, long b, long[] coe) {
            if (a >= b) {
                return extgcd0(a, b, coe);
            } else {
                long g = extgcd0(b, a, coe);
                long tmp = coe[0];
                coe[0] = coe[1];
                coe[1] = tmp;
                return g;
            }
        }

        private static long extgcd0(long a, long b, long[] coe) {
            if (b == 0) {
                coe[0] = 1;
                coe[1] = 0;
                return a;
            }
            long g = extgcd0(b, a % b, coe);
            long n = coe[0];
            long m = coe[1];
            coe[0] = m;
            coe[1] = n - m * (a / b);
            return g;
        }

        /**
         * Get y where x * y = 1 (% mod)
         */
        public static int inverse(int x, int mod) {
            return pow(x, mod - 2, mod);
        }

        /**
         * Get x^n(% mod)
         */
        public static int pow(int x, int n, int mod) {
            int bit = 31 - Integer.numberOfLeadingZeros(n);
            long product = 1;
            for (; bit >= 0; bit--) {
                product = product * product % mod;
                if (((1 << bit) & n) != 0) {
                    product = product * x % mod;
                }
            }
            return (int) product;
        }

        public static long longpow(long x, long n, long mod) {
            if (n == 0) {
                return 1;
            }
            long prod = longpow(x, n >> 1, mod);
            prod = modmul(prod, prod, mod);
            if ((n & 1) == 1) {
                prod = modmul(prod, x, mod);
            }
            return prod;
        }

        /**
         * Get x % mod
         */
        public static int mod(int x, int mod) {
            x %= mod;
            if (x < 0) {
                x += mod;
            }
            return x;
        }

        public static int mod(long x, int mod) {
            x %= mod;
            if (x < 0) {
                x += mod;
            }
            return (int) x;
        }

        /**
         * Get n!/(n-m)!
         */
        public static long permute(int n, int m) {
            return m == 0 ? 1 : n * permute(n - 1, m - 1);
        }

        /**
         * Put all primes less or equal to limit into primes after offset
         */
        public static int eulerSieve(int limit, int[] primes, int offset) {
            boolean[] isComp = new boolean[limit + 1];
            int wpos = offset;
            for (int i = 2; i <= limit; i++) {
                if (!isComp[i]) {
                    primes[wpos++] = i;
                }
                for (int j = offset, until = limit / i; j < wpos && primes[j] <= until; j++) {
                    int pi = primes[j] * i;
                    isComp[pi] = true;
                    if (i % primes[j] == 0) {
                        break;
                    }
                }
            }
            return wpos - offset;
        }

        /**
         * Round x into integer
         */
        public static int intRound(double x) {
            if (x < 0) {
                return -(int) (-x + 0.5);
            }
            return (int) (x + 0.5);
        }

        /**
         * Round x into long
         */
        public static long longRound(double x) {
            if (x < 0) {
                return -(long) (-x + 0.5);
            }
            return (long) (x + 0.5);
        }
    }

    public static class FastIO {
        public final StringBuilder cache = new StringBuilder();
        private final InputStream is;
        private final OutputStream os;
        private final Charset charset;
        private StringBuilder defaultStringBuf = new StringBuilder(1 << 8);
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastIO(InputStream is, OutputStream os, Charset charset) {
            this.is = is;
            this.os = os;
            this.charset = charset;
        }

        public FastIO(InputStream is, OutputStream os) {
            this(is, os, Charset.forName("ascii"));
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                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;
        }

        public long readLong() {
            int sign = 1;

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

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

        public double readDouble() {
            boolean sign = true;
            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+';
                next = read();
            }

            long val = 0;
            while (next >= '0' && next <= '9') {
                val = val * 10 + next - '0';
                next = read();
            }
            if (next != '.') {
                return sign ? val : -val;
            }
            next = read();
            long radix = 1;
            long point = 0;
            while (next >= '0' && next <= '9') {
                point = point * 10 + next - '0';
                radix = radix * 10;
                next = read();
            }
            double result = val + (double) point / radix;
            return sign ? result : -result;
        }

        public String readString(StringBuilder builder) {
            skipBlank();

            while (next > 32) {
                builder.append((char) next);
                next = read();
            }

            return builder.toString();
        }

        public String readString() {
            defaultStringBuf.setLength(0);
            return readString(defaultStringBuf);
        }

        public int readLine(char[] data, int offset) {
            int originalOffset = offset;
            while (next != -1 && next != '\n') {
                data[offset++] = (char) next;
                next = read();
            }
            return offset - originalOffset;
        }

        public int readString(char[] data, int offset) {
            skipBlank();

            int originalOffset = offset;
            while (next > 32) {
                data[offset++] = (char) next;
                next = read();
            }

            return offset - originalOffset;
        }

        public int readString(byte[] data, int offset) {
            skipBlank();

            int originalOffset = offset;
            while (next > 32) {
                data[offset++] = (byte) next;
                next = read();
            }

            return offset - originalOffset;
        }

        public char readChar() {
            skipBlank();
            char c = (char) next;
            next = read();
            return c;
        }

        public void flush() {
            try {
                os.write(cache.toString().getBytes(charset));
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public boolean hasMore() {
            skipBlank();
            return next != -1;
        }
    }

    public static class Debug {
        private boolean allowDebug;

        public Debug(boolean allowDebug) {
            this.allowDebug = allowDebug;
        }

        public void assertTrue(boolean flag) {
            if (!allowDebug) {
                return;
            }
            if (!flag) {
                fail();
            }
        }

        public void fail() {
            throw new RuntimeException();
        }

        public void assertFalse(boolean flag) {
            if (!allowDebug) {
                return;
            }
            if (flag) {
                fail();
            }
        }

        private void outputName(String name) {
            System.out.print(name + " = ");
        }

        public void debug(String name, int x) {
            if (!allowDebug) {
                return;
            }

            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, long x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, double x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, int[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, long[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, double[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, Object x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, Object... x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.deepToString(x));
        }
    }
}


    import java.awt.Frame;
import java.io.BufferedReader;
import java.io.FileReader;
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.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

public class Main {
	static ArrayList<Integer> adj[];
	// static PrintWriter out = new PrintWriter(System.out);

	static int[][] notmemo;
	static int k;
	static int[] a;
	static int b[];
	static int m;

	static class Pair implements Comparable<Pair> {
		int d;
		int s;

		public Pair(int b, int l) {
			d = b;
			s = l;
		}

		@Override
		public int compareTo(Pair o) {
			if (o.d - o.s > this.d - this.s) {
				return 1;
			} else if (o.d - o.s < this.d - this.s) {
				return -1;
			} else
				return 0;

		}

	}

	static Pair s1[];
	static ArrayList<Pair> adjlist[];
	// static char c[];
	public static long mod = (long) (1e9 + 7);
	static int V;
	static long INF = (long) 1E16;
	static int n;
	static char c[];
	static int d[];
	static int z;
	static Pair p[];
static int R;
static int C;
static int K;
public static void main(String args[]) throws Exception {

	Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
			n=sc.nextInt();
			moves=sc.nextInt();
			ss=sc.nextLine().toCharArray();
			String m=sc.next();
			first=m.charAt(0);
			second=m.charAt(1);
			memo=new int[200][200][200];
			for(int x[][]:memo) {
				for(int y[]:x) {
					Arrays.fill(y,-1);
				}
			}
		System.out.println(dp(0,0,moves));

out.flush();

}
static int dp(int i,int count1,int maxmoves) {
	if(i==n)
		return 0;
	if(memo[i][count1][maxmoves]!=-1)
		return memo[i][count1][maxmoves];
	int max=0;
	if(ss[i]==first&&ss[i]==second) {
		max=Math.max(max,count1+dp(i+1,count1+1,maxmoves));
	}
	if(first==second&&maxmoves>0) {
		max=Math.max(max,count1+dp(i+1,count1+1,maxmoves-1));
	}

	if(maxmoves>0&&ss[i]!=first)
	max=Math.max(max,dp(i+1,count1+1,maxmoves-1));
	if(maxmoves>0&&ss[i]!=second)
	max=Math.max(max,count1+dp(i+1,count1,maxmoves-1));
	if(ss[i]==first) {
		max=Math.max(max,dp(i+1,count1+1,maxmoves));
	}
	 if(ss[i]==second) {
		 max=Math.max(max,count1+dp(i+1,count1,maxmoves));

	}
	max=Math.max(max,dp(i+1,count1,maxmoves));
return memo[i][count1][maxmoves]=max;
}
static int memoo[][][];
static char ss[];
static int moves;
static char first;
static char second;

static  class FenwickTree { // one-based DS

		int n;
		int[] ft;

		FenwickTree(int size) { n = size; ft = new int[n+1]; }

		int rsq(int b) //O(log n)
		{
			int sum = 0;
			while(b > 0) { sum += ft[b]; b -= b & -b;}		//min?
			return sum;
		}

		int rsq(int a, int b) { return rsq(b) - rsq(a-1); }

		void point_update(int k, int val)	//O(log n), update = increment
		{
			while(k <= n) { ft[k] += val; k += k & -k; }		//min?
		}
	}

static long total[];
static TreeMap<Integer,Integer> map1;

		static int zz;
//static int dp(int idx,int left,int state) {
//if(idx>=k-((zz-left)*2)||idx+1==n) {
//	return 0;
//}
//if(memo[idx][left][state]!=-1) {
//	return memo[idx][left][state];
//}
//int ans=a[idx+1]+dp(idx+1,left,0);
//if(left>0&&state==0&&idx>0) {
//	ans=Math.max(ans,a[idx-1]+dp(idx-1,left-1,1));
//}
//return memo[idx][left][state]=ans;
//}21
static HashMap<Integer,Integer> map;
static int maxa=0;
static int ff=123123;

static long dist[][];
static int[][][] memo;

	static long modmod=998244353;
	static int dx[]= {1,-1,0,0};
	 static int dy[]= {0,0,1,-1};
	static class BBOOK implements Comparable<BBOOK>{
		int t;
		int alice;
		int bob;
		public BBOOK(int x,int y,int z) {
		t=x;
		alice=y;
		bob=z;
		}
		@Override
		public int compareTo(BBOOK o) {
			return this.t-o.t;
		}
	}
	private static long lcm(long a2, long b2) {
	return (a2*b2)/gcd(a2,b2);
	}
	static class Edge implements Comparable<Edge>
	{
		int node;long cost ; long time;

		Edge(int a, long b,long c) { node = a;	cost = b; time=c; }

		public int compareTo(Edge e){ return Long.compare(time,e.time);	}
	}

        static void sieve(int N)	// O(N log log N)
	{
		isComposite = new int[N+1];
		isComposite[0] = isComposite[1] = 1;			// 0 indicates a prime number
		primes = new ArrayList<Integer>();

		for (int i = 2; i <= N; ++i) 					//can loop till i*i <= N if primes array is not needed O(N log log sqrt(N))
			if (isComposite[i] == 0) 					//can loop in 2 and odd integers for slightly better performance
			{
				primes.add(i);
				if(1l * i * i <= N)
					for (int j = i * i; j <= N; j += i)	// j = i * 2 will not affect performance too much, may alter in modified sieve
						isComposite[j] = 1;
			}
	}
    static TreeSet<Integer> factors;
    static ArrayList<Integer> primeFactors(int N)		// O(sqrt(N) / ln sqrt(N))
	{
		ArrayList<Integer> factors = new ArrayList<Integer>();		//take abs(N) in case of -ve integers
		int idx = 0, p = primes.get(idx);

		while(1l*p * p <= N)
		{
			while(N % p == 0) { factors.add(p); N /= p; }
			p = primes.get(++idx);
		}

		if(N != 1)						// last prime factor may be > sqrt(N)
			factors.add(N);				// for integers whose largest prime factor has a power of 1
		return factors;
	}

	static class UnionFind {
		int[] p, rank, setSize;
		int numSets;
		int max[];

		public UnionFind(int N) {
			p = new int[numSets = N];
			rank = new int[N];
			setSize = new int[N];
			for (int i = 0; i < N; i++) {
				p[i] = i;
				setSize[i] = 1;
			}
		}

		public int findSet(int i) {
			return p[i] == i ? i : (p[i] = findSet(p[i]));
		}

		public boolean isSameSet(int i, int j) {
			return findSet(i) == findSet(j);
		}

		public void unionSet(int i, int j) {
			if (isSameSet(i, j))
				return;
			numSets--;
			int x = findSet(i), y = findSet(j);
			if (rank[x] > rank[y]) {
				p[y] = x;
				setSize[x] += setSize[y];

			} else {
				p[x] = y;
				setSize[y] += setSize[x];
				if (rank[x] == rank[y])
					rank[y]++;

			}
		}

		public int numDisjointSets() {
			return numSets;
		}

		public int sizeOfSet(int i) {
			return setSize[findSet(i)];
		}
	}

	static class Quad implements Comparable<Quad> {
		int u;
		int v;
		char state;
		int turns;

		public Quad(int i, int j, char c, int k) {
			u = i;
			v = j;
			state = c;
			turns = k;
		}

		public int compareTo(Quad e) {
			return (int) (turns - e.turns);
		}

	}

	static long manhatandistance(long x, long x2, long y, long y2) {
		return Math.abs(x - x2) + Math.abs(y - y2);
	}

	static long fib[];

	static long fib(int n) {
		if (n == 1 || n == 0) {
			return 1;
		}
		if (fib[n] != -1) {
			return fib[n];
		} else
			return fib[n] = ((fib(n - 2) % mod + fib(n - 1) % mod) % mod);
	}

	static class Point  implements Comparable<Point>{
		long x, y;

		Point(long counth, long counts) {
			x = counth;
			y = counts;
		}

		@Override
		public int compareTo(Point p )
		{
			return Long.compare(p.y*1l*x, p.x*1l*y);
		}
	}

	static TreeSet<Long> primeFactors(long N) // O(sqrt(N) / ln sqrt(N))
	{
		TreeSet<Long> factors = new TreeSet<Long>(); // take abs(N) in case of -ve integers
		int idx = 0, p = primes.get(idx);

		while (p * p <= N) {
			while (N % p == 0) {
				factors.add((long) p);
				N /= p;
			}
			if (primes.size() > idx + 1)
				p = primes.get(++idx);
			else
				break;
		}

		if (N != 1) // last prime factor may be > sqrt(N)
			factors.add(N); // for integers whose largest prime factor has a power of 1
		return factors;
	}

	static boolean visited[];

	/**
	 * static int bfs(int s) { Queue<Integer> q = new LinkedList<Integer>();
	 * q.add(s); int count=0; int maxcost=0; int dist[]=new int[n]; dist[s]=0;
	 * while(!q.isEmpty()) {
	 *
	 * int u = q.remove(); if(dist[u]==k) { break; } for(Pair v: adj[u]) {
	 * maxcost=Math.max(maxcost, v.cost);
	 *
	 *
	 *
	 * if(!visited[v.v]) {
	 *
	 * visited[v.v]=true; q.add(v.v); dist[v.v]=dist[u]+1; maxcost=Math.max(maxcost,
	 * v.cost); } }
	 *
	 * } return maxcost; }
	 **/

	static boolean[] vis2;

	static boolean f2 = false;

	static long[][] matMul(long[][] a2, long[][] b, int p, int q, int r) // C(p x r) = A(p x q) x (q x r) -- O(p x q x
																			// r)
	{
		long[][] C = new long[p][r];
		for (int i = 0; i < p; ++i) {
			for (int j = 0; j < r; ++j) {
				for (int k = 0; k < q; ++k) {
					C[i][j] = (C[i][j] + (a2[i][k] % mod * b[k][j] % mod)) % mod;
					C[i][j] %= mod;
				}

			}
		}
		return C;
	}
	public static int[] schuffle(int[] a2) {
		for (int i = 0; i < a2.length; i++) {
			int x =  (int) (Math.random() * a2.length);
			int temp = a2[x];
			a2[x] = a2[i];
			a2[i] = temp;
		}
		return a2;
	}

	static boolean vis[];
	static HashSet<Integer> set = new HashSet<Integer>();

	static long modPow(long ways, long count, long mod) // O(log e)
	{
		ways %= mod;
		long res = 1;
		while (count > 0) {
			if ((count & 1) == 1)
				res = (res * ways) % mod;
			ways = (ways * ways) % mod;
			count >>= 1;
		}
		return res % mod;
	}

	static long gcd(long l, long o) {
		if (o == 0) {
			return l;
		}
		return gcd(o, l % o);
	}

	static int[] isComposite;
	static int[] valid;

	static ArrayList<Integer> primes;
	static ArrayList<Integer> l1;

	static TreeSet<Integer> primus = new TreeSet<Integer>();

	static void sieveLinear(int N)
	{
		int[] lp = new int[N + 1];								//lp[i] = least prime divisor of i
		for(int i = 2; i <= N; ++i)
		{
			if(lp[i] == 0)
			{
				primus.add(i);
				lp[i] = i;
			}
			int curLP = lp[i];
			for(int p: primus)
				if(p > curLP || p * i > N)
					break;
				else
					lp[p * i] = i;
		}
	}

	public static long[] schuffle(long[] a2) {
		for (int i = 0; i < a2.length; i++) {
			int x =  (int) (Math.random() * a2.length);
			long temp = a2[x];
			a2[x] = a2[i];
			a2[i] = temp;
		}
		return a2;
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

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

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

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

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

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

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

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

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

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

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}

		public int[] nxtArr(int n) throws IOException {
			int[] ans = new int[n];
			for (int i = 0; i < n; i++)
				ans[i] = nextInt();
			return ans;
		}

	}
	public static int[] sortarray(int a[]) {
		schuffle(a);
		Arrays.sort(a);
		return a;
		}
		public static long[] sortarray(long a[]) {
			schuffle(a);
			Arrays.sort(a);
			return a;
		}
}


import java.util.Scanner;
public class Chores {
    public Chores() {
    }
    public static void main(String[] args) {
    Scanner kbd = new Scanner(System.in);
    System.out.println('');
  }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

import org.omg.CORBA.PUBLIC_MEMBER;

public class my_class {

	public static ArrayList<Integer> nums = new ArrayList<>();
	public static int rec(int i, int num, int arr[]){
		if(i < arr.length){
			rec(i + 1, num + arr[i], arr);
			rec(i + 1, num - arr[i], arr);
			if(i != 0)
				rec(i + 1, num * arr[i], arr);
		}else{
			if(!nums.contains(Math.abs(num)) && (num != 0)){
				nums.add(Math.abs(num));
			}
		}
		return 0;
	}
	public static void rec1(int arr[], int qwer){
		if(arr.length >= 2){
			rec(0,0, arr);
			if(arr.length > 2){
				for(int i = 0; i < arr.length - 1;i++){
					if(i + 1 != qwer){
						int[] tmpA = new int[arr.length - 1];
						for(int s = 0; s < tmpA.length; s++){
							if(s < i)
								tmpA[s] = arr[s];
							if(s == i)
								tmpA[s] = arr[s]*10 + arr[s + 1];
							if(s > i)
								tmpA[s] = arr[s + 1];
						}
						rec1(tmpA,i);
					}
				}
			}
		}
	}
	public static void main(String[] args) throws Exception{
		int  k = nextInt(), m = nextInt();
		int sum = 0;
		for(int counter = 0; counter <= 9999;counter++){
			int arr[] = new int[4];
			nums.clear();
			int tmp = counter;
			for(int i = arr.length - 1;tmp != 0;i--){
				arr[i] = tmp % 10;
				tmp = tmp / 10;
			}
			nums.add(counter);
			rec1(arr,0);
			int l;
			for(int i = 0; i < nums.size();i++){
				if(sum > m - 1)
					break;
				if(nums.get(i)/10000 == 0){
					if(nums.get(i) > k)
						l = nums.get(i) - k;
					else
						l = k - nums.get(i);
					l = (((l*10 + arr[0])*10 + arr[1])*10 + arr[2])*10 + arr[3];
					tmp  = l;
					int s = 0;
					while(tmp/10 != 0){
						tmp = tmp/10;
						s++;
					}
					s = 7 - s;
					while(s != 0){
						print(0);
						s--;
					}
					println(l);
					sum++;
				}
			}
			if(sum > m - 1){
				break;
			}
		}
		//println(sum);
	}
	private static PrintWriter out = new PrintWriter(System.out);
	private static BufferedReader inB = new BufferedReader(new InputStreamReader(System.in));
	private static StreamTokenizer in = new StreamTokenizer(inB);

	private static void exit(Object o) throws Exception {
		out.println(o);
		out.flush();
		System.exit(0);
	}
	private static void println(Object o) throws Exception{
		out.println(o);
		out.flush();
	}
	private static void print(Object o) throws Exception{
		out.print(o);
		out.flush();
	}
	private static long nextLong() throws Exception {
		in.nextToken();
		return (long)in.nval;
	}
	private static int nextInt() throws Exception {
		in.nextToken();
		return (int)in.nval;
	}
	private static String nextString() throws Exception {
		in.nextToken();
		return in.sval;
	}
}


import java.io.*;
import java.time.temporal.IsoFields;
import java.util.*;

public class e1 {
	static int a[][];
	static Scanner in;
	static long curans;
	static long ans;

	static int read() {
		return in.nextInt();
	}

	static void gen1(int i, int n){
		try {
			a[i] = new int[n];

			gen(i, 0, n);
			gen1(i + 1, n);

		} catch (ArrayIndexOutOfBoundsException e) {
			return;
		}
	}

	static void gen(int i, int j, int n) {
		try {
			a[i][j] = 0;

		} catch (ArrayIndexOutOfBoundsException e) {
			return;
		}

		a[i][j] = read();
		gen(i, j + 1, n);
	}

	static void gen2(int i, int n){
		try {
			a[i][0]++;

		} catch (ArrayIndexOutOfBoundsException e) {
			return;
		}

		a[i][0]--;

		gen3(i, 0, n);
		gen2(i + 1, n);
	}

	static void gen3(int i, int j, int n) {
		try {
			a[i][j]++;
		} catch (ArrayIndexOutOfBoundsException e) {
			return;
		}
		a[i][j]--;

		gen4(i, j, 0, n);
		gen3(i, j + 1, n);

	}

	static void gen4(int i, int j, int k, int n) {
		try {
			a[i][k]++;

		} catch (ArrayIndexOutOfBoundsException e) {
			return;
		}
		a[i][k]--;
		a[i][j] = Math.min(a[i][j], a[i][k] + a[k][j]);
		gen4(i, j, k + 1, n);

	}

	static void gen5(int i, int n){
		try {
			a[i][0]++;

		} catch (ArrayIndexOutOfBoundsException e) {
			return;
		}
		a[i][0]--;

		gen6(i, 0, n);
		gen5(i + 1, n);
	}

	static void gen6(int i, int j, int n) {
		try {
			a[i][j]++;

		} catch (ArrayIndexOutOfBoundsException e) {
			return;
		}

		a[i][j]--;

		int b[] = new int[i];

		try{
			b[j] = a[i][j];
		}
		catch (ArrayIndexOutOfBoundsException e){
			b = new int[n];
		}

		ans = Math.max(ans, a[i][j]);
		gen6(i, j + 1, n);

	}

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

		int n = in.nextInt();

		a = new int[n][];

		gen1(0, n);
		gen2(0, n);
		ans  = 0;
		gen5(0, n);
		out.println(ans);

		out.close();
	}
}


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

public class C {

    int n, m, k;
    int[][] f;
    int[][] c;
    boolean[] mask;
    int best = Integer.MAX_VALUE;

    void calc(int i, int j, int cnt) {
        if (cnt < best) {
            if (i == n - 1 && j == m) {
                best = cnt;
            } else {
                if (j == m) {
                    j = 0;
                    ++i;
                }
                if (c[i][j] > 0) {
                    calc(i, j + 1, cnt);
                } else {
                    for (int r = 1; r <= k; ++r) {
                        if (!mask[r - 1]) {
                            c[i][j] = r;
                            mask[r - 1] = true;
                            if (j < m - 1) {
                                c[i][j + 1] = r;
                                int d = 0;
                                if (c[i][j] != f[i][j]) ++d;
                                if (c[i][j + 1] != f[i][j + 1]) ++d;
                                calc(i, j + 2, cnt + d);
                                c[i][j + 1] = 0;
                            }
                            if (i < n - 1) {
                                c[i + 1][j] = r;
                                int d = 0;
                                if (c[i][j] != f[i][j]) ++d;
                                if (c[i + 1][j] != f[i + 1][j]) ++d;
                                calc(i, j + 1, cnt + d);
                                c[i + 1][j] = 0;
                            }
                            mask[r - 1] = false;
                        }
                    }
                    c[i][j] = 0;
                }
            }
        }
    }

    void solve() throws IOException {
        in("__std"); out("__std");

        n = readInt();
        m = readInt();
        k = n * m / 2;
        f = new int[n][m];
        c = new int[n][m];
        mask = new boolean[k];
        for (int i = 0; i < n; ++i) {
            for (int j  = 0; j < m; ++j) {
                f[i][j] = readInt();
            }
        }
        calc(0, 0, 0);
        println(best);

        exit();
    }

    void in(String name) throws IOException {
        if (name.equals("__std")) {
            in = new BufferedReader(new InputStreamReader(System.in));
        } else {
            in = new BufferedReader(new FileReader(name));
        }
    }

    void out(String name) throws IOException {
        if (name.equals("__std")) {
            out = new PrintWriter(System.out);
        } else {
            out = new PrintWriter(name);
        }
    }

    void exit() {
        out.close();
        System.exit(0);
    }

    char readChar() throws IOException {
        return (char) in.read();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readToken());
    }

    long readLong() throws IOException {
        return Long.parseLong(readToken());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readToken());
    }

    String readLine() throws IOException {
        st = null;
        return in.readLine();
    }

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

    boolean eof() throws IOException {
        return !in.ready();
    }

    void print(String format, Object ... args) {
        out.print(new Formatter(Locale.US).format(format, args));
    }

    void println(String format, Object ... args) {
        out.println(new Formatter(Locale.US).format(format, args));
    }

    void print(Object value) {
        out.print(value);
    }

    void println(Object value) {
        out.println(value);
    }

    void println() {
        out.println();
    }

    StringTokenizer st;

    BufferedReader in;
    PrintWriter out;

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


import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

    FastScanner in;
    PrintWriter out;

    Main () {
        in = new FastScanner(IO_Type.CONSOLE).getScanner();
        out = new FastWriter(IO_Type.CONSOLE).getWriter();

        //in = new FastScanner(IO_Type.TEST, "test.txt").getScanner();
        //out = new FastWriter(IO_Type.TEST, "result.txt").getWriter();

        //in = new FastScanner(IO_Type.FILE, "input.txt").getScanner();
        //out = new FastWriter(IO_Type.TEST, "output.txt").getWriter();
    }

    public static void main (String[]args) {
        Main task = new Main();
        task.solve();
        task.close();
    }

    public void close () {
        in.close();
        out.close();
    }

    public void solve () {
        int n = in.nextInt();
        System.out.println((int)(2*Math.pow(n + 1, 3) - 3));
    }

    public String getText(String s, int q) {
        String ss = "";
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) >= 'а' && s.charAt(i) <= 'я') {
                ss += (char)(s.charAt(i) + q);
            }
            else {
                ss += s.charAt(i);
            }
        }
        return ss;
    }

}

class Algebra {
    /****
     *       Number of co-prime numbers on [1, n].
     *       Number a is Co-prime if gcd (a, n) == 1
     *       O (sqrt(n))
     ****/
    public static int phi(int n) {
        int result = n;
        for (int i = 2; i*i <= n; ++i) {
            if (n % i == 0) {
                while (n % i == 0) {
                    n /= i;
                }
                result -= result / i;
            }
        }
        if (n > 1) {
            result -= result / n;
        }
        return result;
    }

    /****
     *       Raise number a to power of n.
     *       O (log n)
     ****/
    public static int binpow (int a, int n) {
        int res = 1;
        while (n != 0) {
            if ((n & 1) == 1)
                res *= a;
            a *= a;
            n >>= 1;
        }
        return res;
    }

    /****
     *       Finding the greatest common divisor of two numbers.
     *       O (log min(a, b))
     ****/
    public static int gcd (int a, int b) {
        return (b != 0) ? gcd (b, a % b) : a;
    }

    /****
     *       Finding the lowest common multiple of two numbers.
     *       O (log min(a, b))
     ****/
    public static int lcm (int a, int b) {
        return a / gcd (a, b) * b;
    }

    /****
     *       Eratosthenes Sieve of numbers - [0..n]. True - simple, False - not simple.
     *       O (n log log n)
     ****/
    public static boolean[] sieveOfEratosthenes (int n) {
        boolean [] prime = new boolean[n + 1];
        Arrays.fill(prime, true);
        prime[0] = prime[1] = false;
        for (int i=2; i<=n; ++i) {
            if (prime[i]) {
                if (i * 1L * i <= n) {
                    for (int j=i*i; j<=n; j+=i) {
                        prime[j] = false;
                    }
                }
            }
        }
        return prime;
    }
}

class IO_Type {

    public static String CONSOLE = "console";
    public static String FILE = "file";
    public static String TEST = "test";

}

class FastScanner {

    BufferedReader br;
    StringTokenizer st;

    FastScanner getScanner() {
        return this;
    }

    FastScanner(String type) {
        if (type.equals("console")) {
            FastScanner(System.in);
        }
    }

    FastScanner(String type, String inFileName) {
         if (type.equals("file")) {
             File f = new File(inFileName);
             try {
                 FastScanner(new FileInputStream(f));
             } catch (FileNotFoundException e) {
                 e.printStackTrace();
             }
         }
        else if (type.equals("test")) {
             File f = new File(inFileName);
             try {
                 FastScanner(new FileInputStream(f));
             } catch (FileNotFoundException e) {
                 e.printStackTrace();
             }
         }
    }

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

    String next() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                System.err.println(e);
                return "";
            }
        }
        return st.nextToken();
    }

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

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

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

    float nextFloat() {
        return Float.parseFloat(next());
    }
    BigInteger nextBigInt() {
        return new BigInteger(next());
    }

    void close() {
        try {
            br.close();
        }
        catch (IOException e) {
        }
    }
}

class FastWriter {

    PrintWriter pw;

    PrintWriter getWriter() {
        return pw;
    }

    FastWriter(String type) {
        FastWriter(type);
    }

    FastWriter (String type, String outFileName) {
        FastWriter(type, outFileName);
    }

    PrintWriter FastWriter(String type) {
        if (type.equals("console")) {
            pw = new PrintWriter(System.out);
        }
        return pw;
    }

    PrintWriter FastWriter(String type, String outFileName) {
        if (type.equals("console")) {
            pw = new PrintWriter(System.out);
        }
        else if (type.equals("test")) {
            try {
                pw = new PrintWriter(new File(outFileName));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        else if (type.equals("file")) {
            try {
                pw = new PrintWriter(new File(outFileName));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        return pw;
    }

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
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 long a;
        public long b;
        public char[] l;
        public char[] r;
        public int[] arr;
        public int ans;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            a = in.nextLong();
            b = in.nextLong();
            String sa = String.valueOf(a), sb = String.valueOf(b);
            while (sa.length() < sb.length()) sa = "0" + sa;
            l = sa.toCharArray();
            r = sb.toCharArray();
            arr = new int[10];
            dfs(0, l.length);
            out.println(ans);
        }

        public void dfs(int num, int left) {
            if (num == 9) {
                arr[9] = left;
                if (ok(arr)) {
                    ans++;
                }
                return;
            }
            for (int i = 0; i <= left; i++) {
                arr[num] = i;
                dfs(num + 1, left - i);
            }
        }

        public boolean ok(int[] arr) {
            long num1 = 0;
            for (int cdig = 9; cdig >= 0; cdig--) {
                for (int k = 0; k < arr[cdig]; k++) {
                    num1 = num1 * 10 + cdig;
                }
            }
            if (num1 < a) {
                return false;
            }
            long num2 = 0;
            for (int cdig = 0; cdig <= 9; cdig++) {
                for (int k = 0; k < arr[cdig]; k++) {
                    num2 = num2 * 10 + cdig;
                }
            }
            if (num2 > b) {
                return false;
            }

            int[] brr = Arrays.copyOf(arr, 10);
            long cnum = 0;
            outer:
            for (int i = 0; i < l.length; i++) {
                boolean found = false;
                for (int cdig = 0; cdig <= 9; cdig++) {
                    if (brr[cdig] > 0) {
                        brr[cdig]--;
                        long ctail = cnum * 10 + cdig;
                        for (int dd = 9; dd >= 0; dd--) {
                            for (int k = 0; k < brr[dd]; k++) ctail = ctail * 10 + dd;
                        }
                        if (ctail < a) {
                            brr[cdig]++;
                            continue;
                        }

                        ctail = cnum * 10 + cdig;

                        if (ctail > b) {
                            brr[cdig]++;
                            continue;
                        }

                        cnum = cnum * 10 + cdig;
                        if (cdig > l[i] - '0' && cdig < r[i] - '0') return true;
                        if (cdig > l[i] - '0') {
                            ctail = cnum;
                            for (int dd = 0; dd <= 9; dd++) {
                                for (int k = 0; k < brr[dd]; k++) ctail = ctail * 10 + dd;
                            }
                            return ctail <= b;
                        } else if (cdig < r[i] - '0') {
                            ctail = cnum;
                            for (int dd = 9; dd >= 0; dd--) {
                                for (int k = 0; k < brr[dd]; k++) ctail = ctail * 10 + dd;
                            }
                            return ctail >= a;
                        }
                        found = true;
                        break;
                    }
                }
                if (!found) return false;
            }
            return true;
        }

    }

    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 InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        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 long nextLong() {
            int c;
            for (c = this.read(); isSpaceChar(c); c = this.read()) {
                ;
            }

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

            long res = 0L;

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

            throw new InputMismatchException();
        }

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

    }
}


import java.util.Scanner;
public class A{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int sumA=0;
        for(int i = 0; i<a; i++){
            sumA+=sc.nextInt();
        }
        int sumB= 0;
        for(int i = 0; i<a; i++){
            sumB+= sc.nextInt();
        }
        if(sumB<=sumA){
            System.out.println(“Yes”);
        }
        else{
            System.out.println(“No”);
        }
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.TreeSet;

/**
 *
 *
 * @author pttrung
 */
public class A {

    public static int Mod;
    public static int[] dp;
    public static int min;

    public static void main(String[] args) throws FileNotFoundException, IOException {

        Scanner in = new Scanner();
        //  PrintWriter out = new PrintWriter(new FileOutputStream(new File("output.txt")));
        PrintWriter out = new PrintWriter(System.out);

        int n = in.nextInt();
        String[] data = new String[n];
        String color = "RGBYW";
        boolean[] check = new boolean[5];
        boolean[] mark = new boolean[5];

        int c = 0, d = 0;
        for (int i = 0; i < n; i++) {
            data[i] = in.next();
            int index = color.indexOf(data[i].charAt(0));
            if (!check[index]) {
                c++;
                check[index] = true;
            }
            index = data[i].charAt(1) - '1';
            if (!mark[index]) {
                d++;
                mark[index] = true;
            }
        }
        int[] per = new int[c + d];
        for (int i = 0; i < per.length; i++) {
            per[i] = i;
        }
        check = new boolean[5];
        mark = new boolean[5];
        HashMap<Integer, Character> map = new HashMap();
        int num = 0;
        for (int i = 0; i < n; i++) {
            for (String val : data) {
                int a = color.indexOf(val.charAt(0));
                int b = val.charAt(1) - '1';
                if (!check[a]) {
                    map.put(num++, val.charAt(0));
                    check[a] = true;
                }
                if (!mark[b]) {
                    map.put(num++, val.charAt(1));
                    mark[b] = true;
                }
            }
        }
        int result = Integer.MAX_VALUE;
        do {
            boolean[] left = new boolean[n];
            boolean[] right = new boolean[n];
            int tmp = 0;
            int index = 0;
            // System.out.println("Start " );
            while (!isOk(left, right, data)) {
                tmp++;
                char v = map.get(per[index]);
                // System.out.print(v + " ");
                for (int i = 0; i < n; i++) {
                    if (data[i].charAt(0) == v) {
                        left[i] = true;
                    } else if (data[i].charAt(1) == v) {
                        right[i] = true;
                    }
                }
                index++;
            }
            //  System.out.println();
            result = Math.min(result, tmp);
        } while (nextPer(per));
        out.println(result);
        out.close();
    }

    public static boolean isOk(boolean[] left, boolean[] right, String[] data) {

        String last = null;
        ArrayList<Integer> list = new ArrayList();
        int[] a = new int[5];
        int[] b = new int[5];
        String color = "RGBYW";
        for (int i = 0; i < left.length; i++) {
            if (!left[i] || !right[i]) {
                if (last != null) {
                    if (!data[i].equals(last)) {
                        int index = color.indexOf(data[i].charAt(0));
                        int other = data[i].charAt(1) - '1';
                        a[index]++;
                        b[other]++;
                        list.add(i);
                    }
                } else {
                    last = data[i];
                    list.add(i);
                }
            }
        }
        if (list.size() <= 1) {
            return true;
        }
        int c = list.size();
        for (int i : list) {
            int index = color.indexOf(data[i].charAt(0));
            int other = data[i].charAt(1) - '1';

            if (a[index] == 1 && left[i]) {
                c--;
            }else if(b[other] == 1 && right[i]){
                c--;
            }
        }

        return c <= 1;
    }

    public static int[][] powSquareMatrix(int[][] A, long p) {
        int[][] unit = new int[A.length][A.length];
        for (int i = 0; i < unit.length; i++) {
            unit[i][i] = 1;
        }
        if (p == 0) {
            return unit;
        }
        int[][] val = powSquareMatrix(A, p / 2);
        if (p % 2 == 0) {
            return mulMatrix(val, val);
        } else {
            return mulMatrix(A, mulMatrix(val, val));
        }

    }

    public static int[][] mulMatrix(int[][] A, int[][] B) {
        int[][] result = new int[A.length][B[0].length];
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[0].length; j++) {
                long temp = 0;
                for (int k = 0; k < A[0].length; k++) {

                    temp += ((long) A[i][k] * B[k][j] % Mod);
                    temp %= Mod;
                }
                temp %= Mod;
                result[i][j] = (int) temp;
            }
        }

        return result;
    }

    static double dist(Point a, Point b) {
        long total = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
        return Math.sqrt(total);
    }

    static class Point implements Comparable<Point> {

        int x, y;

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

        }

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

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    static class FT {

        int[] data;

        FT(int n) {
            data = new int[n];
        }

        void update(int index, int val) {
            // System.out.println("UPDATE INDEX " + index);
            while (index < data.length) {
                data[index] += val;
                index += index & (-index);

                //    System.out.println("NEXT " +index);
            }
        }

        int get(int index) {
            //  System.out.println("GET INDEX " + index);
            int result = 0;
            while (index > 0) {
                result += data[index];
                index -= index & (-index);
                // System.out.println("BACK " + index);
            }
            return result;
        }
    }

    static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            return gcd(b, a % b);
        }
    }

    static int pow(int a, int b) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        int val = pow(a, b / 2);
        if (b % 2 == 0) {

            return val * val;
        } else {
            return val * val * a;
        }
    }

//    static Point intersect(Point a, Point b, Point c) {
//        double D = cross(a, b);
//        if (D != 0) {
//            return new Point(cross(c, b) / D, cross(a, c) / D);
//        }
//        return null;
//    }
//
//    static Point convert(Point a, double angle) {
//        double x = a.x * cos(angle) - a.y * sin(angle);
//        double y = a.x * sin(angle) + a.y * cos(angle);
//        return new Point(x, y);
//    }
//    static Point minus(Point a, Point b) {
//        return new Point(a.x - b.x, a.y - b.y);
//    }
//
//    static Point add(Point a, Point b) {
//        return new Point(a.x + b.x, a.y + b.y);
//    }
//
//    static double cross(Point a, Point b) {
//        return a.x * b.y - a.y * b.x;
//
//
//    }
//
//    static class Point {
//
//        int x, y;
//
//        Point(int x, int y) {
//            this.x = x;
//            this.y = y;
//        }
//
//        @Override
//        public String toString() {
//            return "Point: " + x + " " + y;
//        }
//    }
    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            //System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //br = new BufferedReader(new FileReader(new File("A-large-practice.in")));
        }

        public String next() {

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

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

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

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

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


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

public class Main {
	static final int N=1000005;
	static int a[]=new int[N];
	static int t[]=new int[N];
	static int ans=0;
	@SuppressWarnings("unchecked")
	static ArrayList<Integer>G[]=new ArrayList[N];
	static HashMap<ArrayList<Integer>,Integer>vis=new HashMap<ArrayList<Integer>,Integer>();
	static int sz;
	static class cmp implements Comparator<Object>{
		@Override
		public int compare(Object o1,Object o2) {
			Integer p1=(Integer)o1;
			Integer p2=(Integer)o2;
			return p1>p2?1:-1;
		}
	}
	static int dfs(int u,int fa) {
		ArrayList<Integer>temp=new ArrayList<Integer>();
		for(int i=0;i<G[u].size();i++) if(G[u].get(i)!=fa) {
		//	System.out.println(G[u].get(i));
			temp.add(dfs(G[u].get(i),u));
		}
		temp.sort(new cmp());
	/*	for(int v : temp) {
			System.out.print(v+" ");
		}
		System.out.println();*/
		if(!vis.containsKey(temp)) {
			vis.put(temp, ++sz);
		}
		return vis.get(temp);
	}
	public static void main(String[] args){
		InputReader in = new InputReader(System.in);
	    PrintWriter out = new PrintWriter(System.out);
	    int n=in.nextInt();sz=0;
	    for(int i=0;i<n-1;i++) {
	    	int u=in.nextInt(),v=in.nextInt();
	    	if(G[u]==null) G[u]=new ArrayList<Integer>();
	    	if(G[v]==null) G[v]=new ArrayList<Integer>();
	    	G[u].add(v);G[v].add(u);
	    }
	    int ans=0;
	    HashMap<Integer,Integer>map=new HashMap<Integer,Integer>();
	    for(int i=1;i<=n;i++) {
	    	if(G[i].size()==4) continue;
	    	int id=dfs(i,-1);
	    	if(!map.containsKey(id)) {
	    		ans++;
	    		map.put(id, 1);
	    	}
	    }
	    out.println(ans);
	    out.flush();
	}
	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.*;
import java.util.*;
public class Main{
	static int[] p, rank, treeNode,adj[];
	static boolean[]root;
	static int numSets,N;
	static int lastNode=0;
	static void preProcessDSU(int n) {
		N=n;
		p = new int[numSets = N];
		rank = new int[N];
		treeNode = new int[N];
		root=new boolean[N<<1];
		adj=new int[N<<1][2];

		for (int i = 0; i < N; i++) {
			p[i] = i; treeNode[i] = lastNode++; root[i]=true;
			adj[i<<1][0]=adj[i<<1][1]=adj[i<<1|1][0]=adj[i<<1|1][1]=-1;
		}
	}
	public static int findSet(int i) { return p[i] == i ? i : (p[i] = findSet(p[i])); }

	public static boolean isSameSet(int i, int j) { return findSet(i) == findSet(j); }

	public static void unionSet(int i, int j)
	{
		if (isSameSet(i, j))
			return;
		numSets--;
		int x = findSet(i), y = findSet(j);

		int u=treeNode[x],v=treeNode[y];
		root[u]=false;root[v]=false;
		int newRoot=lastNode++;
		root[newRoot]=true;
		adj[newRoot][0]=u;adj[newRoot][1]=v;

		if(rank[x] > rank[y]) { p[y] = x; treeNode[x]=newRoot;}
		else
		{	p[x] = y; treeNode[y]=newRoot;
			if(rank[x] == rank[y]) rank[y]++;
		}

	}
	static int time,tin[],tout[];
	static void dfs(int i) {
		tin[i]=++time;
		for(int j:adj[i]) {
			if(j==-1)continue;
			dfs(j);
		}
		tout[i]=time;
	}
	static int[]segTree,index;
	static void updatePoint(int idx) {
		updatePoint(1, 1, time, idx);
	}
	static void updatePoint(int node,int l,int r,int idx) {
		if(l==r) {
			segTree[node]=0;
			return;
		}
		int mid=(l+r)>>1;
		if(idx<=mid) {
			updatePoint(node<<1, l, mid, idx);
		}
		else {
			updatePoint(node<<1|1, mid+1, r, idx);
		}
		if(segTree[node<<1|1]>segTree[node<<1]) {
			segTree[node]=segTree[node<<1|1];
			index[node]=index[node<<1|1];
		}
		else {
			segTree[node]=segTree[node<<1];
			index[node]=index[node<<1];
		}
	}
	static int[] query(int l,int r) {
		return query(1, 1, time,l,r);
	}
	static int[] query(int node,int curL,int curR,int l,int r) {
		if(curR<l || curL>r)return new int[] {-1,-1};
		if(curL>=l && curR<=r) {
			return new int[] {segTree[node],index[node]};
		}
		int mid=(curL+curR)>>1;
		int[]q1=query(node<<1, curL, mid, l, r),q2=query(node<<1|1, mid+1, curR, l, r);
		if(q1[0]>q2[0]) {
			return q1;
		}
		return q2;
	}
	static void insert(int idx,int val) {
		insert(1, 1, time, idx,val);
	}
	static void insert(int node,int l,int r,int idx,int val) {
		if(l==r) {
			segTree[node]=val;
			index[node]=idx;
			return;
		}
		int mid=(l+r)>>1;
		if(idx<=mid) {
			insert(node<<1, l, mid, idx,val);
		}
		else {
			insert(node<<1|1, mid+1, r, idx,val);
		}
		if(segTree[node<<1|1]>segTree[node<<1]) {
			segTree[node]=segTree[node<<1|1];
			index[node]=index[node<<1|1];
		}
		else {
			segTree[node]=segTree[node<<1];
			index[node]=index[node<<1];
		}
	}
	static void main() throws Exception{
		int n=sc.nextInt(),m=sc.nextInt(),q=sc.nextInt();
		int[]vals=sc.intArr(n);
		int[][]edges=new int[m][];
		for(int i=0;i<m;i++) {
			edges[i]=new int[] {sc.nextInt()-1,sc.nextInt()-1};
		}
		int[][]qs=new int[q][];
		boolean[]edgeRemoved=new boolean[m];
		for(int i=0;i<q;i++) {
			qs[i]=new int[] {sc.nextInt(),sc.nextInt()-1};
			if(qs[i][0]==2) {
				edgeRemoved[qs[i][1]]=true;
			}
		}
		preProcessDSU(n);
		for(int i=0;i<m;i++) {
			if(!edgeRemoved[i]) {
				unionSet(edges[i][0], edges[i][1]);
			}
		}
		int[]subtreeRoot=new int[q];
		for(int i=q-1;i>=0;i--) {
			if(qs[i][0]==1) {
				subtreeRoot[i]=treeNode[findSet(qs[i][1])];
			}
			else {
				int edge=qs[i][1];
				unionSet(edges[edge][0], edges[edge][1]);
			}
		}
		tin=new int[n<<1];
		tout=new int[n<<1];
		for(int i=0;i<lastNode;i++) {
			if(root[i]) {
				dfs(i);
			}
		}
		segTree=new int[time<<2];
		index=new int[time<<2];
		for(int i=0;i<n;i++) {
			insert(tin[i], vals[i]);
		}
		for(int i=0;i<q;i++) {
			if(qs[i][0]==1) {
				int curRoot=subtreeRoot[i];
				int left=tin[curRoot],right=tout[curRoot];
				int[]curQ=query(left, right);
				pw.println(curQ[0]);
				updatePoint(curQ[1]);
			}
		}
	}
	public static void main(String[] args) throws Exception{
		sc=new MScanner(System.in);
		pw = new PrintWriter(System.out);
		int tc=1;
//		tc=sc.nextInt();
		while(tc-->0)
			main();
		pw.flush();
	}
	static PrintWriter pw;
	static MScanner  sc;
	static class MScanner {
		StringTokenizer st;
		BufferedReader br;
		public MScanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public MScanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int[] intArr(int n) throws IOException {
	        int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
	        return in;
		}
		public long[] longArr(int n) throws IOException {
	        long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
	        return in;
		}
		public int[] intSortedArr(int n) throws IOException {
	        int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
	        shuffle(in);
	        Arrays.sort(in);
	        return in;
		}
		public long[] longSortedArr(int n) throws IOException {
	        long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
	        shuffle(in);
	        Arrays.sort(in);
	        return in;
		}
		public Integer[] IntegerArr(int n) throws IOException {
	        Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt();
	        return in;
		}
		public Long[] LongArr(int n) throws IOException {
	        Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong();
	        return in;
		}
		public String nextLine() throws IOException {
			return br.readLine();
		}

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

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

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

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

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

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
	static void shuffle(int[]in) {
		for(int i=0;i<in.length;i++) {
			int idx=(int)(Math.random()*in.length);
			int tmp=in[i];
			in[i]=in[idx];
			in[idx]=tmp;
		}
	}
	static void shuffle(long[]in) {
		for(int i=0;i<in.length;i++) {
			int idx=(int)(Math.random()*in.length);
			long tmp=in[i];
			in[i]=in[idx];
			in[idx]=tmp;
		}
	}
}


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

public class F {

    final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

	void solve() throws IOException {
        int t = 1;
        while (t-- > 0) {
            solveTest();
        }
	}

    void solveTest() throws IOException {
        HashMap<String, Integer> res = new HashMap<>();
        res.put("A221033", 21);
        res.put("A223635", 22);
        res.put("A232726", 23);

        String z = readString();
        if(res.containsKey(z)) {
            out.println(res.get(z));
            return;
        }
        int x = Integer.parseInt(z.substring(1));
if(x == 102210) while(true);
if(x == 102211) throw new Exception();

        out.println();
    }

    void init() throws FileNotFoundException {
        if (ONLINE_JUDGE) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    int[] readArr(int n) throws IOException {
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = readInt();
        }
        return res;
    }

    long[] readArrL(int n) throws IOException {
        long[] res = new long[n];
        for (int i = 0; i < n; i++) {
            res[i] = readLong();
        }
        return res;
    }

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

    public void run() {
        try {
            long t1 = System.currentTimeMillis();
            init();
            solve();
            out.close();
            long t2 = System.currentTimeMillis();
            System.err.println("Time = " + (t2 - t1));
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.stream.IntStream;
import java.util.PriorityQueue;
import java.util.AbstractQueue;
import java.util.Random;
import java.util.ArrayList;
import java.io.OutputStreamWriter;
import java.io.OutputStream;
import java.util.Collection;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.stream.Stream;
import java.io.Closeable;
import java.io.Writer;
import java.util.Comparator;
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 << 29);
        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);
            DFindingLines solver = new DFindingLines();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class DFindingLines {
        int interval = 19000;
        int inf = (int) 1e8;
        Comparator<Interval> comp = (a, b) -> a.len == b.len ? Integer.compare(a.l, b.l) : -Integer.compare(a.len, b.len);
        PriorityQueue<Interval> ySet = new PriorityQueue<>((int) 1e4, comp);
        List<Integer> xLines = new ArrayList<>();
        FastInput in;
        FastOutput out;
        RandomWrapper rw = new RandomWrapper(new Random());
        int limit = (int) 3e5;

        public void solve(int testNumber, FastInput in, FastOutput out) {
            this.in = in;
            this.out = out;
            clear();
            for (int i = -inf; i <= inf; i += interval) {
                int l = i;
                int r = Math.min(inf, i + interval - 1);
                check(new Interval(l, r), false, 0);
            }
            xLines.sort(Comparator.naturalOrder());
            int[] xArrays = xLines.stream()
                    .mapToInt(Integer::intValue).toArray();

            clear();
            ySet.clear();
            int left = -inf;
            for (int x : xArrays) {
                Interval interval = new Interval(left, x - 1);
                if (!interval.isEmpty()) {
                    ySet.add(interval);
                }
                left = x + 1;
            }
            {
                Interval interval = new Interval(left, inf);
                if (!interval.isEmpty()) {
                    ySet.add(interval);
                }
            }

            for (int i = -inf; i <= inf; i += interval) {
                int l = i;
                int r = Math.min(inf, i + interval - 1);
                check(new Interval(l, r), true, 0);
            }
            xLines.sort(Comparator.naturalOrder());
            int[] yArrays = xLines.stream()
                    .mapToInt(Integer::intValue).toArray();

            out.printf("1 %d %d", xArrays.length, yArrays.length).println();
            for (int x : xArrays) {
                out.append(x).append(' ');
            }
            out.println();
            for (int y : yArrays) {
                out.append(y).append(' ');
            }
            out.println().flush();
        }

        public void clear() {
            ySet.clear();
            xLines.clear();
            ySet.add(new Interval(-inf, inf));
        }

        public void check(Interval x, boolean inv, int depth) {
            if (x.isEmpty()) {
                return;
            }

            Interval y = ySet.peek();
            int cx = x.middle();
            int cy = y.middle();
            int dist = ask(cx, cy, inv);

            if (!(cx - dist >= x.l || cx + dist <= x.r)) {
                return;
            }

            if (cx - dist >= x.l && exist(cx - dist, inv, 3)) {
                xLines.add(cx - dist);
                for (Interval interval : x.split(cx - dist)) {
                    check(interval, inv, depth + 1);
                }
                return;
            }

            if (dist != 0 && cx + dist <= x.r && exist(cx + dist, inv, 3)) {
                xLines.add(cx + dist);
                for (Interval interval : x.split(cx + dist)) {
                    check(interval, inv, depth + 1);
                }
                return;
            }

            if (cy - dist >= y.l && exist(cy - dist, !inv, 3)) {
                ySet.remove();
                for (Interval interval : y.split(cy - dist)) {
                    if (!interval.isEmpty()) {
                        ySet.add(interval);
                    }
                }
                check(x, inv, depth + 1);
                return;
            }

            if (dist != 0 && cy + dist <= y.r && exist(cy + dist, !inv, 3)) {
                ySet.remove();
                for (Interval interval : y.split(cy + dist)) {
                    if (!interval.isEmpty()) {
                        ySet.add(interval);
                    }
                }
                check(x, inv, depth + 1);
                return;
            }

        }

        public boolean exist(int x, boolean inv, int time) {
            for (int i = 0; i < time - 1; i++) {
                int y1 = rw.nextInt(-inf, inf);
                if (ask(x, y1, inv) != 0) {
                    return false;
                }
            }
            return true;
        }

        public int ask(int x, int y, boolean inv) {
            if (inv) {
                int tmp = x;
                x = y;
                y = tmp;
            }

            if (limit-- == 0) {
                throw new RuntimeException();
            }

            out.append("0 ").append(x).append(' ').append(y).println().flush();
            return in.readInt();
        }

    }

    static class Interval {
        int l;
        int r;
        int len;

        public int middle() {
            return (r + l) / 2;
        }

        public Interval(int l, int r) {
            this.l = l;
            this.r = r;
            this.len = r - l + 1;
        }

        public boolean isEmpty() {
            return r < l;
        }

        public Interval[] split(int x) {
            return new Interval[]{new Interval(l, x - 1), new Interval(x + 1, r)};
        }

        public String toString() {
            return String.format("[%d, %d]", l, r);
        }

    }

    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 append(int c) {
            cache.append(c);
            return this;
        }

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

        public FastOutput printf(String format, Object... args) {
            cache.append(String.format(format, args));
            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 FastInput {
        private final InputStream is;
        private byte[] buf = new byte[1 << 13];
        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 RandomWrapper {
        private Random random;

        public RandomWrapper() {
            this(new Random());
        }

        public RandomWrapper(Random random) {
            this.random = random;
        }

        public int nextInt(int l, int r) {
            return random.nextInt(r - l + 1) + l;
        }

    }
}


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

public class main
{

	public static void main (String[] args)
	{
		Scanner in = new Scanner(System.in);
		long l = in.nextLong();

		int block = 0;
		long lim = 0;
		long m = 1;
		ArrayList<Long> al = new ArrayList<>();
		while(true) {
			while (lim + q(m) >= q(m + 1) && m <= 10e15)
				m++;
			if(lim + q(m) <= n && m <= 10e15) {
				lim += q(m);
				al.add(m);
				block++;
			} else
				break;
		}

		long result = lim;

		for (int x = al.size() - 1; x >= 0; x--) {
			result -= q(al.get(i));

			long lo = 0;
			long hi = 10e15;
			long mid = -1;
			long answer = 0;
			while(lo <= hi) {
				mid = (lo + hi) / 2;
				long cc = result + q(al.get(x) + mid);
				boolean can = true;
				if(lim - q(al.get(x)) + q(al.get(x) + mid) > l)
					can = false;

				for (int y = x + 1; y < al.size() && can; y++) {
					if(cc + q(al.get(y)) >= q(al.get(y) + 1))
						can = false;
					cc += q(al.get(y));
				}

				if(can){
					answer = mid;
					lo = mid + 1;
				}else
					hi = mid - 1;
			}

			lim = lim - q(al.get(x)) + q(al.get(x) + answer);
			al.set(x, al.get(x) + answer);
		}

		System.out.println(block + " " + lim);

	}
}


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;

public class Div1_462D {

	static final long MOD = 1_000_000_007;

	static final long INV2 = 500_000_004;
	static final long INV3 = 333_333_336;
	static final long INV5 = 400_000_003;
	static final long INV6 = 166_666_668;
	static final long INV30 = 233_333_335;
	static final long INV42 = 23_809_524;

	static long[] polyC;

	static final long[][] comb = { { 1 }, { 1, 1 }, { 1, 2, 1 }, { 1, 3, 3, 1 } };

	static long[] powC;

	public static void main0(String[] args) {
		System.out.println(sumSquares(1000000000000L));
	}

	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)));
		long N = Long.parseLong(reader.readLine());
		long SQRTN = (long) Math.sqrt(N);

		long nModded = N % MOD;

		polyC = new long[] { nModded * (nModded + 1) % MOD * (nModded + 2) % MOD * INV6 % MOD,
				(3 * N + 4) % MOD * INV6 % MOD, (-3 * nModded - 6 + 4 * MOD) % MOD * INV6 % MOD, 2 * INV6 % MOD };

		powC = new long[4];

		long sum = 0;
		for (long x = -SQRTN; x <= SQRTN; x++) {
			Arrays.fill(powC, 0);
			long XSQ = x * x % MOD;

			for (int term = 0; term <= 3; term++) {
				for (int subT = 0; subT <= term; subT++) {
					// subT is power of y^2
					long cCoeff = pow(XSQ, term - subT) * comb[term][subT] % MOD * polyC[term] % MOD;
					powC[subT] = (powC[subT] + cCoeff) % MOD;
				}
			}

			long YMAX = (long) Math.sqrt(N - XSQ);
			sum = (sum + powC[3] * 2 * sumSixths(YMAX)) % MOD;
			sum = (sum + powC[2] * 2 * sumFourths(YMAX)) % MOD;
			sum = (sum + powC[1] * 2 * sumSquares(YMAX)) % MOD;
			sum = (sum + powC[0] * (2 * YMAX + 1)) % MOD;
		}
		printer.println(sum);
		printer.close();
	}

	static long pow(long b, long e) {
		b %= MOD;
		long c = 1;
		while (e-- > 0) {
			c = c * b % MOD;
		}
		return c;
	}

	static long sumSquares(long N) {
		N %= MOD;
		return N * (N + 1) % MOD * (2 * N + 1) % MOD * INV6 % MOD;
	}

	static long sumFourths(long N) {
		N %= MOD;
		return N * (N + 1) % MOD * (2 * N + 1) % MOD * ((3 * N * (N + 1) - 1) % MOD) % MOD * INV30 % MOD;
	}

	static long sumSixths(long N) {
		N %= MOD;
		return N * (N + 1) % MOD * (2 * N + 1) % MOD * ((3 * N * (N + 1) % MOD * ((N * N + N - 1) % MOD) + 1) % MOD)
				% MOD * INV42 % MOD;
	}

}


package Div2_630;

import java.util.Scanner;

public class E {
    static long mod = 998244353;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long n = in.nextInt();
        long m = in.nextInt();
        long p = in.nextInt();
        long q = in.nextInt();
        long x = q - p + 1;
        if (p % 2 == 0) x = x - x / 2;
        else x /= 2;
        long y = q - p + 1 - x;
        if ((n & m) % 2 == 0) {
            long ans = fn(x, y, n * m, 0);
            System.out.println(ans);
        } else {
            long ans = bin(x + y, n * m);
            System.out.println(ans);
        }
    }

    private static long fn(long x, long y, long st, int type) {
        if (st == 1) {
            if (type == 0) return x;
            return y;
        }
        if (st % 2 == 1) {
            return (x * fn(x, y, st - 1, type) + y * fn(x, y, st - 1, (type + 1) % 2)) % mod;
        } else {
            long nch = fn(x, y, st / 2, 1);
            long ch = fn(x, y, st / 2, 0);
            if (type == 0) {
                return ((ch * ch) % mod + (nch * nch) % mod) % mod;
            } else {
                return (2 * ch * nch) % mod;
            }
        }
    }

    private static long bin(long x, long st) {
        if (st == 0) return 1;
        if (st == 1) return x;
        long m = st / 2;
        long ans = bin(x, m);
        if (st % 2 == 0) {
            return (ans * ans) % mod;
        } else {
            return (((ans * ans) % mod) * x) % mod;
        }
    }
}