import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
    InputStream is;
    PrintWriter out;
    String INPUT = "";
    //class  Declaration

    static class pair implements Comparable < pair > {
        long x;
        long y;

        pair(long i, long j) {
            x = i;
            y = j;

        }
        public int compareTo(pair p) {
            if (this.x != p.x) {
                return Long.compare(this.x,p.x);
            } else {
                return -Long.compare(this.y,p.y);
            }
        }
        public int hashCode() {
            return (x + " " + y).hashCode();
        }
        public String toString() {
            return x + " " + y;
        }
        public boolean equals(Object o) {
            pair x = (pair) o;
            return (x.x == this.x && x.y == this.y);
        }
    }
    // int[] dx = {0,0,1,-1};
    // int[] dy = {1,-1,0,0};
    // int[] ddx = {0,0,1,-1,1,-1,1,-1};
    // int[] ddy = {1,-1,0,0,1,-1,-1,1};

int inf = (int) 1e9 + 9;
long biginf  = (long)1e17 + 7 ;
long mod =  (long)1e9 + 7;

void solve() throws Exception {
int n=ni();
ArrayList<pair> al=  new ArrayList<>();
for(int i=0;i<n;++i) al.add(new pair(ni(),i));
Collections.sort(al,Collections.reverseOrder());

//pn(al);
ArrayList<ArrayList<pair>> ans = new ArrayList<>();

for(int i=0;i<n;++i){
   ArrayList<pair> subans = new ArrayList<>();
    for(int j=0;j<i+1;++j){
        pair p = al.get(j);
        //ans[i][(int)p.y] = (int)p.x ;
        subans.add(new pair(p.y,p.x));
    }
    Collections.sort(subans);
    ans.add(subans);
}
//pn(ans);

int m =ni();
for(int i=0;i<m;++i){
    int x =ni(),y=ni();
    pn(ans.get(x-1).get(y-1).y);
}

}

long pow(long a, long b) {
        long result = 1;
        while (b > 0) {
            if (b % 2 == 1) result = (result * a) % mod;
            b /= 2;
            a = (a * a) % mod;
        }
        return result;
    }

    void print(Object o) {
        System.out.println(o);
        System.out.flush();
    }

    long gcd(long a, long b) {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
    void run() throws Exception {
        is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        out = new PrintWriter(System.out);

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

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

    //output methods
    private void pn(Object o) {
        out.println(o);
    }
    private void p(Object o) {
        out.print(o);
    }
    private ArrayList < ArrayList < Integer >> ng(int n, int e) {
        ArrayList < ArrayList < Integer >> g = new ArrayList < > ();
        for (int i = 0; i <= n; ++i) g.add(new ArrayList < > ());

        for (int i = 0; i < e; ++i) {
            int u = ni(), v = ni();
            g.get(u).add(v);
            g.get(v).add(u);
        }

        return g;
    }

    private ArrayList < ArrayList < pair >> nwg(int n, int e) {
        ArrayList < ArrayList < pair >> g = new ArrayList < > ();
        for (int i = 0; i <= n; ++i) g.add(new ArrayList < > ());

        for (int i = 0; i < e; ++i) {
            int u = ni(), v = ni(), w = ni();
            g.get(u).add(new pair(w, v));
            g.get(v).add(new pair(w, u));
        }

        return g;
    }

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

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

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

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

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

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

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

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

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

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

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

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

    private void tr(Object...o) {
        if (INPUT.length() > 0) System.out.println(Arrays.deepToString(o));
    }
    void watch(Object...a) throws Exception {
        int i = 1;
        print("watch starts :");
        for (Object o: a) {
            //print(o);
            boolean notfound = true;
            if (o.getClass().isArray()) {

                String type = o.getClass().getName().toString();
                //print("type is "+type);
                switch (type) {
                    case "[I":
                        {
                            int[] test = (int[]) o;
                            print(i + " " + Arrays.toString(test));
                            break;
                        }
                    case "[[I":
                        {
                            int[][] obj = (int[][]) o;

                            print(i + " " + Arrays.deepToString(obj));
                            break;
                        }
                    case "[J":
                        {

                            long[] obj = (long[]) o;
                            print(i + " " + Arrays.toString(obj));
                            break;
                        }
                    case "[[J":
                        {

                            long[][] obj = (long[][]) o;
                            print(i + " " + Arrays.deepToString(obj));
                            break;
                        }
                    case "[D":
                        {

                            double[] obj = (double[]) o;
                            print(i + " " + Arrays.toString(obj));
                            break;
                        }
                    case "[[D":
                        {

                            double[][] obj = (double[][]) o;
                            print(i + " " + Arrays.deepToString(obj));
                            break;
                        }
                    case "[Ljava.lang.String":
                        {

                            String[] obj = (String[]) o;
                            print(i + " " + Arrays.toString(obj));
                            break;
                        }
                    case "[[Ljava.lang.String":
                        {

                            String[][] obj = (String[][]) o;
                            print(i + " " + Arrays.deepToString(obj));
                            break;
                        }
                    case "[C":
                        {
                            char[] obj = (char[]) o;
                            print(i + " " + Arrays.toString(obj));
                            break;
                        }
                    case "[[C":
                        {

                            char[][] obj = (char[][]) o;
                            print(i + " " + Arrays.deepToString(obj));
                            break;
                        }

                    default:
                        {
                            print(i + " type not identified");
                            break;
                        }
                }
                notfound = false;

            }
            if (o.getClass() == ArrayList.class) {
                print(i + " al: " + o);
                notfound = false;
            }
            if (o.getClass() == HashSet.class) {
                print(i + " hs: " + o);
                notfound = false;
            }
            if (o.getClass() == TreeSet.class) {
                print(i + " ts: " + o);
                notfound = false;
            }
            if (o.getClass() == TreeMap.class) {
                print(i + " tm: " + o);
                notfound = false;
            }
            if (o.getClass() == HashMap.class) {
                print(i + " hm: " + o);
                notfound = false;
            }
            if (o.getClass() == LinkedList.class) {
                print(i + " ll: " + o);
                notfound = false;
            }
            if (o.getClass() == PriorityQueue.class) {
                print(i + " pq : " + o);
                notfound = false;
            }
            if (o.getClass() == pair.class) {
                print(i + " pq : " + o);
                notfound = false;
            }

            if (notfound) {
                print(i + " unknown: " + o);
            }
            i++;
        }
        print("watch ends ");
    }

}

#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  vector<int> v2 = v;
  sort(v.rbegin(), v.rend());
  int m;
  cin >> m;
  for (int i = 0; i < m; ++i) {
    int k, ind;
    cin >> k >> ind;
    multiset<int> kmax;
    for (int i = 0; i < k; ++i) {
      kmax.insert(v[i]);
    }
    vector<int> seq;
    for (int i = 0; i < n; ++i) {
      auto it = kmax.find(v2[i]);
      if (it != kmax.end()) {
        kmax.erase(it);
        seq.push_back(v2[i]);
      }
      if (seq.size() == k) {
        break;
      }
    }
    cout << seq[ind - 1] << "\n";
  }
  return 0;
}


m = int(input())
l = [0 for _ in range(m + 1)]
for _ in range(m - 1):
	a,b = map(int, input().split())
	l[a] += 1
	l[b] += 1
if 2 in l:
	print("NO")
else:
	print("YES")

NO YES NO NO YES YES NO NO NO YES NO YES YES

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

public class D1_AddOnATree {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader inp = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Solver solver = new Solver();
        solver.solve(inp, out);
        out.close();
    }

    private static class Solver {
        private void solve(InputReader inp, PrintWriter out) {
            int n = inp.nextInt();

            ArrayList<Integer>[] tree = new ArrayList[n];
            for (int i = 0; i < n; i++) tree[i] = new ArrayList<>();
            for (int i = 0; i < n - 1; i++) {
                int u = inp.nextInt() - 1, v = inp.nextInt() - 1;
                tree[u].add(v);
                tree[v].add(u);
            }

            for (int i = 0; i < n; i++) {
                if (tree[i].size() == 2) {
                    out.print("NO");
                    return;
                }
            }
            out.print("YES");
        }
    }

    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

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

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

        public int nextInt() {
            return Integer.parseInt(next());
        }
        public long nextLong() {
            return Long.parseLong(next());
        }
    }
}


n = int(input())
A = []
for i in range(n):
    A = A+[input().split()]
for a in A:
    if int(a[2]) < int(a[0]) or int(a[2]) > int(a[1]):
        print(a[2])
    else:
        print(int(a[2])*(int(a[1])//int(a[2])+1))

#include <bits/stdc++.h>
using namespace std;
long long f[1000001];
long long pow(long long a, long long b, long long MOD) {
  long long x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x * y);
      if (x > MOD) x %= MOD;
    }
    y = (y * y);
    if (y > MOD) y %= MOD;
    b /= 2;
  }
  return x;
}
long long InverseEuler(long long n, long long MOD) {
  return pow(n, MOD - 2, MOD);
}
long long C(long long n, long long r, long long MOD) {
  return (f[n] *
          ((InverseEuler(f[r], MOD) * InverseEuler(f[n - r], MOD)) % MOD)) %
         MOD;
}
int main() {
  int q;
  cin >> q;
  while (q--) {
    long long l, r, d;
    cin >> l >> r >> d;
    long long ans1 = d;
    long long y = r / d;
    long long ans2 = (y + 1) * d;
    if (d >= l)
      cout << ans2 << endl;
    else
      cout << ans1 << endl;
  }
}


import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author anhnth37
 */
public class A_1060 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        String s = input.next();
        int maxPhoneNumber = n / 11;
        int countNumberEight = 0;
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == '8') {
                countNumberEight++;
            }
        }

        if (countNumberEight > maxPhoneNumber) {
            System.out.println(maxPhoneNumber);
        } else {
            System.out.println(countNumberEight);
        }
    }
}

11 701
```

Note that each sample input consists of two lines, where the first line contains an integer `n` and the second line contains a string of `n` digits.

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long int LINF = 0x3f3f3f3f3f3f3f3fll;
int t, n, a, b, cnt1, cnt2;
string s;
int main() {
  cin >> n;
  cin >> s;
  cnt1 = n / 11;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '8') cnt2++;
  }
  cout << min(cnt1, cnt2) << endl;
  return 0;
}


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

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner in  = new Scanner(System.in);
		String ls = in.next();
		for(int i=1;i<ls.length()-1;i++){
		    if(ls.charAt(i)!=ls.charAt(i-1)&&ls.charAt(i)!=ls.charAt(i+1)&&ls.charAt(i-1)!=ls.charAt(i+1)){
		        if(ls.charAt(i-1)=='.'||ls.charAt(i)=='.'||ls.charAt(i+1)=='.'){
		            continue;
		        }
		        System.out.println("Yes");
		        return;
		    }
		}
		System.out.println("No");
	}
}

no yes no yes no no no no no no no no no no yes no no no no yes no no yes no no no no yes no no no yes yes no no no no no yes no no no no yes yes no no no no no yes

#include <bits/stdc++.h>
using namespace std;
int a[111];
int ok(char ch) {
  if (ch == 'A') return 100;
  if (ch == 'B') return 10;
  if (ch == 'C') return 1;
  return 0;
}
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  char s[111];
  cin >> s;
  for (int i = 0; s[i] != '\0'; i++) {
    if (s[i] != '.') {
      for (int j = -1; j <= 1; j++) {
        if (i + j >= 0) {
          a[i + j] += ok(s[i]);
        }
      }
    }
  }
  for (int i = 0; s[i] != '\0'; i++) {
    if (a[i] == 111) {
      cout << "yes\n";
      return 0;
    }
  }
  cout << "no\n";
  return 0;
}


from collections import Counter

def main():
    n, k = list(map(int, input().split()))
    a = list(map(int, input().split()))
    t = list(map(int, input().split()))
    print(solve(n, k, a, t))

def solve(n, k, a, t):
    minsum = sum(ai for ai, ti in zip(a,t) if ti == 1)
    inc = sum(ai for ai, ti in zip(a[:k], t[:k]) if ti == 0)
    best_inc = inc
    for i in range(1, n - k + 1):
        if t[i - 1] == 0:
            inc -= a[i - 1]
        if t[i + k - 1] == 0:
            inc += a[i + k - 1]
        if inc > best_inc:
            best_inc = inc
    return minsum + best_inc

main()

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> theorems(n);
  vector<bool> awake(n);
  for (int i = 0; i < n; i++) {
    cin >> theorems[i];
  }
  bool isAwake;
  long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> isAwake;
    awake[i] = isAwake;
    if (awake[i]) {
      sum += theorems[i];
    }
  }
  long currSum = 0;
  for (int i = 0; i < k; i++) {
    if (!awake[i]) {
      currSum += theorems[i];
    }
  }
  long maxSum = currSum + sum;
  for (int i = k; i < n; i++) {
    if (!awake[i]) {
      currSum += theorems[i];
    }
    if (!awake[i - k]) {
      currSum -= theorems[i - k];
    }
    maxSum = max(maxSum, sum + currSum);
  }
  cout << maxSum;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
unsigned long long dp[40];
unsigned long long N, L, i, C = 0;
int main() {
  cin >> N >> L >> dp[0];
  for (i = 1; i < N; i++) {
    cin >> dp[i];
    dp[i] = min(dp[i], 2 * dp[i - 1]);
  }
  for (; i < 32; i++) {
    dp[i] = 2 * dp[i - 1];
  }
  for (i = 0; L || i < N; i++) {
    C = min(C, dp[i]);
    if (L & 1) C += dp[i];
    L >>= 1;
  }
  cout << C;
  return 0;
}

n = input().split()
l = int(n[1])
n = int(n[0])
c = input().split()
cperl = []
for i in range(n):
    c[i] = int(c[i])
    cperl.append([(c[i])/2**i, 2**i, i])
cperl.sort()
f = False
nl = [0]
nc = [0]
i = 0
while not f:
    p = nl[i]
    nl[i] += cperl[i][1] * ((l-p)//cperl[i][1])
    nc[i] += c[cperl[i][2]] * ((l-p)//cperl[i][1])
    if nl[i] == l:
        f = True
        break
    nl.append(nl[i])
    nc.append(nc[i])
    nl[i] += cperl[i][1]
    nc[i] += c[cperl[i][2]]
    i += 1
    if i == n:
        break
print(min(nc))


#include <bits/stdc++.h>
using namespace std;
int n, i, j, k, p, q;
pair<int, int> a[100007];
vector<vector<int> > ans;
bool vis[100007];
void dfs(int x, vector<int> &ans) {
  if (vis[x]) return;
  ans.push_back(x);
  vis[x] = 1;
  dfs(a[x].second, ans);
}
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a + 1, a + n + 1);
  for (i = 1; i <= n; i++) {
    if (vis[a[i].second] == 0) {
      ans.push_back(vector<int>());
      dfs(a[i].second, ans.back());
    }
  }
  cout << ans.size() << endl;
  for (i = 0; i < ans.size(); i++) {
    cout << ans[i].size() << " ";
    for (j = 0; j < ans[i].size(); j++) cout << ans[i][j] << " ";
    cout << endl;
  }
}

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

public class C {
  public static void main(String[] args) {
    int n;
    int[] a = null, sorted = null, mark = null;
    List<List> outputs = null;

    try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {
      String line = in.readLine();
      n = Integer.parseInt(line);
      a = new int[n];
      sorted = new int[n];
      mark = new int[n];
      outputs = new LinkedList<List>();

      line = in.readLine();
      String[] numbers = line.split("\\s+");
      for (int i = 0; i < n; i++) {
        a[i] = Integer.parseInt(numbers[i]);
        sorted[i] = a[i];
      }
    } catch (IOException e) {
      return;
    }

    Arrays.sort(sorted);
    PrintWriter out = new PrintWriter(System.out);
    int subSequenceCount = 0;

    for (int i = 0; i < n; i++) {
      if (mark[i] > 0) { continue; }

      List<Integer> outputLine = new LinkedList<Integer>();
      outputs.add(outputLine);
      subSequenceCount++;
      int j = i, count = 0;

      while (mark[j] == 0) {
        mark[j] = subSequenceCount;
        outputLine.add(j);
        j = findIndexOf(a[j], n, sorted);
      }
    }

    out.println(subSequenceCount);
    for (List<Integer> list : outputs) {
      out.printf("%d ", list.size());
      for (Integer i : list) {
        out.printf("%d ", i + 1);
      }
      out.println();
    }
    out.flush();
    out.close();
  }

  private static int findIndexOf(int value, int n, int[] a) {
    int begin = 0, end = n - 1;
    while (begin <= end) {
      if (begin == end) {
        if (a[begin] == value) {
          return begin;
        } else {
          return -1;
        }
      }
      int mid = begin + (end - begin)/2;

      if (value == a[mid]) {
        return mid;
      } else if (value < a[mid]) {
        end = mid - 1;
      } else {
        begin = mid + 1;
      }
    }
    return -1;
  }
}


import java.util.Scanner;

public class Main2 {
	public static final int MAXL=20000;
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		 int n;
		n=in.nextInt();
		  int lat = 0; boolean valid = true;
		  for(int i = 0; i < n; i++) {
		    int v=in.nextInt();
			  String dir=in.next();
		    	char[]dirr=dir.toCharArray();
		    switch(dirr[0]) {
		      case 'N': lat -= v; break;
		      case 'S': lat += v; break;
		      default: if(lat == 0 || lat == MAXL) lat = -1;
		    }
		    if(lat < 0 || lat > MAXL) {
		      valid = false;
		      break;
		    }
		  }
		  System.out.printf("%s\n", valid && lat == 0 ? "YES" : "NO");

	}
}

16
1 South
2 North
3 East
4 West
5 South
6 North
7 East
8 West
9 South
10 North
11 East
12 West
13 South
14 North
15 East
16 West

4
50 South
50 North
15000 South
15000 North

20
1 South
2 North
3 East
4 West
5 South
6 North
7 East
8 West
9 South
10 North
11 East
12 West
13 South
14 North
15 East
16 West
17 South
18 North
19 East
20 West

17
1 North
2 South
3 East
4 West
5 North
6 South
7 East
8 West
9 North
10 East
11 South
12 West
13 North
14 East
15 West
16 North
17 South

27
1 North
2 South
3 East
4 West
5 South
6 North
7 East
8 West
9 South
10 North
11 East
12 West
13 South
14 North
15 East
16 West
17 South
18 North
19 East
20 West
21 South
22 North
23 East
24 West
25 South
26 East
27 North

15
1 North
2 South
3 East
4 West
5 North
6 South
7 East
8 West
9 North
10 East
11 South
12 West
13 North
14 East
15 South

31
1 North
2 South
3 East
4 West
5 South
6 North
7 East
8 West
9 South
10 North
11 East
12 West
13 South
14 North
15 East
16 West
17 South
18 North
19 East
20 West
21 South
22 North
23 East
24 West
25 South
26 East
27 North
28 West
29 South
30 North
31 West

5
7500 South
10000 East
3500 North
4444 West
4000 North

18
1 South
2 North
3 East
4 West
5 South
6 North
7 East
8 West
9 South
10 North
11 East
12 West
13 South
14 North
15 East
16 West
17 North
18 South

11
1 North
2 South
3 East
4 West
5 North
6 South
7 East
8 West
9 North
10 East
11 South

23
1 North
2 South
3 East
4 West
5 North
6 South
7 East
8 West
9 North
10 East
11 South
12 West
13 North
14 East
15 West
16 South
17 North
18 East
19 West
20 North
21 South
22 East
23 North

14
1 South
2 North
3 East
4 West
5 South
6 North
7 East
8 West
9 South
10 North
11 East
12 West
13 South
14 North

22
1 South
2 North
3 East
4 West
5 South
6 North
7 East
8 West
9 South
10 North
11 East
12 West
13 South
14 North
15 East
16 West
17 South
18 North
19 East
20 West
21 North
22 South

25
1 North
2 South
3 East
4 West
5 South
6 North
7 East
8 West
9 South
10 North
11 East
12 West
13 South
14 North
15 East
16 West
17 South
18 North
19 East
20 West
21 South
22 North
23 East
24 West
25 North

28
1 South
2 North
3 East
4 West
5 South
6 North
7 East
8 West
9 South
10 North
11 East
12 West
13 South
14 North
15 East
16 West
17 South
18 North
19 East
20 West
21 South
22 North
23 East
24 West
25 South
26 East
27 North
28 West

21
1 North
2 South
3 East
4 West
5 North
6 South
7 East
8 West
9 North
10 East
11 South
12 West
13 North
14 East
15 West
16 South
17 North
18 East
19 West
20 North
21 South

12
1 South
2 North
3 East
4 West
5 South
6 North
7 East
8 West
9 South
10 North
11 East
12 West

7
13750 South
6645 South
18539 East
5713 North
1580 North
10012 West
13102 North

19
1 North
2 South
3 East
4 West
5 North
6 South
7 East
8 West
9 North
10 East
11 South
12 West
13 North
14 East
15 West
16 North
17 South
18 East
19 North

10
1 South
2 North
3 East
4 West
5 South
6 North
7 East
8 West
9 South
10 North

4
6549 East
5118 North
12198 East
5118 South

13
1 North
2 South
3 East
4 West
5 North
6 South
7 East
8 West
9 North
10 East
11 South
12 West
13 North

30
1 South
2 North
3 East
4 West
5 South
6 North
7 East
8 West
9 South
10 North
11 East
12 West
13 South
14 North
15 East
16 West
17 South
18 North
19 East
20 West
21 South
22 North
23 East
24 West
25 South
26 East
27 North
28 West
29 South
30 North

29
1 North
2 South
3 East
4 West
5 South
6 North
7 East
8 West
9 South
10 North
11 East
12 West
13 South
14 North
15 East
16 West
17 South
18 North
19 East
20 West
21 South
22 North
23 East
24 West
25 South
26 East
27 North
28 West
29 North

26
1 South
2 North
3 East
4 West
5 South
6 North
7 East
8 West
9 South
10 North
11 East
12 West
13 South
14 North
15 East
16 West
17 South
18 North
19 East
20 West
21 South
22 North
23 East
24 West
25 South
26 North

24
1 South
2 North
3 East
4 West
5 South
6 North
7 East
8 West
9 South
10 North
11 East
12 West
13 South
14 North
15 East
16 West
17 South
18 North
19 East
20 West
21 South
22 North
23 East
24 West

NO YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES NO YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO

n = int(input())
x = 0
y = 0
fl = True
for i in range(n):
    ar = input().split()
    d = int(ar[0])
    if fl:
        if x == 0 and ar[1] != 'South':
            fl = False
        if x == 20000 and ar[1] != 'North':
            fl = False
        if ar[1][0] == 'S':
            if x + d > 20000:
                fl = False
            else:
                x += d
        if ar[1][0] == 'N':
            if x - d < 0:
                fl = False
            else:
                x -= d
        if ar[1][0] == 'E':
            y = (x + d) % 40000
        if ar[1][0] == 'W':
            y = (40000 + x - d) % 40000
if x != 0 or not fl:
    print('NO')
else:
    print('YES')


s = input()

stack = []
res = 0
for c in s:
    if c in '(<[{':
        stack.append(c)
    else:
        if not stack:
            print('Impossible')
            break
        last = stack.pop()
        if last + c not in ('[]', '()', '<>', '{}'):
            res += 1
else:
    if stack:
        print('Impossible')
    else:
        print(res)

#include <bits/stdc++.h>
using namespace std;
char ch[1000000 + 10];
stack<int> s;
int main() {
  int i;
  while (~scanf("%s", ch)) {
    int l = strlen(ch);
    int flag = 1;
    int ans = 0;
    while (!s.empty()) s.pop();
    for (i = 0; i < l; i++) {
      if (ch[i] == '[' || ch[i] == '<' || ch[i] == '(' || ch[i] == '{')
        s.push(ch[i]);
      if (s.empty()) {
        flag = 0;
        break;
      }
      if (ch[i] == ']') {
        if (s.top() != '[') ans++;
        s.pop();
      } else if (ch[i] == '}') {
        if (s.top() != '{') ans++;
        s.pop();
      } else if (ch[i] == '>') {
        if (s.top() != '<') ans++;
        s.pop();
      } else if (ch[i] == ')') {
        if (s.top() != '(') ans++;
        s.pop();
      }
    }
    if (!s.empty() || !flag)
      printf("Impossible\n");
    else
      printf("%d\n", ans);
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, frm, to, mon, ret = 0;
  scanf("%d%d", &n, &m);
  vector<int> peop(n, 0);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &frm, &to, &mon);
    peop[frm - 1] += mon, peop[to - 1] -= mon;
  }
  for (int i = 0; i < n; i++) {
    mon = peop[i];
    if (mon < 0) mon = 0;
    ret += mon;
  }
  printf("%d\n", ret);
  return 0;
}

import java.io.*;
import java.util.*;
public final class iou
{
	static Scanner sc=new Scanner(System.in);
	static PrintWriter out=new PrintWriter(System.out);

	public static void main(String args[]) throws Exception
	{
		int n=sc.nextInt(),m=sc.nextInt();
		long[] a=new long[n],b=new long[n];
		for(int i=0;i<m;i++)
		{
			int u=sc.nextInt()-1,v=sc.nextInt()-1;
			long amt=sc.nextLong();
			a[u]+=amt;
			b[v]+=amt;
		}
		long ans=0;
		for(int i=0;i<n;i++)
		{
			if(b[i]-a[i]>0)
			{
				ans+=b[i]-a[i];
			}
		}
		out.println(ans);
		out.close();
	}
}


import collections
n = int(input())
l = list(map(int,input().split()))
s = ""
if 0 in l:
    c1 = collections.Counter(l)
    s+=('0'*c1[0])

    x = (c1[5]//9)*9
    s = ('5'*x) + s
    print(int(s))
else:
    print(-1)

import java.util.Scanner;

public class nugo{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int n;
		n = scan.nextInt();
		int[] arr = new int[n];
		int i = 0;
		int count_0 = 0;
		int count_5 = 0;
		String num_5 ="";
		String num_0 ="";
		String num;
		while(i < n){
			arr[i] =scan.nextInt();
			if(arr[i] == 5){
				count_5++;
				if(count_5 == 9){
					num_5 += "555555555";
					count_5 = 0;
				}
			}
			else{
				count_0++;
				num_0 += "0";
			}
			i++;
		}
		if(count_0 == 0){
			System.out.println(-1);
		}
		else if(num_5 == ""){
			System.out.println(0);
		}
		else{
			num = num_5 + num_0;
			System.out.print(num);
		}

	}
}


#include <bits/stdc++.h>
using namespace std;
long long n, k;
bool ch(long long a) {
  long long ans = 0, res = 1;
  while (a >= res) {
    ans += a / res;
    res *= k;
  }
  return ans >= n;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  long long l = 1, r = n;
  while (r - l > 1) {
    long long mid = (l + r) / 2;
    if (ch(mid))
      r = mid;
    else
      l = mid;
  }
  if (ch(l))
    cout << l;
  else
    cout << l + 1;
  return 0;
}

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class BurningMidnightOil {
	static int n;
	public static void main(String[]args)throws NumberFormatException,IOException{
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		String s=bf.readLine();
		String[]sa=s.split(" ");
		n=Integer.parseInt(sa[0]);
		int k=Integer.parseInt(sa[1]);
		System.out.println(search(k));
	}

	public static int search(int k){
		int ans=0,start=0,end=n;
		while(start<=end){
			int mid=((start+end)>>1);
			int y=f(mid,k);
			if(y<n)
				start=mid+1;
			else{
				end=mid-1;
				ans=mid;
			}
		}
		return ans;
	}

	public static int f(int v,int k){
		int ans=0,x=v;
		while(x>0){
			ans+=x;
			x/=k;
		}
		return ans;
	}
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Main {

public static void main(String[] args){

   new Thread(null, null, "Anshum Gupta", 99999999) {
        public void run() {
            try {
                solve();
            } catch(Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    }.start();
}

static final long MXX = (long)(1e18 + 5), MOD = 998244353; //(long)1e9 + 7
static final int MXN = (int)(1001), MXV = (int)(1e6), log = 18, INF = (int)1e9 + 500;
static boolean[]vis;
static ArrayList<ArrayList<Integer>> adj;
static int n, m, q, k;
static char[] str;

public static void solve() throws Exception {
	   // solve the problem here
		s = new MyScanner();
   		out = new MyWriter(new BufferedOutputStream(System.out));
//	   		out = new PrintWriter("output.txt");
        int tc = 1;//s.nextInt();
    	for(int i=1; i<=tc; i++) {
//        	out.print("Case #" + i + ": ");
    		testcase();
    	}

        out.flush();
        out.close();
}

static void testcase() {
	n = s.nextInt();
	int[] a = s.nextIntArray(n);
	shuffleSort(a);
	long[][] dp = new long[n][n];
	long[][] prefix = new long[n][n + 1];

	// dp[i][j] -> place i fisherman such that j is the last happy one

	for(int i = 0; i < n; i++) dp[i][0] = 1l;

	int pointer = 0;

	for(int i = 0; i < n; i++) {

		// pointer points to the first fish such that a[pointer] * 2 > a[i]
		// that is everything from pointer...i-1 cannot make current fisherman happy

		while(2 * a[pointer] <= a[i])
			pointer++;

		for(int j = 0; j < i; j++) {

			// add all previous ones which can make i'th fisherman last happy
			dp[i][j + 1] += prefix[j][pointer];
			dp[i][j + 1] %= MOD;
		}

		for(int j = 0; j < pointer; j++) {
			dp[i][j + 1] += (dp[i][j] * (pointer - j)) % MOD;
			dp[i][j + 1] %= MOD;
		}

		for(int j = 0; j <= i; j++) {
			prefix[j][i + 1] = prefix[j][i] + dp[i][j];
			prefix[j][i + 1] %= MOD;
		}

	}

	out.println(dp[n - 1][n - 1]);

}

public static MyWriter out;
public static MyScanner s;
static void shuffleArray(int[] a) {
	Random random = new Random();
	for (int i = a.length-1; i > 0; i--) {
		int index = random.nextInt(i + 1);
		int tmp = a[index];
		a[index] = a[i];
		a[i] = tmp;
	}
}
static void shuffleSort(int[] a) {
	shuffleArray(a);
	Arrays.parallelSort(a);
}
static class MyScanner {

    BufferedReader br;
    StringTokenizer st;

    public MyScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    public MyScanner(String fileName) {
    	try {
			br = new BufferedReader(new FileReader(fileName));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
    }
    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() { return Integer.parseInt(next()); }
    long nextLong() { return Long.parseLong(next()); }
    double nextDouble() { return Double.parseDouble(next()); }
    int[] nextIntArray(int n){
    	int[]a = new int[n];
    	for(int i=0; i<n; i++) {
    		a[i] = this.nextInt();
    	}
    	return a;
    }
    long[] nextlongArray(int n) {
    	long[]a = new long[n];
    	for(int i=0; i<n; i++) {
    		a[i] = this.nextLong();
    	}
    	return a;
    }
    Integer[] nextIntegerArray(int n){
    	Integer[]a = new Integer[n];
    	for(int i=0; i<n; i++) {
    		a[i] = this.nextInt();
    	}
    	return a;
    }
    Long[] nextLongArray(int n) {
    	Long[]a = new Long[n];
    	for(int i=0; i<n; i++) {
    		a[i] = this.nextLong();
    	}
    	return a;
    }
    char[][] next2DCharArray(int n, int m){
    	char[][]arr = new char[n][m];
    	for(int i=0; i<n; i++) {
    		arr[i] = this.next().toCharArray();
    	}
    	return arr;
    }
    ArrayList<ArrayList<Integer>> readUndirectedUnweightedGraph(int n, int m) {
    	ArrayList<ArrayList<Integer>>adj = new ArrayList<ArrayList<Integer>>();
    	for(int i=0; i<=n; i++)adj.add(new ArrayList<Integer>());
    	for(int i=0; i<m; i++) {
    		int u = s.nextInt();
    		int v = s.nextInt();
    		adj.get(u).add(v);
    		adj.get(v).add(u);
    	}
    	return adj;
    }
    ArrayList<ArrayList<Integer>> readDirectedUnweightedGraph(int n, int m) {
    	ArrayList<ArrayList<Integer>>adj = new ArrayList<ArrayList<Integer>>();
    	for(int i=0; i<=n; i++)adj.add(new ArrayList<Integer>());
    	for(int i=0; i<m; i++) {
    		int u = s.nextInt();
    		int v = s.nextInt();
    		adj.get(u).add(v);
    	}
    	return adj;
    }
    String nextLine(){
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}

static class MyWriter extends PrintWriter {
    MyWriter(OutputStream out) {
        super(out);
    }
    void println(int[] x) {
        println(x, " ");
    }
    void println(int[] x, String delimiter) {
        if (x.length > 0) {
            print(x[0]);
            for (int i = 1; i < x.length; i++) {
                print(delimiter);
                print(x[i]);
            }
        }
        println();
    }
    void println(long[] x) {
        println(x, " ");
    }
    void println(long[] x, String delimiter) {
        if (x.length > 0) {
            print(x[0]);
            for (int i = 1; i < x.length; i++) {
                print(delimiter);
                print(x[i]);
            }
        }
        println();
    }
    void println(Iterable<?> iterable) {
        println(iterable, " ");
    }
    void println(Iterable<?> iterable, String delimiter) {
        Iterator<?> i = iterable.iterator();
        if (i.hasNext()) {
            print(i.next());
            while (i.hasNext()) {
                print(delimiter);
                print(i.next());
            }
        }
        println();
    }
    void printLines(int[] x) {
        println(x, System.lineSeparator());
    }
    void printLines(long[] x) {
        println(x, System.lineSeparator());
    }
    void printLines(Iterable<?> iterable) {
        println(iterable, System.lineSeparator());
    }
}

}

#include <bits/stdc++.h>
void init();
void solve();
int main() {
  init();
  while (scanf("%*[ \t\n]*") != EOF) {
    solve();
  }
  return 0;
}
const int MOD = 998244353;
const int MaxN = 5001;
int a[MaxN], n;
long long dp[2][MaxN];
void init() {}
void solve() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  std::sort(a, a + n);
  std::fill(dp[0], dp[0] + n, 1);
  int fd = 0, nd = 1;
  for (int i = 1; i < n; i++, fd ^= 1, nd ^= 1) {
    std::fill(dp[nd], dp[nd] + n, 0);
    long long sum = 0;
    for (int j = 0, k = 0; j < n; j++) {
      if (dp[fd][j] > 0) {
        while (k < n && a[k] * 2 <= a[j]) {
          sum = (sum + dp[fd][k]) % MOD;
          k++;
        }
        if (k > i - 1) {
          dp[nd][j] = (dp[nd][j] + dp[fd][j] * (k - i + 1)) % MOD;
        }
        dp[nd][j] = (dp[nd][j] + sum) % MOD;
      }
    }
  }
  printf("%lld\n", std::accumulate(dp[fd], dp[fd] + n, 0LL) % MOD);
}


import java.util.Scanner;

public class C {

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

        for (int i = 1; i > 0; i--) solve(scan);
    }

    protected static void solve(Scanner scan) {
        int n = scan.nextInt();
        int m = scan.nextInt();
        int[] a = new int[n];
        int[] b = new int[m];

        for (int i = 0; i < n; i++) {
            a[i] = scan.nextInt();
        }

        for (int i = 0; i < m; i++) {
            b[i] = scan.nextInt();
        }

        int i = 0;

        for (; i < 2 << 9; i++) {
            boolean af = false;
            for (int j : a) {
                af = false;
                for (int k : b) {
                    if ((i | (j & k)) == i) {
                        af = true;
                        break;
                    }
                }
                if (!af) {
                    break;
                }
            }
            if (af) break;
        }

        System.out.println(i);
    }
}

from sys import stdin
input=lambda : stdin.readline().strip()
from math import ceil,sqrt,factorial,gcd
from collections import deque
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
z=[]
ma=0
for i in a:
	x=float("infinity")
	for j in b:
		x=min(i&j,x)
	ma=max(x,ma)
for i in a:
	t=float("infinity")
	for j in b:
		if t==float("infinity") or ma|(i&j)<ma|t:
			t=i&j
	z.append(t)
x=z[0]
for i in z:
	x=x|i
print(x)


def func(lst):
    prod = 1
    for item in lst:
        if item!=0:
            prod = prod*item
    return prod

w = int(input())
a=list(map(int,input().split()))

count=0
count1=0

for i,item in enumerate(a):
    if item<0 and item !=-1:
        count+=abs(item+1)
        a[i]=-1

    if item>0 and item!=1:
        count+=item-1
        a[i]=1

    if item ==0:
        count1 += 1

prod = func(a)
if prod == -1 and count1==0:
    count = count+2
else:
    count = count+ count1

print(count)

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int arr[n];
  long long ans = 0, c = 0, c2 = 0;
  for (int i = 0; i < n; i++) {
    int h;
    cin >> h;
    if (h < 0) {
      ans += (-h - 1);
      c++;
    } else if (h > 0) {
      ans += h - 1;
    } else {
      c2++;
    }
  }
  if (c % 2) {
    if (c2 > 0)
      ans += c2;
    else
      ans += 2;
  } else {
    ans += c2;
  }
  cout << ans << endl;
  return 0;
}


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

public class one {

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

		StringTokenizer st = new StringTokenizer(in.readLine());
		int n = Integer.parseInt(st.nextToken());
		int a = Integer.parseInt(st.nextToken());
		int x = Integer.parseInt(st.nextToken());
		int b = Integer.parseInt(st.nextToken());
		int y = Integer.parseInt(st.nextToken());

		int x_pos = a;
		int y_pos = b;

		boolean xcont=true;
		boolean ycont=true;
		while (xcont || ycont) {
			x_pos++;
			y_pos--;
			if (x_pos>n) x_pos = 1;
			if (y_pos<1) y_pos = n;

			if (xcont && ycont && x_pos == y_pos) {
				System.out.println("YES");
				return;
			}

			if (x_pos == x) {
				xcont=false;
			}
			if (y_pos == y) {
				ycont=false;
			}
		}

		System.out.println("NO");

	}

}

NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES NO NO NO NO NO NO NO NO YES YES NO NO NO YES NO NO NO YES NO NO YES NO NO NO NO NO YES NO YES NO NO NO NO NO YES

n,a,x,b,y=map(int,input().split(" "))
flag =1
while a!=x and b!=y:
	a = a+ 1
	b = b-1
	if(a==n+1):
		a = 1
	if(b==0):
		b=n
	if(a==b):
		flag =0
		print('YES')
		break;
if flag==1:
	print ('NO')


import java.util.*;
import java.io.*;
public class MainClass
{
    public static void main(String[] args)throws IOException
    {
//        Reader in = new Reader();
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String s1[] = in.readLine().split(" ");
        int n = Integer.parseInt(s1[0]);
        int a = Integer.parseInt(s1[1]);
        int b = Integer.parseInt(s1[2]);
        String s = in.readLine();
        String rev = new StringBuilder(s).reverse().toString();
        ZAlgorithm zAlgorithm = new ZAlgorithm();
        int[] dp = new int[n];
        int inf = Integer.MAX_VALUE / 5;
        Arrays.fill(dp, inf);
        dp[0] = a;
        for (int i=1;i<n;i++)
        {
            int[] Z = zAlgorithm.calculateZ(rev.substring(n - 1 - i).toCharArray());
            for (int j=1;j<=i;j++)
            {
                int xx = Z[j];
                dp[i] = Math.min(dp[i], dp[Math.max(i - xx, i - j)] + b);
            }
            dp[i] = Math.min(dp[i - 1] + a, dp[i]);
        }
        System.out.println(dp[n - 1]);
    }
}
class Reader
{
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader()
    {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException
    {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException
    {
        byte[] buf = new byte[64]; // line length
        int cnt = 0, c;
        while ((c = read()) != -1)
        {
            if (c == '\n')
                break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException
    {
        int ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do
        {
            ret = ret * 10 + c - '0';
        }  while ((c = read()) >= '0' && c <= '9');

        if (neg)
            return -ret;
        return ret;
    }

    public long nextLong() throws IOException
    {
        long ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public double nextDouble() throws IOException
    {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();

        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');

        if (c == '.')
        {
            while ((c = read()) >= '0' && c <= '9')
            {
                ret += (c - '0') / (div *= 10);
            }
        }

        if (neg)
            return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException
    {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1)
            buffer[0] = -1;
    }

    private byte read() throws IOException
    {
        if (bufferPointer == bytesRead)
            fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException
    {
        if (din == null)
            return;
        din.close();
    }
}
class ZAlgorithm
{
    public int[] calculateZ(char input[])
    {
        int Z[] = new int[input.length];
        int left = 0;
        int right = 0;
        for(int k = 1; k < input.length; k++) {
            if(k > right) {
                left = right = k;
                while(right < input.length && input[right] == input[right - left]) {
                    right++;
                }
                Z[k] = right - left;
                right--;
            } else {
                //we are operating inside box
                int k1 = k - left;
                //if value does not stretches till right bound then just copy it.
                if(Z[k1] < right - k + 1) {
                    Z[k] = Z[k1];
                } else { //otherwise try to see if there are more matches.
                    left = k;
                    while(right < input.length && input[right] == input[right - left]) {
                        right++;
                    }
                    Z[k] = right - left;
                    right--;
                }
            }
        }
        return Z;
    }
    public ArrayList<Integer> matchPattern(char text[], char pattern[])
    {
        char newString[] = new char[text.length + pattern.length + 1];
        int i = 0;
        for(char ch : pattern) {
            newString[i] = ch;
            i++;
        }
        newString[i] = '$';
        i++;
        for(char ch : text) {
            newString[i] = ch;
            i++;
        }
        ArrayList<Integer> result = new ArrayList<>();
        int Z[] = calculateZ(newString);

        for(i = 0; i < Z.length ; i++) {
            if(Z[i] == pattern.length) {
                result.add(i - pattern.length - 1);
            }
        }
        return result;
    }
}

79 1 2
fifchfihhhfbijhhifcefgfjifdfchfdfjegiddhfagdhfiffhfijghdifbhfafedhdfcififcfaccj

45 44 43
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst

33 32 31
abcdefghijklmnopqrstuvwxyzabcdefg

38 37 36
abcdefghijklmnopqrstuvwxyzabcdefghijkl

43 42 41
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqr

66 65 64
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnop

35 34 33
abcdefghijklmnopqrstuvwxyzabcdefghi

52 51 50
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyza

48 47 46
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvw

60 59 58
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghi

55 54 53
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd

32 31 30
abcdefghijklmnopqrstuvwxyzabcdef

62 61 60
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk

53 52 51
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzab

36 35 34
abcdefghijklmnopqrstuvwxyzabcdefghij

44 43 42
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs

57 56 55
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdef

42 41 40
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopq

39 38 37
abcdefghijklmnopqrstuvwxyzabcdefghijklm

61 60 59
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghij

100 1 5000
rrjzcferfrgxyyxrrrrderrrwrrvtrrtlfrrrraqgfrzurtlrlrrrunrporrrroyrgsrjfrwsenarvhkrtrrrrqnrlrryrxrrqrr

37 36 35
abcdefghijklmnopqrstuvwxyzabcdefghijk

47 46 45
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv

50 49 48
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy

46 45 44
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstu

63 62 61
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl

58 57 56
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg

44 5000 1
cbbbbbbccbcbbcbbbbcbbbbbcbcbcbbcbcbbcbbbbcbb

65 64 63
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmn

31 30 29
abcdefghijklmnopqrstuvwxyzabcde

64 63 62
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm

56 55 54
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcde

34 33 32
abcdefghijklmnopqrstuvwxyzabcdefgh

100 5 4
bbbccccbaabccccaccaaabbcacbccabbbcaccaaacbbcbccbacbbccccbbaabcbcbbbbaababccbcbbcbbbccccbabcbaaccabaa

40 39 38
abcdefghijklmnopqrstuvwxyzabcdefghijklmn

41 40 39
abcdefghijklmnopqrstuvwxyzabcdefghijklmnop

54 53 52
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabc

49 48 47
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx

59 58 57
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefgh

51 50 49
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz

#include <bits/stdc++.h>
using namespace std;
signed main() {
  int n, a, b;
  cin >> n >> a >> b;
  string s;
  cin >> s;
  vector<int> dp(n + 1, INT_MAX);
  vector<vector<int> > lcp(n, vector<int>(n));
  for (int i = n - 1; i >= 0; --i) {
    for (int j = n - 1; j >= 0; --j) {
      if (s[i] == s[j]) {
        if (i == n - 1 || j == n - 1) {
          lcp[i][j] = 1;
        } else {
          lcp[i][j] = 1 + lcp[i + 1][j + 1];
        }
      }
    }
  }
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    dp[i + 1] = min(dp[i + 1], dp[i] + a);
    for (int j = 0; j < i; ++j) {
      int k = min(lcp[i][j], i - j);
      dp[i + k] = min(dp[i + k], dp[i] + b);
    }
  }
  cout << dp[n];
}


#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0LL) return a;
  return gcd(b, a % b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  long long k;
  cin >> n >> k;
  long long ai;
  cin >> ai;
  long long g = ai;
  for (int i = 2; i <= n; i++) {
    cin >> ai;
    g = gcd(g, ai);
  }
  g %= k;
  set<int> s;
  for (int i = 0; i < k; i++) {
    s.insert((i * g) % k);
  }
  cout << s.size() << "\n";
  for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
    cout << (*it) << " ";
  }
  cout << "\n";
  return 0;
}

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

public class Main {
	static Scanner in;
 	static PrintWriter out;
 	static Random rand = new Random();
 	static int gcd(int a, int b) {
 		if(a%b==0) return b;
 		return gcd (b, a%b);
 	}
	public static void main(String args[]) throws IOException {
		out = new PrintWriter(System.out);
	    in = new Scanner(System.in);
	    int n = in.nextInt();
	    int k = in.nextInt();
	    int g = in.nextInt();
	    for(int i=1;i<n;i++) {
	    	int next = in.nextInt();
	    	if(next>g) {
	    		g = gcd(next, g);
	    	}else {
	    		g = gcd(g, next);
	    	}
	    }
	    int tmp = 0;
	    Set<Integer> s = new TreeSet<Integer>();
	    for(int i =0;i<k;i++) {
	    	tmp+=g;
	    	tmp%=k;
	    	s.add(tmp);
	    }
	    int ans = s.size();
	    System.out.println(ans);
	    Object[] arr = s.toArray();
	    ArrayList a = new ArrayList();
	    a.addAll(s);
	    for(int i =0;i<ans;i++){
	    	System.out.print(a.get(i)+" ");
	    }

	}
}


#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 10;
long long aft[N], a[N];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long n, k;
  cin >> n >> k;
  long long ans = 0;
  for (long long i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  aft[n + 1] = n + 1;
  aft[n] = n;
  for (long long i = n - 1; i >= 1; --i) {
    if (a[i] > 1)
      aft[i] = i;
    else
      aft[i] = aft[i + 1];
  }
  long long sum;
  long long mul;
  long long pre, suf;
  for (long long i = 1; i <= n; ++i) {
    sum = 0;
    mul = 1;
    suf = i;
    pre = i - 1;
    while (suf <= n) {
      sum += a[suf];
      if ((long long)2e18 / a[suf] < mul) break;
      mul *= a[suf];
      pre = suf;
      suf = aft[suf + 1];
      if (mul % k == 0 && mul / k >= sum && mul / k <= sum + suf - pre - 1)
        ++ans;
      sum += suf - pre - 1;
    }
  }
  cout << ans << endl;
  return 0;
}

from sys import stdin
import math
# stdin = open('in')

n, k = map(int, stdin.readline().split())
a = [int(x) for x in stdin.readline().split()]
nxt = [-1]*n
pref = []
f, s = -1, 0
for i in range(n):
	s += a[i]
	pref.append(s)
	nxt[n-1-i] = f
	if a[n-1-i] != 1:
		f = n-1-i
ans = 0
for i in range(n):
	pos, cur = i, 0
	prod = 1
	while 1:
		if prod > 1e18:
			break
		prod *= a[pos]
		cur += a[pos]
		if prod == k*cur:
			ans += 1
		nt = nxt[pos]
		if nt == -1:
			ones = n-1-pos
			if k*cur < prod and k*(cur+ones) >= prod:
				ans += 1
			break
		ones = nt - pos - 1
		if k*cur < prod and k*(cur+ones) >= prod and prod%k == 0:
			ans += 1
		cur += ones
		pos = nt
print(ans)


#include <bits/stdc++.h>
using namespace std;
int res = 1e9, n, k;
int a[1000005];
int main() {
  cin >> n >> k;
  for (int i = 1; i < n; ++i) {
    cin >> a[i];
    a[i] += a[i - 1];
    if (i >= k) res = min(res, a[i] - a[i - k]);
  }
  cout << res;
  return 0;
}

w, l = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
s = res = sum(a[:l])
for i in range(l, w - 1):
    s += a[i] - a[i - l]
    res = min(res, s)
print(res)


import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner kb = new Scanner(System.in);
		long l, r, a = 1;
        l = kb.nextInt();
        r = kb.nextInt();
        for(; a <= r; a *= 10) ;
        l = Math.min(Math.max(a / 2, l), r);
        System.out.println(l * (a - l - 1));

	}

}

def ref(n):
    return int(''.join([str(9-int(x)) for x in str(n)]))
l,r=map(int,input().split())
ans=0
ans=max(ans,ref(l)*l)
ans=max(ans,ref(r)*r)
cur=5
for i in range(20):
    if(l<=cur<=r):
        ans=max(ans,ref(cur)*cur)
    cur*=10
print(ans)


// package CodeForces;

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

public class Problem_68A {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner();
		PrintWriter pw = new PrintWriter(System.out);
		int min=sc.nextInt();
		for(int i=0;i<3;i++)
			min=Math.min(min, sc.nextInt());
		int start=sc.nextInt();
		int end=sc.nextInt();
		if(min>start)
		{
			if(min>end)
				pw.println(end-start+1);
			else
				pw.println(min-start);
		}
		else
			pw.print(0);
		pw.close();
	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

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

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

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

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

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

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

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

	}
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  int p[4], p0[4], a, b;
  cin >> p[0] >> p[1] >> p[2] >> p[3] >> a >> b;
  sort(p, p + 4);
  int i;
  int ans = 0;
  for (i = a; i <= b; i++) {
    int s = 0;
    memcpy(p0, p, sizeof(p));
    do {
      if (i % p0[0] % p0[1] % p0[2] % p0[3] == i) s++;
    } while (next_permutation(p0, p0 + 4));
    if (s >= 7) ans++;
  }
  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  long long int n = s.length();
  long long int a[n];
  a[0] = 1;
  for (long long int i = 1; i <= n - 1; i++) {
    a[i] = 1;
    if (s[i] == s[i - 1]) a[i] = a[i - 1] + 1;
  }
  for (long long int i = 0; i < n; i++) {
    if (a[i] == 0 || a[i] % 2 == 1)
      cout << s[i];
    else {
      int c = s[i] - 'a';
      c++;
      c %= 26;
      if (i + 1 < n && s[i + 1] == c + 'a') c += 1;
      c %= 26;
      cout << (char)(c + 'a');
    }
  }
}

aaaac aa aaaaab ab abac aaaaaab caaab aaaaaab b aaaabc c aab aaac zza aaaab aaaabc aab aac aaaaab ac aabc aaaaaac aab aaabc aaaaaabc aabc a aaabc aaaaaac acac ac aaab ab aaaaac aaaaaabc ab aac nnop aaac aaaac aaaaac abc dtottttotd abc aaaab abab zscoder bc aaab aac

anbc canab ananc anananb anbc abac ananab anananbc ananbc anananbc abc ananbc bc anc zna anananc acac anabc anc au ananb anananc anb anab zscoder anac ananac ananab ab ab ananac anab b ananc ab naop ac abab anb anc c a ac anabc ananb anananb anb abc anac dtotataotd

s=list(input())
n=len(s)
def call(a,b):
    str="anfrozeyuibiu"
    for i in str:
        if i!=a and i!=b:
            return i
def call1(a):
    str="urweifbn"
    for i in str:
        if i!=a:
            return i
for i in range(1,n):
    if s[i]==s[i-1]:
        if i==n-1:
            s[i]=call1(s[i])
        else:
            s[i]=call(s[i],s[i+1])
print(*s,sep='')


#include <bits/stdc++.h>
using namespace std;
int main() {
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    int cnt = 1;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int x = 0;
    while (a[x] != 1) {
      x++;
    }
    long long int ans = 0;
    for (int i = x; i < n; i++) {
      if (a[i] == 1) {
        if (ans == 0) ans = 1;
        ans = ans * cnt;
        cnt = 1;
      } else {
        cnt++;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}

import java.util.Scanner;

public class Chocolate {
    static String chocolatePieces;
    static int n;

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        s.nextLine();
        chocolatePieces = s.nextLine().replaceAll(" ", "");
        System.out.println(getNumberOfWaysToBreakChocolate());

    }

    private static long getNumberOfWaysToBreakChocolate() {
        if (chocolatePieces.indexOf('0') == -1)
            return 1;
        if (chocolatePieces.indexOf('1') == chocolatePieces.lastIndexOf('1')) {
            return chocolatePieces.indexOf('1') == -1 ? 0 : 1;
        }
        long total = 1;
        long counter = 0;
        for (int i = 0; i < n; i++) {
            if (chocolatePieces.charAt(i) == '1') {
                if (chocolatePieces.lastIndexOf('1') == i) continue;
                ++counter;
                int j = i + 1;
                while (j < chocolatePieces.length() && chocolatePieces.charAt(j++) != '1') {
                    ++counter;
                }
                total *= counter;
                counter = 0;
            }
        }
        return total;

    }
}


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

public class C {

	static ArrayList<Integer>[] adjList;

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		adjList = new ArrayList[n];
		for (int i = 0; i < n; i++)
			adjList[i] = new ArrayList<Integer>();
		boolean[][] con = new boolean[n][n];
		for (int i = 0; i < m; i++) {
			int u = sc.nextInt() - 1;
			int v = sc.nextInt() - 1;
			adjList[u].add(v);
			adjList[v].add(u);
			con[u][v] = con[v][u] = true;
		}

		// i -> v , u -> v
		int min = 5000;
		for (int i = 0; i < n; i++) {
			for (int v : adjList[i]) {
				for (int u : adjList[v]) {
					if (u == v)
						continue;
					if (con[i][u]) {
						min = Math.min(adjList[i].size() + adjList[v].size() + adjList[u].size() - 6, min);
					}
				}
			}
		}
		System.out.println(min == 5000 ? -1 : min);

	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

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

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

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

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

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

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

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

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

		public int[] nextIntArray1(int n) throws IOException {
			int[] a = new int[n + 1];
			for (int i = 1; i <= n; i++)
				a[i] = nextInt();
			return a;
		}

		public int[] nextIntArraySorted(int n) throws IOException {
			int[] a = nextIntArray(n);
			Random r = new Random();
			for (int i = 0; i < n; i++) {
				int j = i + r.nextInt(n - i);
				int t = a[i];
				a[i] = a[j];
				a[j] = t;
			}
			Arrays.sort(a);
			return a;
		}

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

		public long[] nextLongArray1(int n) throws IOException {
			long[] a = new long[n + 1];
			for (int i = 1; i <= n; i++)
				a[i] = nextLong();
			return a;
		}

		public long[] nextLongArraySorted(int n) throws IOException {
			long[] a = nextLongArray(n);
			Random r = new Random();
			for (int i = 0; i < n; i++) {
				int j = i + r.nextInt(n - i);
				long t = a[i];
				a[i] = a[j];
				a[j] = t;
			}
			Arrays.sort(a);
			return a;
		}
	}
}

n, m = map(int, input().split())
amatr = [[0] * n for i in range(n)]
alist = [[] for i in range(n)]
for i in range(m):
    a, b = map(int, input().split())
    amatr[a - 1][b - 1] = 1
    amatr[b - 1][a - 1] = 1
    alist[a - 1].append(b - 1)
    alist[b - 1].append(a - 1)

known = [len(alist[i]) for i in range(n)]

ans = 10**10

for first in range(n):
    for second in alist[first]:
        if amatr[first][second] == 1:
            for third in alist[second]:
                if amatr[third][second] == 1:
                    if amatr[first][third] == 1:
                        ans = min(ans, known[first] + known[second] + known[third] - 6)
            amatr[first][second] = 0
            amatr[second][first] = 0

print(ans if ans < 10 ** 10 else -1)


import sys
# sys.stdin = open('in.txt')
R = lambda: map(int, input().split())

n = int(input())
s = input()

ca = s.count('A')
cg = s.count('G')
cc = s.count('C')
ct = s.count('T')
mx = max(ca, cg, cc, ct)

sum = (ca==mx)+(cg==mx)+(cc==mx)+(ct==mx)

ans = 1

for i in range(n):
    ans = (ans*sum)%1000000007

print(ans)

import java.math.BigInteger;
import java.util.Scanner;

public class DNA {

	public static void main(String args[]) {

		Scanner st = new Scanner(System.in);
		int n = st.nextInt();
		String s = st.next();

		if (n == 1) {
			System.out.println(1);
			return;
		}

		int count[] = new int[4];

		for (int i = 0; i < n; i++)
			if (s.charAt(i) == 'A')
				count[0]++;
			else if (s.charAt(i) == 'C')
				count[1]++;
			else if (s.charAt(i) == 'G')
				count[2]++;
			else if (s.charAt(i) == 'T')
				count[3]++;

		int w = 0;

		if (count[0] == 0)
			w++;
		if (count[1] == 0)
			w++;
		if (count[2] == 0)
			w++;
		if (count[3] == 0)
			w++;

		if (w == 3) {
			System.out.println(1);
			return;
		}

		int main = Math.max(count[3],
				Math.max(count[2], Math.max(count[1], count[0])));

		int how = 0;

		if (main == count[0])
			how++;

		if (main == count[1])
			how++;

		if (main == count[2])
			how++;

		if (main == count[3])
			how++;

		BigInteger b1 = new BigInteger(how + "");
		BigInteger b2 = new BigInteger(s.length() + "");
		BigInteger b3 = new BigInteger(1000000007 + "");

		BigInteger br = b1.modPow(b2, b3);

		System.out.println(br.toString());

		st.close();
	}
}


#include <bits/stdc++.h>
using namespace std;
string s[105];
bool mark_row[105];
int n, m;
void ngk() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> s[i], s[i] = '0' + s[i] + '0';
  int ans = 0;
  for (int j = 1; j <= m; j++) {
    bool bruh = 0;
    for (int i = 1; i < n; i++) {
      if (!mark_row[i] && s[i][j] > s[i + 1][j]) {
        bruh = 1;
        break;
      }
    }
    if (bruh)
      ans++;
    else {
      for (int i = 1; i < n; i++)
        if (s[i][j] < s[i + 1][j]) mark_row[i] = 1;
    }
  }
  cout << ans;
}
int main(int argc, char *argv[]) {
  iostream::sync_with_stdio(0);
  int test = 1;
  while (test--) {
    ngk();
  }
  return 0;
}

from math import *
from collections import *
from random import *
from bisect import *
import sys
input=sys.stdin.readline
def lis():
    return list(map(int,input().split()))
def ma():
    return map(int,input().split())
def inp():
    return int(input())
def fk(a,s,k):
    b=[]
    for i in range(len(a)):
        b.append(a[i]+(i+1)*k)
    b.sort()
    return sum(b[:k])
n,m=ma()
re=0
r=[]
co=['']*n
for i in range(n):
    s=input().rstrip('\n')
    r.append(s)
for i in range(m):
    t=[co[j]+r[j][i] for j in range(n)]
    if(t==sorted(t)):
        co=t
    else:
        re+=1
print(re)


#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int N = 55;
double dp[N][N], fact[N];
int a[N];
int main() {
  int n;
  cin >> n;
  double factN = 1.0, ans = 0.0;
  dp[0][0] = 1.0;
  fact[0] = 1.0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    fact[i] = fact[i - 1] * i;
  }
  int p;
  cin >> p;
  for (int i = 1; i <= n; ++i) {
    for (int j = i; j >= 1; --j) {
      for (int k = a[i]; k <= p; ++k) {
        dp[j][k] += dp[j - 1][k - a[i]];
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= p; ++j) {
      ans += dp[i][j] * fact[i] * fact[n - i];
    }
  }
  cout << ans / fact[n];
  return 0;
}

n=int(input())
arr=list(map(int,input().split()))
p=int(input())
dp=[[[0 for k in range(n+1)] for i in range(p+1)] for i in range(n+1)]
for j in range(p+1):
    for k in range(n+1):
        dp[0][j][k]=1
for i in range(1,n+1):
    for j in range(p+1):
        for k in range(1,n+1):
            if j>=arr[k-1]:
                dp[i][j][k]=dp[i][j][k-1]+i*dp[i-1][j-arr[k-1]][k-1]
            else:
                dp[i][j][k]=dp[i][j][k-1]
fact=n
ans=0
for i in range(1,n+1):
    ans+=dp[i][p][n]/fact
    fact*=(n-i)
print(ans)


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 9;
int n, m;
long long pot[maxn];
bool test() {
  long long p = 1;
  for (int i = 1; i <= m; ++i) {
    p *= 2LL;
    if (p > n) return true;
  }
  return false;
}
int main() {
  cin >> n >> m;
  if (!test()) {
    cout << 0 << '\n';
    return 0;
  }
  long long ans = 1LL;
  pot[0] = 1LL;
  for (int i = 1; i <= m; ++i) pot[i] = (pot[i - 1] * 2LL) % mod;
  for (int i = 1; i <= n; ++i) {
    long long add = (pot[m] - i + mod) % mod;
    ans = (ans * add) % mod;
  }
  cout << ans << '\n';
  return 0;
}

//package contest;

import java.io.*;

public class ProblemA {
    static final int mod = 1000 * 1000 * 1000 + 9;
    static int mult(int a, int b) {
        return (int)((long)a * b % mod);
    }
    static int sum(int a, int b) {
        return (a + b) % mod;
    }
    int solve(int n, int m) {
        int x = 1;
        for(int i = 0; i < m; i++) {
            x = mult(x, 2);
        }
        x = sum(x, mod - 1);
        int ans = 1;
        for(int i = 0; i < n; i++) {
            ans = mult(ans, x);
            x = sum(x, mod - 1);
        }
        return ans;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String line = reader.readLine();
        String []s = line.split(" ");
        int n = Integer.parseInt(s[0]);
        int m = Integer.parseInt(s[1]);
        int ans = new ProblemA().solve(n, m);
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        writer.write(Integer.toString(ans));
        writer.write("\n");
        writer.flush();
    }
}


n,m = map(int,input().split())
a = 0
for i in range(0, 1000):
    for j in range(0, 1000):
        if i * i + j == n and j * j + i == m:
            a = a + 1
print(a)

#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 5;
const long long mod = 1e9 + 7;
const long long inf = 3e18 + 5;
const double eps = 1e-8;
const double pi = acos(-1);
void test() {
  long long n, m;
  cin >> n >> m;
  set<pair<int, int> > s;
  for (int i = 0; i < (1001); i++) {
    if (i * i > n) break;
    for (int j = 0; j < (1001); j++) {
      if (j * j > m || i * i + j > n) break;
      if (i * i + j == n && (j * j) + i == m) s.insert({i, j});
    }
  }
  cout << s.size() << endl;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int cases = 1, c = 1;
  while (cases--) {
    test();
  }
}


import java.util.Scanner;

public class A {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        if (n == 0 && m != 0)
            System.out.println("Impossible");
        else {
            int min = n + Math.max(0, m - n);
            int max = n + Math.max(0, m - 1);
            System.out.println(min + " " + max);
        }
    }
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  if (n == 0 && m != 0) {
    cout << "Impossible";
    return 0;
  }
  cout << n + max(m - n, 0) << ' ' << n + m - (m == 0 ? 0 : 1);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 7;
const int N = 5e4 + 10;
int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); }
int main() {
  int a, b, x, y;
  cin >> a >> b >> x >> y;
  int g = gcd(x, y);
  x /= g;
  y /= g;
  int ratio = min(a / x, b / y);
  cout << x * ratio << ' ' << y * ratio << endl;
  return 0;
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
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);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int a = in.readInt();
            int b = in.readInt();
            int x = in.readInt();
            int y = in.readInt();
            int gcd = (int) (MathUtil.gcd(x, y));
            x /= gcd;
            y /= gcd;

            if (x > a || y > b) {
                out.println("0 0");
                return;
            }

            long low = 1, high = a / x;

            while (low < high) {
                long mid = (low + high + 1) / 2;
                if (mid * x > a || mid * y > b) {
                    high = mid - 1;
                } else low = mid;
            }
            out.println((x * low) + " " + (y * low));
        }

    }

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

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

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

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

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

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

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

        }

    }

    static class MathUtil {
        public static long gcd(long a, long b) {
            while (b > 0) {
                long t = a % b;
                a = b;
                b = t;
            }
            return a;
        }

    }
}


#include <bits/stdc++.h>
const int maxn = 5e5 + 10;
using namespace std;
int n;
int l, r, k, gap;
long long a[maxn], x, pre[maxn], tmp;
bool f;
int main() {
  scanf("%d", &n);
  l = ceil(1.0 * n / 2);
  r = n - l;
  for (int i = 1; i <= l; i++) scanf("%lld", &a[i]), pre[i] = pre[i - 1] + a[i];
  scanf("%lld", &x);
  for (int i = l + 1; i <= n; i++) pre[i] = pre[i - 1] + x;
  if (x >= 0) {
    if (pre[n] <= 0)
      printf("-1\n");
    else
      printf("%d\n", n);
  } else {
    long long Min = 1e18;
    for (int i = 0; i < l; i++) {
      long long tmp = pre[n] - pre[i];
      if (Min == 1e18)
        Min = tmp;
      else {
        Min -= x;
        Min = min(Min, tmp);
      }
      if (Min > 0) {
        printf("%d\n", n - i);
        return 0;
      }
    }
    printf("-1\n");
  }
  return 0;
}

import java.util.*;
import java.io.*;
public class Main{
	public static void main(String args[]){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		long a[]=new long[n+1];
		for(int i=1;i<=(n+1)/2;i++)a[i]=sc.nextLong();
		long x=sc.nextLong();
		for(int i=(n+1)/2+1;i<=n;i++)a[i]=x;
		long pre[]=new long[n+1];
		long pmax[]=new long[n+1];
		pmax[0]=Integer.MIN_VALUE;
		for(int i=1;i<=n;i++){
			pre[i]=pre[i-1]+a[i];
			pmax[i]=Math.max(pmax[i-1],pre[i]-i*x);
		}
		for(int i=(n+1)/2;i<=n;i++){
			if(pre[i]>0&&pre[i]>pmax[n-i]){
				System.out.println(i);
				return ;
			}
		}
		System.out.println(-1);
		return;
	}
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 3000 + 50;
vector<int> edge[maxn];
int sub[maxn][maxn], fa[maxn][maxn];
long long n, ans, dp[maxn][maxn];
void dfs(int root, int k, int dad) {
  sub[root][k]++;
  fa[root][k] = dad;
  for (auto t : edge[k]) {
    if (t != dad) {
      dfs(root, t, k);
      sub[root][k] += sub[root][t];
    }
  }
}
long long DP(int a, int b) {
  if (a == b) return 0;
  if (dp[a][b] != -1) return dp[a][b];
  return dp[a][b] =
             sub[a][b] * sub[b][a] + max(DP(fa[a][b], a), DP(fa[b][a], b));
}
int main() {
  cin >> n;
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) dfs(i, i, -1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (i != j) ans = max(ans, DP(i, j));
  cout << ans << endl;
}

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

public class XenonAttackOnGangs {
    static int[][] par, ss;
    static long[][] dp;
    static ArrayList<Integer>[] graph;
    public static void main(String[] args) {
        FastScanner scanner = new FastScanner();
        PrintWriter out = new PrintWriter(System.out,false);
        int n = scanner.nextInt();
        par = new int[n][n];
        ss = new int[n][n];
        dp = new long[n][n];
        graph = new ArrayList[n];
        for(int i = 0; i < n; i++) {
            graph[i] = new ArrayList<>();
        }
        for(long[] dd: dp) Arrays.fill(dd, -1);
        int[][] edges = new int[n][2];
        for(int i =1; i < n; i++) {
            int a = scanner.nextInt()-1;
            int b = scanner.nextInt()-1;
            graph[a].add(b);
            graph[b].add(a);
            edges[i][0] = a; edges[i][1] = b;
        }
        for(int i = 0; i < n; i++) { dfs(i,i,i); }
        for(int i = 1; i < n; i++) {
            dp[edges[i][0]][edges[i][1]] = dp[edges[i][1]][edges[i][0]] = ss[edges[i][0]][edges[i][1]] * ss[edges[i][1]][edges[i][0]];
        }
        long best = 0;
        for(int i = 0; i < n; i++) {
            for(int j = i + 1; j < n; j++) {
                best = Math.max(best, dp(i,j));
            }
        }
        out.println(best);
        out.flush();
    }
    static long dp(int i, int j) {
        if (dp[i][j] == -1) {
            //all paths that go from each subtree to the other
            //defined respective the direction of the other node
            //dp is to either put the edge from v to par(u, v) and extend that subproblem
            //or from u to par(v,u);
            dp[i][j] = dp[j][i] = ss[i][j] * ss[j][i] + Math.max(dp(par[i][j], i), dp(par[j][i], j));
        }
        return dp[i][j];
    }

    static void dfs(int v, int p, int i) {
        ss[i][v] = 1;
        for(int edge: graph[v]) {
            if (edge == p) continue;
            par[i][edge] = v;
            dfs(edge, v,i);
            ss[i][v] += ss[i][edge];
        }
    }
    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }
        public FastScanner() {
            this(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


lucky_numbers = [4, 7, 44, 77, 47, 74, 444, 447, 474, 744, 777, 774, 747, 477]
n = input()
lucky = True
for i in range(0, len(n)):
    if n[i] != '4' and n[i] != '7' :
        lucky = False
        break

if lucky:
    print("YES")
else:
    lucky = False
    for i in lucky_numbers:
        if int(n) % i == 0:
            lucky = True
            break
    if lucky:
        print("YES")
    else:
        print("NO")

NO YES NO YES NO YES NO NO NO YES YES NO NO YES YES YES NO YES NO YES NO YES YES NO YES YES NO NO YES YES YES NO NO NO YES NO NO NO YES YES NO NO YES NO NO YES NO NO NO NO NO YES


n, q = map(int, input().split())
a    = list(map(int, input().split()))
d    =  {}

def max_frequent(s, e, a):
    d = {}
    for x in a[s: e+1]:
        if x not in d:
            d[x] = 0
        d[x] += 1

    return e - s + 1 - max(list(d.values()))

for i, x in enumerate(a):
    if x not in d:
        d[x] = []
    d[x].append(i)

segment = [[v[0], v[-1]] for v in d.values()]
end     = -1
start   = -1
block   = []

for s, e in segment:
    if s > end:
        if end != -1:
            block.append([start, end])
        start = s
        end   = e
    if e > end:
        end = e

block.append([start, end])

cnt = 0
for s, e in block:
    cnt += max_frequent(s, e, a)
print(cnt)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

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

    static class G1NaBlokiUproshennayaVersiya {
        int[] cnt;

        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            in.nextInt();
            int[] a = new int[n];
            int m = 0;
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt() - 1;
                m = Math.max(m, a[i] + 1);
            }
            cnt = new int[m];
            for (int i = 0; i < n; i++) {
                cnt[a[i]]++;
            }
            DSU dsu = new DSU(m);

            int[] s = new int[n];
            int sp = -1;
            boolean[] onStack = new boolean[m];
            for (int i = 0; i < n; i++) {
                int v = dsu.get(a[i]);
                if (sp == -1 || !onStack[v]) {
                    sp++;
                    s[sp] = v;
                    onStack[v] = true;
                } else {
                    while (dsu.get(s[sp]) != dsu.get(v)) {
                        dsu.unite(s[sp], v);
                        sp--;
                    }
                    v = dsu.get(v);
                    onStack[v] = true;
                }
            }

            int[] max = new int[m];
            for (int i = 0; i < n; i++) {
                int v = dsu.get(a[i]);
                max[v] = Math.max(max[v], cnt[a[i]]);
            }

            int ans = n;
            for (int i = 0; i < m; i++) {
                ans -= max[i];
            }
            out.println(ans);
        }

        class DSU {
            int n;
            int[] p;
            int[] r;

            public DSU(int n) {
                this.n = n;
                p = new int[n];
                r = new int[n];
                for (int i = 0; i < n; i++) {
                    p[i] = i;
                }
            }

            int get(int v) {
                if (p[v] != v) {
                    p[v] = get(p[v]);
                }
                return p[v];
            }

            void unite(int x, int y) {
                x = get(x);
                y = get(y);
                if (r[x] == r[y]) {
                    r[x]++;
                }
                if (r[x] > r[y]) {
                    p[y] = x;
                } else {
                    p[x] = y;
                }
            }

        }

    }
}


#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long a, b, c;
  cin >> a >> b >> c;
  if (a > (b + c))
    cout << '+';
  else if ((a + c) < b)
    cout << '-';
  else {
    if (a == b && c == 0)
      cout << 0;
    else
      cout << '?';
  }
  cout << endl;
}

x, y, z = [int(x) for x in input().split()]

if x == y and z == 0:
    print(0)
elif x > y + z:
    print("+")
elif y > x + z:
    print("-")
else:
    print("?")


k = int(input())
cSum = 0
res = [-1,1]
while True:
    if cSum + 1_000_000 < k + len(res):
        cSum += 1_000_000
        res.append(1_000_000)
    else:
        res.append(k + len(res) - cSum)
        break
print(len(res))
for i in res:
    print(i, end = ' ')

#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int k;
  cin >> k;
  int goal = k + 2, S = 0;
  vector<int> ans;
  ans.push_back(-1);
  while (1) {
    if (S + MAX >= goal) {
      ans.push_back(goal - S);
      break;
    }
    ans.push_back(MAX);
    S += MAX;
    goal++;
  }
  cout << ans.size() << "\n";
  for (auto x : ans) cout << x << " ";
}


#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    int q;
    scanf("%d", &q);
    if (q == 0) {
      printf("1 1\n");
      continue;
    }
    int n = 0, m = 0, ch = 0, x = 1;
    for (int i = 1; i <= sqrt(q); i++) {
      if (q % i) continue;
      int pp = q / i;
      if ((pp + i) % 2 == 0) {
        n = (pp + i) / 2;
        m = n - i;
        for (int i = 1; i <= n; i++) {
          if (n / i == m) {
            m = i;
            ch = 1;
            break;
          }
          if (n / i < m) break;
        }
      }
      if (ch == 1) break;
    }
    if (ch == 0)
      puts("-1");
    else {
      printf("%d %d\n", n, m);
    }
  }
}

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		for(int i=0; i<t; i++) {
			int x = sc.nextInt();
			solve(x);
		}
	}

	static void solve(int x) {
		if(x==0) {
			System.out.println("1 1");
			return;
		}
		for(int b=1; b<Math.sqrt(x)+1; b++) {
			if(x%b==0) {
				int c = x/b;
				if(c<b) continue;
				if((b+c)%2==0) {
					int n = (b+c)/2;
					int y = (c-b)/2;
					if(y==0) continue;
					for(int m = n/y; m>0; m--) {
						if(x == n*n-(n/m)*(n/m)) {
							System.out.println(n + " " + m);
							return;
						}
					}
				}
			}
		}
		System.out.println(-1);
	}
}


n,pos,l,r  = [int(i) for i in input().split()]

time_l = 0;
if l != 1:
    time_l += abs(pos - l) + 1 # move to l and delete
    pos1 = l
else:
    pos1 = pos
if r != n: time_l += abs(r-pos1) + 1 # move to r and delete

time_r = 0;
if r != n:
    time_r += abs(pos - r) + 1 # move to l and delete
    pos1 = r
else:
    pos1 = pos
if l != 1: time_r += abs(pos1-l) + 1 # move to r and delete

#print(time_l, time_r)
print(min(time_l, time_r))

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, pos, l, r;
  cin >> n >> pos >> l >> r;
  int ans;
  if (l == 1 && r == n) {
    ans = 0;
  } else if (l == 1 && r != n) {
    ans = 1 + abs(r - pos);
  } else if (l != 1 && r == n) {
    ans = 1 + abs(pos - l);
  } else {
    if (pos == l) {
      ans = 1 + r - l + 1;
    } else if (pos == r) {
      ans = 1 + r - l + 1;
    } else {
      ans = r - l + 2 + min(abs(pos - l), abs(pos - r));
    }
  }
  cout << ans << endl;
  return 0;
}


NO NO NO NO NO NO NO YES NO YES YES NO NO NO YES NO NO NO NO YES NO NO YES NO YES NO YES NO NO YES NO NO YES NO NO NO YES NO NO NO NO YES YES NO NO NO NO NO NO YES

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e6;
const int maxn = 100000 + 10;
bool vis[100];
int main() {
  int a, b, c, t1, t2;
  scanf("%d%d%d%d%d", &a, &b, &c, &t1, &t2);
  for (int i = 0; i < 10; i++)
    ;
  for (int i = 0; i < 10; i++)
    ;
  for (int i = 0; i < 10; i++)
    ;
  for (int i = 0; i < 10; i++)
    ;
  int a1 = a * 2;
  if (b || c) ++a1;
  a1 %= 24;
  int b1 = b / 5 * 2;
  if (b % 5 != 0) ++b1;
  if (b % 5 == 0 && c) ++b1;
  int c1 = c / 5 * 2;
  if (c % 5 != 0) ++c1;
  vis[a1] = vis[b1] = vis[c1] = true;
  int flag1 = 1, flag2 = 1;
  if (t1 > t2) swap(t1, t2);
  for (int i = t1 * 2; i <= t2 * 2; i++)
    if (vis[i]) flag1 = 0;
  for (int i = t2 * 2; i <= t1 * 2 + 24; i++)
    if (vis[i % 24]) flag2 = 0;
  if (flag1 || flag2)
    printf("YES\n");
  else
    printf("NO\n");
  return 0;
}


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

public class Main {

    static FastReader in = new FastReader("in.txt");
    static PrintWriter out = new PrintWriter(System.out);

    static long x, y;

    public static void main(String[] args) throws IOException {
        long a = in.nextInt(),
             b = in.nextInt(),
             c = -in.nextInt();

        long d = extendedEuclid(a, b);

        if(c%d != 0)
            out.println(-1);
        else
            out.println(x * c/d + " " + y * c/d);

        out.flush();
    }

    static long extendedEuclid(long a, long b){
        if (b == 0) { x = 1; y = 0; return a; }
        long d = extendedEuclid(b, a % b);

        long x1 = y;
        long y1 = x - (a / b) * y;
        x = x1; y = y1;

        return d;
    }

    static class FastReader {

        BufferedReader br;
        StringTokenizer st;

        public FastReader(String file) {
            try {
                br = new BufferedReader(new FileReader(file));
            } catch (FileNotFoundException e) {
                br = new BufferedReader(new InputStreamReader(System.in));
            }
        }

        String next() {

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

            return st.nextToken();
        }

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

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

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

        String nextLine() {

            String str = "";

            try {
                str = br.readLine();
            } catch (IOException e) { e.printStackTrace(); }

            return str;
        }
    }
}

#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b, long long& x, long long& y) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  }
  long long ans = gcd(b, a % b, x, y);
  int temp = x;
  x = y;
  y = temp - a / b * y;
  return ans;
}
int main() {
  long long a, b, c, d, x, y;
  cin >> a >> b >> c;
  d = gcd(a, b, x, y);
  if (c % d != 0)
    cout << -1 << endl;
  else
    cout << -x * (c / d) << " " << -y * (c / d) << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, k = 1;
  cin >> n;
  if (n == 1) {
    cout << "1\n1";
  } else if (n == 2) {
    cout << "1\n1 1";
  } else {
    vector<bool> isprime(n + 2, true);
    vector<long long int> A(n + 2, 1);
    for (int i = 2; i <= n + 1; i++) {
      if (isprime[i]) {
        for (int j = 2 * i; j <= n + 1; j = j + i) {
          isprime[j] = false;
        }
      }
    }
    cout << 2 << "\n";
    for (int i = 2; i <= n + 1; i++) {
      if (isprime[i])
        cout << 1 << " ";
      else
        cout << 2 << " ";
    }
  }
  return 0;
}

import java.util.Scanner;

public class PracticeDemo {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		if(n <= 2) System.out.println(1);
		else System.out.println(2);
		sc.close();
		int i = 0,j =0;
		boolean flag =true;
		for( i  = 2; i <= n + 1; i++ ){
			if(i==2){
				System.out.print(1+" ");
				continue;
			}

			flag =true;
			for(j = 2; j <=  (Math.sqrt(i)+1);j++)
				if( i % j == 0)
					flag = false;
				if(flag)
					System.out.print( 1 + " ");
				else
					System.out.print( 2 +" ");
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
int judge(int n) {
  int flag = 0;
  for (int i = 2; i <= n / 2; i++) {
    if (n % i == 0) {
      flag = 1;
      break;
    }
  }
  if (flag)
    return 0;
  else
    return 1;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= 1000; i++) {
    int sum = i * n + 1;
    if (judge(sum) == 0) {
      printf("%d\n", i);
      return 0;
    }
  }
  return 0;
}

import sys

n = int(input())

for m in range(1, 1001):
        for j in range(2, 1001):
            if j < (n * m + 1) and (n * m + 1) % j == 0:
                print(m)
                sys.exit(0)


// package codeforces.cf3xx.cf347.div1;

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

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

        int n = in.nextInt();
        in.nextLine();
        for (int i = 0; i < n ; i++) {
            String suffix = in.nextLine().split("'")[1];
            long Y = Long.valueOf(suffix);
            int l = suffix.length();
            long pw = (long)Math.pow(10L, l);
            long pk = 0;
            for (int j = 1 ; j < l; j++) {
                pk += Math.pow(10L, j);
            }
            while (Y < 1989+pk) {
                Y += pw;
            }
            out.println(Y);
        }
        out.flush();
    }

    static void debug(Object... o) {
        System.err.println(Arrays.deepToString(o));
    }
}


#include <bits/stdc++.h>
using namespace std;
vector<int> G[100001];
long long L[100001];
long long ans = 0;
int dfs(int a) {
  if (L[a] != 0) return L[a];
  int resp = 0;
  for (int x : G[a]) {
    if (a > x) {
      resp = max(dfs(x), resp);
    }
  }
  return L[a] = resp + 1;
}
int main() {
  int n, m, aux, t, a, b;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &a, &b);
    G[a].push_back(b);
    G[b].push_back(a);
  }
  ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, (long long)dfs(i) * (long long)G[i].size());
  }
  cout << ans << endl;
  return 0;
}

from sys import stdin,stdout,setrecursionlimit
setrecursionlimit(10**5)
from collections import defaultdict
nmbr = lambda: int(stdin.readline())
lst = lambda: list(map(int,stdin.readline().split()))
PI=float('inf')
def dfs(src):
    vis[src]=1
    for neigh in g[src]:
        if neigh<src:
            if not vis[neigh]:dfs(neigh)
            dp[src]=max(dp[src],1+dp[neigh])
for _ in range(1):#nmbr()):
    n,eg=lst()
    g=defaultdict(list)
    for i in range(eg):
        u,v=lst()
        g[u]+=[v]
        g[v]+=[u]
    dp=[1]*(1+n)
    vis=[0]*(1+n)
    for node in range(1,n+1):
        if not vis[node]:dfs(node)
    print(max(len(g[node])*dp[node] for node in range(1,n+1)))


na,nb=map(int,input().split())
k,m=map(int,input().split())
a=[int(z)for z in input().split()]
b=[int(z)for z in input().split()]
print("YNEOS"[a[k-1]>=b[-m]::2])
# Interesting. --YKF

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = sc.nextInt();
        int p = sc.nextInt(), k = sc.nextInt();;

        int a[] = new int[n];
        for(int i = 0; i < n; ++i)
            a[i] = sc.nextInt();
        int b[] = new int[m];
        for(int i = 0; i < m; ++i)
            b[i] = sc.nextInt();

        System.out.println((a[p-1] < b[m-k]) ? "YES" : "NO");
    }
}


import java.util.*;
import java.io.*;
public class equiv {

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=Integer.parseInt(br.readLine());
		StringTokenizer st=new StringTokenizer(br.readLine());
		PriorityQueue<Integer>a=new PriorityQueue<Integer>(n);
		while(n-->0)
			a.add(Integer.parseInt(st.nextToken()));
		int sum=0;
		int happ=0;
		while(!a.isEmpty()){
			int x=a.poll();
			if(sum<=x)
			{
				happ++;
				sum+=x;
			}
		}
		System.out.println(happ);

	}

}

#include <bits/stdc++.h>
using namespace std;
long long n, a[100007], d, t;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  sort(a + 1, a + (n + 1));
  for (int i = 1; i <= n; i++) {
    if (t <= a[i]) {
      d++;
      t += a[i];
    }
  }
  cout << d;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
void ga(int N, int *A) {
  for (int i(0); i < N; i++) scanf("%d", A + i);
}
int N, M, a, b, C[128];
int main(void) {
  scanf("%d%d", &N, &M);
  for (int i(0); i < M; i++) {
    scanf("%d%d", &a, &b);
    for (int k(--a); k < b; k++) ++C[k];
  }
  for (int i(0); i < N; i++)
    if (C[i] ^ 1) return printf("%d %d\n", i + 1, C[i]), 0;
  puts("OK");
  return 0;
}

n, m = map(int, input().split())

c = [0] * n

for i in range(m):

    a, b = map(int, input().split())

    for j in range(a - 1, b):

        c[j] += 1

for i in range(n):

    if c[i] != 1:

        print(i + 1, c[i])

        exit()

print('OK')

# Made By Mostafa_Khaled