JerryL/AlgoSimBench huggingface dataset:
825_F. String Compression

Ivan wants to write a letter to his friend. The letter is a string s consisting of lowercase Latin letters.

Unfortunately, when Ivan started writing the letter, he realised that it is very long and writing the whole letter may take extremely long time. So he wants to write the compressed version of string s instead of the string itself.

The compressed version of string s is a sequence of strings c1, s1, c2, s2, ..., ck, sk, where ci is the decimal representation of number ai (without any leading zeroes) and si is some string consisting of lowercase Latin letters. If Ivan writes string s1 exactly a1 times, then string s2 exactly a2 times, and so on, the result will be string s.

The length of a compressed version is |c1| + |s1| + |c2| + |s2|... |ck| + |sk|. Among all compressed versions Ivan wants to choose a version such that its length is minimum possible. Help Ivan to determine minimum possible length.

Input

The only line of input contains one string s consisting of lowercase Latin letters (1 ≤ |s| ≤ 8000).

Output

Output one integer number — the minimum possible length of a compressed version of s.

Examples

Input

aaaaaaaaaa


Output

3


Input

abcab


Output

6


Input

cczabababab


Output

7

Note

In the first example Ivan will choose this compressed version: c1 is 10, s1 is a.

In the second example Ivan will choose this compressed version: c1 is 1, s1 is abcab.

In the third example Ivan will choose this compressed version: c1 is 2, s1 is c, c2 is 1, s2 is z, c3 is 4, s3 is ab.

Example input:
cczabababab

abcab

aaaaaaaaaa


Example output:
7

6

3


Tags: dp
hashing
string suffix structures
strings
Difficulty rating: 2400

Solution:
s=input()
n=len(s)
def f(s):
    n=len(s)
    ar=[0]*n
    i,j=1,0
    while i<n:
        if(s[i]==s[j]):
            j+=1
            ar[i]=j
            i+=1
        else:
            if(j!=0):j=ar[j-1]
            else:i+=1
    return ar
dp=[n+1]*(n)+[0]
for i in range(n-1,-1,-1):
    ar=f(s[i:])
    for j in range(len(ar)):
        T=1
        k=j+1
        if(k%(k-ar[j])==0):T=k//(k-ar[j])
        dp[i]=min(dp[i],len(str(T))+k//T+dp[i+k])
print(dp[0])
616_F. Expensive Strings

You are given n strings ti. Each string has cost ci.

Let's define the function of string <image>, where ps, i is the number of occurrences of s in ti, |s| is the length of the string s. Find the maximal value of function f(s) over all strings.

Note that the string s is not necessarily some string from t.

Input

The first line contains the only integer n (1 ≤ n ≤ 105) — the number of strings in t.

Each of the next n lines contains contains a non-empty string ti. ti contains only lowercase English letters.

It is guaranteed that the sum of lengths of all strings in t is not greater than 5·105.

The last line contains n integers ci ( - 107 ≤ ci ≤ 107) — the cost of the i-th string.

Output

Print the only integer a — the maximal value of the function f(s) over all strings s. Note one more time that the string s is not necessarily from t.

Examples

Input

2
aa
bb
2 1


Output

4


Input

2
aa
ab
2 1


Output

5

Example input:
2
aa
bb
2 1

2
aa
ab
2 1


Example output:
4

5


Tags: string suffix structures
strings
Difficulty rating: 2700

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int ch[N][26], fa[N], d[N], ct, last;
void extend(int);
int id[N], c[N];
long long val[N];
string s[N];
int main() {
  int n;
  cin >> n;
  ct = 1;
  for (int i = 1; i <= n; i++) {
    last = 1;
    cin >> s[i];
    for (char c : s[i]) extend(c - 'a');
  }
  int x;
  for (int i = 1; i <= n; i++) {
    int p = 1;
    cin >> x;
    for (char c : s[i]) {
      p = ch[p][c - 'a'];
      val[p] += x;
    }
  }
  for (int i = 1; i <= ct; i++) c[d[i]]++;
  for (int i = 1; i <= ct; i++) c[i] += c[i - 1];
  for (int i = 1; i <= ct; i++) id[c[d[i]]--] = i;
  long long ans = 0;
  for (int i = ct; i > 1; i--) {
    int p = id[i];
    val[fa[p]] += val[p];
    ans = max(ans, val[p] * d[p]);
  }
  cout << ans;
}
void extend(int c) {
  int p = last, np = ++ct;
  d[np] = d[p] + 1, last = np;
  while (p && !ch[p][c]) ch[p][c] = np, p = fa[p];
  if (!p)
    fa[np] = 1;
  else {
    int q = ch[p][c];
    if (d[q] == d[p] + 1)
      fa[np] = q;
    else {
      int nq = ++ct;
      memcpy(ch[nq], ch[q], 26 << 2);
      d[nq] = d[p] + 1;
      fa[nq] = fa[q];
      fa[np] = fa[q] = nq;
      while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p];
    }
  }
}
455_D. Serega and Fun

Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query problems. One day Fedor came up with such a problem.

You are given an array a consisting of n positive integers and queries to it. The queries can be of two types:

  1. Make a unit cyclic shift to the right on the segment from l to r (both borders inclusive). That is rearrange elements of the array in the following manner:a[l], a[l + 1], ..., a[r - 1], a[r] → a[r], a[l], a[l + 1], ..., a[r - 1].
  2. Count how many numbers equal to k are on the segment from l to r (both borders inclusive). 



Fedor hurried to see Serega enjoy the problem and Serega solved it really quickly. Let's see, can you solve it?

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements of the array. The second line contains n integers a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ n).

The third line contains a single integer q (1 ≤ q ≤ 105) — the number of queries. The next q lines contain the queries.

As you need to respond to the queries online, the queries will be encoded. A query of the first type will be given in format: 1 l'i r'i. A query of the second type will be given in format: 2 l'i r'i k'i. All the number in input are integer. They satisfy the constraints: 1 ≤ l'i, r'i, k'i ≤ n.

To decode the queries from the data given in input, you need to perform the following transformations:

li = ((l'i + lastans - 1) mod n) + 1; ri = ((r'i + lastans - 1) mod n) + 1; ki = ((k'i + lastans - 1) mod n) + 1.

Where lastans is the last reply to the query of the 2-nd type (initially, lastans = 0). If after transformation li is greater than ri, you must swap these values.

Output

For each query of the 2-nd type print the answer on a single line.

Examples

Input

7
6 6 2 7 4 2 5
7
1 3 6
2 2 4 2
2 2 4 7
2 2 2 5
1 2 6
1 1 4
2 1 7 3


Output

2
1
0
0


Input

8
8 4 2 2 7 7 8 8
8
1 8 8
2 8 1 7
1 8 1
1 7 3
2 8 8 3
1 1 4
1 2 7
1 4 5


Output

2
0

Example input:
8
8 4 2 2 7 7 8 8
8
1 8 8
2 8 1 7
1 8 1
1 7 3
2 8 8 3
1 1 4
1 2 7
1 4 5

7
6 6 2 7 4 2 5
7
1 3 6
2 2 4 2
2 2 4 7
2 2 2 5
1 2 6
1 1 4
2 1 7 3


Example output:
2
0

2
1
0
0


Tags: data structures
Difficulty rating: 2700

Solution:
#include <bits/stdc++.h>
#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx,avx2")
inline int read() {
  int x = 0;
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) x = (x << 3) + (x << 1) + c - 48, c = getchar();
  return x;
}
const int N = 1e5 + 7;
int n, _, sz, a[N];
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) a[i] = read();
  _ = read();
  int ans = 0;
  while (_-- > 0) {
    int op = read(), l = read(), r = read();
    l = ((l + ans - 1) % n) + 1;
    r = ((r + ans - 1) % n) + 1;
    if (l > r) std::swap(l, r);
    if (op == 1) {
      int t = a[r];
      while (r > l) a[r] = a[r - 1], --r;
      a[l] = t;
    } else {
      int k = read();
      k = ((k + ans - 1) % n) + 1;
      int ret = 0;
      for (int i = l; i <= r; ++i) ret += a[i] == k;
      printf("%d\n", ans = ret);
    }
  }
  return 0;
}
1252_F. Regular Forestation

A forestation is an act of planting a bunch of trees to grow a forest, usually to replace a forest that had been cut down. Strangely enough, graph theorists have another idea on how to make a forest, i.e. by cutting down a tree!

A tree is a graph of N nodes connected by N - 1 edges. Let u be a node in a tree U which degree is at least 2 (i.e. directly connected to at least 2 other nodes in U). If we remove u from U, then we will get two or more disconnected (smaller) trees, or also known as forest by graph theorists. In this problem, we are going to investigate a special forestation of a tree done by graph theorists.

Let V(S) be the set of nodes in a tree S and V(T) be the set of nodes in a tree T. Tree S and tree T are identical if there exists a bijection f : V(S) → V(T) such that for all pairs of nodes (s_i, s_j) in V(S), s_i and s_j is connected by an edge in S if and only if node f(s_i) and f(s_j) is connected by an edge in T. Note that f(s) = t implies node s in S corresponds to node t in T.

We call a node u in a tree U as a good cutting point if and only if the removal of u from U causes two or more disconnected trees, and all those disconnected trees are pairwise identical.

Given a tree U, your task is to determine whether there exists a good cutting point in U. If there is such a node, then you should output the maximum number of disconnected trees that can be obtained by removing exactly one good cutting point.

For example, consider the following tree of 13 nodes.

<image>

There is exactly one good cutting point in this tree, i.e. node 4. Observe that by removing node 4, we will get three identical trees (in this case, line graphs), i.e. \{5, 1, 7, 13\}, \{8, 2, 11, 6\}, and \{3, 12, 9, 10\}, which are denoted by A, B, and C respectively in the figure. 

  * The bijection function between A and B: f(5) = 8, f(1) = 2, f(7) = 11, and f(13) = 6. 
  * The bijection function between A and C: f(5) = 3, f(1) = 12, f(7) = 9, and f(13) = 10. 
  * The bijection function between B and C: f(8) = 3, f(2) = 12, f(11) = 9, and f(6) = 10. 

Of course, there exists other bijection functions for those trees.

Input

Input begins with a line containting an integer: N (3 ≤ N ≤ 4000) representing the number of nodes in the given tree. The next N - 1 lines each contains two integers: a_i b_i (1 ≤ a_i < b_i ≤ N) representing an edge (a_i,b_i) in the given tree. It is guaranteed that any two nodes in the given tree are connected to each other by a sequence of edges.

Output

Output in a line an integer representing the maximum number of disconnected trees that can be obtained by removing exactly one good cutting point, or output -1 if there is no such good cutting point.

Examples

Input


13
1 5
1 7
2 4
2 8
2 11
3 12
4 7
4 12
6 11
7 13
9 10
9 12


Output


3


Input


6
1 2
1 3
2 4
3 5
3 6


Output


-1

Note

Explanation for the sample input/output #1

This is the example from the problem description.

Example input:
13
1 5
1 7
2 4
2 8
2 11
3 12
4 7
4 12
6 11
7 13
9 10
9 12

6
1 2
1 3
2 4
3 5
3 6


Example output:

3


-1


Tags: hashing
trees
Difficulty rating: 2400

Solution:
22_E. Scheme

To learn as soon as possible the latest news about their favourite fundamentally new operating system, BolgenOS community from Nizhni Tagil decided to develop a scheme. According to this scheme a community member, who is the first to learn the news, calls some other member, the latter, in his turn, calls some third member, and so on; i.e. a person with index i got a person with index fi, to whom he has to call, if he learns the news. With time BolgenOS community members understood that their scheme doesn't work sometimes — there were cases when some members didn't learn the news at all. Now they want to supplement the scheme: they add into the scheme some instructions of type (xi, yi), which mean that person xi has to call person yi as well. What is the minimum amount of instructions that they need to add so, that at the end everyone learns the news, no matter who is the first to learn it?

Input

The first input line contains number n (2 ≤ n ≤ 105) — amount of BolgenOS community members. The second line contains n space-separated integer numbers fi (1 ≤ fi ≤ n, i ≠ fi) — index of a person, to whom calls a person with index i.

Output

In the first line output one number — the minimum amount of instructions to add. Then output one of the possible variants to add these instructions into the scheme, one instruction in each line. If the solution is not unique, output any.

Examples

Input

3
3 3 2


Output

1
3 1


Input

7
2 3 1 3 4 4 1


Output

3
2 5
2 6
3 7

Example input:
3
3 3 2

7
2 3 1 3 4 4 1


Example output:
1
2 1

3
2 6
6 7
7 5


Tags: dfs and similar
graphs
trees
Difficulty rating: 2300

Solution:
#include <bits/stdc++.h>
using namespace std;
struct node {
  int u, v;
} q[100010];
int n, to[100010], in[100010], vis[100010];
int ans;
int dfs(int x) {
  vis[x] = 1;
  if (!vis[to[x]]) return dfs(to[x]);
  return x;
}
int flag;
int main() {
  scanf("%d", &n);
  for (int i = 1, v; i <= n; i++) {
    scanf("%d", &v);
    to[i] = v;
    in[v]++;
  }
  for (int i = 1; i <= n; i++)
    if (!in[i]) q[++ans] = (node){i, dfs(i)};
  if (!ans) flag = 1;
  for (int i = 1; i <= n; i++)
    if (!vis[i]) q[++ans] = (node){i, dfs(i)};
  if (ans == 1 && flag) {
    printf("0");
    return 0;
  }
  printf("%d\n", ans);
  for (int i = 1; i <= ans; i++) printf("%d %d\n", q[i].v, q[i % ans + 1].u);
  return 0;
}
546_E. Soldier and Traveling

In the country there are n cities and m bidirectional roads between them. Each city has an army. Army of the i-th city consists of ai soldiers. Now soldiers roam. After roaming each soldier has to either stay in his city or to go to the one of neighboring cities by at moving along at most one road.

Check if is it possible that after roaming there will be exactly bi soldiers in the i-th city.

Input

First line of input consists of two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 200).

Next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 100).

Next line contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 100).

Then m lines follow, each of them consists of two integers p and q (1 ≤ p, q ≤ n, p ≠ q) denoting that there is an undirected road between cities p and q. 

It is guaranteed that there is at most one road between each pair of cities.

Output

If the conditions can not be met output single word "NO".

Otherwise output word "YES" and then n lines, each of them consisting of n integers. Number in the i-th line in the j-th column should denote how many soldiers should road from city i to city j (if i ≠ j) or how many soldiers should stay in city i (if i = j).

If there are several possible answers you may output any of them.

Examples

Input

4 4
1 2 6 3
3 5 3 1
1 2
2 3
3 4
4 2


Output

YES
1 0 0 0 
2 0 0 0 
0 5 1 0 
0 0 2 1 


Input

2 0
1 2
2 1


Output

NO

Example input:
4 4
1 2 6 3
3 5 3 1
1 2
2 3
3 4
4 2

2 0
1 2
2 1


Example output:
YES
1 0 0 0 
2 0 0 0 
0 5 1 0 
0 0 2 1 

NO


Tags: flows
graphs
math
Difficulty rating: 2100

Solution:
#include <bits/stdc++.h>
using namespace std;
int s, t, x, y, N, M, c[205][205], R[205][205];
int main() {
  cin >> N >> M;
  t = 2 * N + 1;
  for (int i = 1; i <= N; i++) cin >> c[s][i];
  for (int i = 1; i <= N; i++) cin >> c[N + i][t];
  for (int i = 1; i <= N; i++) c[i][N + i] = 999;
  for (int i = 1; i <= M; i++) cin >> x >> y, c[x][N + y] = c[y][N + x] = 999;
  while (true) {
    queue<int> q;
    vector<int> l(205, 0), z(205, -1);
    l[s] = 1, q.push(s);
    while (q.size()) {
      x = q.front(), q.pop();
      for (int v = 0; v <= 204; v++)
        if (c[x][v] && !l[v]) l[v] = 1, z[v] = x, q.push(v);
    }
    if (!l[t]) break;
    y = 999, x = t;
    while (z[x] >= 0) y = min(y, c[z[x]][x]), x = z[x];
    x = t;
    while (z[x] >= 0) c[z[x]][x] -= y, c[x][z[x]] += y, x = z[x];
  }
  for (int i = 1; i <= N; i++)
    if (c[s][i] || c[N + i][t]) {
      cout << "NO";
      return 0;
    }
  for (int n = 1; n <= N; n++)
    for (int v = 1; v <= N; v++) R[n][v] += c[N + v][n];
  cout << "YES\n";
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) cout << R[i][j] << " ";
    cout << "\n";
  }
}
1082_G. Petya and Graph

Petya has a simple graph (that is, a graph without loops or multiple edges) consisting of n vertices and m edges.

The weight of the i-th vertex is a_i.

The weight of the i-th edge is w_i.

A subgraph of a graph is some set of the graph vertices and some set of the graph edges. The set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices. 

The weight of a subgraph is the sum of the weights of its edges, minus the sum of the weights of its vertices. You need to find the maximum weight of subgraph of given graph. The given graph does not contain loops and multiple edges.

Input

The first line contains two numbers n and m (1 ≤ n ≤ 10^3, 0 ≤ m ≤ 10^3) - the number of vertices and edges in the graph, respectively.

The next line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) - the weights of the vertices of the graph.

The following m lines contain edges: the i-e edge is defined by a triple of integers v_i, u_i, w_i (1 ≤ v_i, u_i ≤ n, 1 ≤ w_i ≤ 10^9, v_i ≠ u_i). This triple means that between the vertices v_i and u_i there is an edge of weight w_i. It is guaranteed that the graph does not contain loops and multiple edges.

Output

Print one integer — the maximum weight of the subgraph of the given graph.

Examples

Input


4 5
1 5 2 2
1 3 4
1 4 4
3 4 5
3 2 2
4 2 2


Output


8


Input


3 3
9 7 8
1 2 1
2 3 2
1 3 3


Output


0

Note

In the first test example, the optimal subgraph consists of the vertices {1, 3, 4} and has weight 4 + 4 + 5 - (1 + 2 + 2) = 8. In the second test case, the optimal subgraph is empty.

Example input:
3 3
9 7 8
1 2 1
2 3 2
1 3 3

4 5
1 5 2 2
1 3 4
1 4 4
3 4 5
3 2 2
4 2 2


Example output:
                                                               0

                                                               8


Tags: flows
graphs
Difficulty rating: 2400

Solution:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MX = 2500;
const int S = 0;
const int T = MX - 1;
const int INF = 2e9;
vector<pair<int, int>> edge;
vector<int> adj[MX];
bool vstd[MX];
ll ans;
void addEdge(int u, int v, int w) {
  adj[u].push_back(edge.size());
  edge.emplace_back(v, w);
  adj[v].push_back(edge.size());
  edge.emplace_back(u, 0);
}
bool dfs(int u, int thr) {
  if (u == T) {
    ans -= thr;
    return true;
  }
  vstd[u] = true;
  for (auto e : adj[u]) {
    auto& [v, w] = edge[e];
    if (w < thr || vstd[v]) continue;
    if (dfs(v, thr)) {
      w -= thr;
      edge[e ^ 1].second += thr;
      return true;
    }
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    int a;
    cin >> a;
    addEdge(i, T, a);
  }
  for (int i = n + 1; i <= n + m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    ans += w;
    addEdge(S, i, w);
    addEdge(i, u, INF);
    addEdge(i, v, INF);
  }
  for (int i = INF; i; i /= 2) {
    while (true) {
      memset(vstd, false, sizeof vstd);
      if (!dfs(S, i)) break;
    }
  }
  cout << ans << '\n';
}
600_F. Edge coloring of bipartite graph

You are given an undirected bipartite graph without multiple edges. You should paint the edges of graph to minimal number of colours, so that no two adjacent edges have the same colour.

Input

The first line contains three integers a, b, m (1 ≤ a, b ≤ 1000, 0 ≤ m ≤ 105), a is the size of the first part, b is the size of the second part, m is the number of edges in the graph.

Each of the next m lines contains two integers x, y (1 ≤ x ≤ a, 1 ≤ y ≤ b), where x is the number of the vertex in the first part and y is the number of the vertex in the second part. It is guaranteed that there are no multiple edges.

Output

In the first line print integer c — the minimal number of colours. The second line should contain m integers from 1 to c — the colours of the edges (in the order they appear in the input).

If there are several solutions, you can print any one of them.

Examples

Input

4 3 5
1 2
2 2
3 2
4 1
4 3


Output

3
1 2 3 1 2

Example input:
4 3 5
1 2
2 2
3 2
4 1
4 3


Example output:
3
2 3 1 1 2 


Tags: graphs
Difficulty rating: 2800

Solution:
#include <bits/stdc++.h>
const int N = 1005, M = 100005;
int na, nb, m;
int x[M], y[M];
int ans, c[N << 1][N];
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::cin >> na >> nb >> m;
  for (int i = 1; i <= m; ++i) {
    std::cin >> x[i] >> y[i];
    y[i] += na;
    int p = 1;
    while (c[x[i]][p]) {
      ++p;
    }
    int q = 1;
    while (c[y[i]][q]) {
      ++q;
    }
    ans = std::max(ans, std::max(p, q));
    c[x[i]][p] = y[i];
    c[y[i]][q] = x[i];
    if (p == q) {
      continue;
    }
    for (int w = q, u = y[i]; u; u = c[u][w], w ^= p ^ q) {
      std::swap(c[u][p], c[u][q]);
    }
  }
  std::cout << ans << "\n";
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= ans; ++j) {
      if (c[x[i]][j] == y[i]) {
        std::cout << j << " ";
        break;
      }
    }
  }
}
p03686 AtCoder Regular Contest 076 - Exhausted?

There are M chairs arranged in a line. The coordinate of the i-th chair (1 ≤ i ≤ M) is i.

N people of the Takahashi clan played too much games, and they are all suffering from backaches. They need to sit in chairs and rest, but they are particular about which chairs they sit in. Specifically, the i-th person wishes to sit in a chair whose coordinate is not greater than L_i, or not less than R_i. Naturally, only one person can sit in the same chair.

It may not be possible for all of them to sit in their favorite chairs, if nothing is done. Aoki, who cares for the health of the people of the Takahashi clan, decides to provide additional chairs so that all of them can sit in chairs at their favorite positions.

Additional chairs can be placed at arbitrary real coordinates. Find the minimum required number of additional chairs.

Constraints

* 1 ≤ N,M ≤ 2 × 10^5
* 0 ≤ L_i < R_i ≤ M + 1(1 ≤ i ≤ N)
* All input values are integers.

Input

Input is given from Standard Input in the following format:


N M
L_1 R_1
:
L_N R_N


Output

Print the minimum required number of additional chairs.

Examples

Input

4 4
0 3
2 3
1 3
3 4


Output

0


Input

7 6
0 7
1 5
3 6
2 7
1 6
2 6
3 7


Output

2


Input

3 1
1 2
1 2
1 2


Output

2


Input

6 6
1 6
1 6
1 5
1 5
2 6
2 6


Output

2

Example input:
6 6
1 6
1 6
1 5
1 5
2 6
2 6
4 4
0 3
2 3
1 3
3 4
7 6
0 7
1 5
3 6
2 7
1 6
2 6
3 7
3 1
1 2
1 2
1 2

Example output:
2
0
2
2

Difficulty rating: 0

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

vector<int>vec[201000];
int n,m,x,y,ans,t;
priority_queue<int>q;

int main(){
  cin.tie(0);
  cin>>n>>m;
  for (int i=n;i--;) cin>>x>>y, vec[x].push_back(y);
  for (int i=m;~i;--i){
    for (auto o:vec[i]) q.push(o);
    if (i&&!q.empty())
      q.pop(), ++t;
  }
  ans=t; y=m;
  for (;!q.empty();){
    x=q.top(); q.pop();
    if (x<=y&&y>t) ++ans, --y;
  }
  cout<<n-ans<<endl;
}