import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.stream.IntStream;
import java.util.Arrays;
import java.util.Set;
import java.util.HashMap;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Map;
import java.io.BufferedReader;
import java.io.InputStream;

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

    static class AStoloviePribori {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt(), k = in.nextInt();
            int[] a = in.nextArr(n);

            Map<Integer, Integer> multiSet = GeekMath.multiSet(a);

            int amountIn = multiSet.keySet().size();
            int max = Integer.MIN_VALUE;
            for (Integer integer : multiSet.keySet()) {
                max = Math.max(max, multiSet.get(integer));
            }
            int cou = GeekMath.ceiling(max, k);

            out.println(cou * amountIn * k - n);

        }

    }

    static class GeekMath {
        public static Map<Integer, Integer> multiSet(int[] arr) {
            Map<Integer, Integer> co = new HashMap<>();
            for (int i : arr) {
                co.merge(i, 1, Integer::sum);
            }
            return co;
        }

        public static int ceiling(int n, int k) {
            return (n + k - 1) / k;
        }

    }

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

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

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

        public int[] nextArr(int size) {
            return Arrays.stream(new int[size]).map(c -> nextInt()).toArray();
        }

    }
}


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

// I hate this problem
public class CF1062F {
	class Graph {
		int n, cntfwd[], cntrev[], adj[][], rev[][];
		Deque<Integer> q;
		boolean[] visited;
		public Graph(int nn, int[][] edge) {
			n = nn;
			adj = new int[n][];
			rev = new int[n][];
			cntfwd = new int[n];
			cntrev = new int[n];
			for(int i = 0 ; i < edge[0].length ; i++) {
				cntfwd[edge[0][i]]++;
				cntrev[edge[1][i]]++;
			}
			for(int i = 0 ; i < n ; i++) {
				adj[i] = new int[cntfwd[i]];
				rev[i] = new int[cntrev[i]];
			}
			for(int i = 0 ; i < edge[0].length ; i++)
				add(edge[0][i], edge[1][i]);
			q = new ArrayDeque<>();
			visited = new boolean[n];
		}
		void add(int u, int v) {
			adj[u][--cntfwd[u]] = v;
			rev[v][--cntrev[v]] = u;
		}
		int solve() {

			int res = 0;

			/*

			All important nodes must lie on the longest path.

			Let u be an important node.
			Let path P = p[1] -> p[2] -> p[3] -> ... -> p[k]
			Assume P is the longest path and u is not on P.
			One of three cases will happen with u and P:

				1. u has a path to all p[i]. Because of this, the path Q can be constructed.
				   Q = u -> p[1] -> p[2] -> p[3] -> ... -> p[k].
				   Q is a longer path than P.

				2. All p[i] has a path to u. Because of this, the path Q can be constructed.
				   Q = p[1] -> p[2] -> p[3] -> ... -> p[k] -> u.
				   Q is a longer path than P.

				3. There is some p[i] that u does not have a path to and p[i + 1] that u has a path to.
				   P can be partitioned into two pieces, p[1, i] and p[i + 1, k]. The path Q can be
				   constructed from these partitions, where u can be placed between them.
				   Q = p[1] -> p[2] -> p[3] -> ... -> p[i] -> u -> p[i + 1] -> ... -> p[k]
				   Q is a longer path than P.

			P is not the longest path in all cases. If there is a path that does not contain an important node,
			a longer path can always be constructed using the important node.

			Therefore, all important nodes must lie on the longest path.

			 */
			dp = new int[n];
			path = new int[n];
			Arrays.fill(dp, -1);
			Arrays.fill(path, -1);
			for(int i = 0 ; i < n ; i++)
				go(i);
			int maxpath = 0;
			int maxpathnode = -1;
			for(int i = 0 ; i < n ; i++) {
				if(dp[i] > maxpath) {
					maxpath = dp[i];
					maxpathnode = i;
				}
			}

			int[] p = new int[dp[maxpathnode] + 1];
			{
				int ind = 0;
				int u = maxpathnode;
				while(u != -1) {
					p[ind++] = u;
					u = path[u];
				}
			}

			/*

			To count all important nodes, we need to find the in[u] and out[u] for all nodes
			along P. in[u] will be the number of nodes that have a path to u, and out[u] will
			be the number of nodes u has a path to.

			for each i in P[1 : k]:
				Let u = p[i].

				BFS from u using the reverse edges of the graph, all nodes that u can reach using
				the reverse edges means that all those nodes can reach u using the normal edges.
				Only visit each node once, and keep count of the reachable nodes. Don't visit
				nodes on P.

				in[u] += reachableNodes.

				After that, add in the nodes from in[p[i - 1]] if u is not the first node in the path.
				Add an extra 1 to include the previous node as it reaches u.

				if i > 0: in[u] += in[p[i - 1]] + 1

			for each i in P[k : 1]:
				Let u = p[i].

				BFS from u using the normal edges of the graph. Only visit each node once, and keep
				count of the reachable nodes. Don't visit nodes on P.

				out[u] += reachableNodes.

				After that, add in the nodes of out[p[i + 1]] if u is not the last node in the path.
				Add an extra 1 to include the next node as reachable.

				if i < len(p): out[u] += out[p[i + 1]] + 1

			for each i in P[1 : k]
				Let u = p[i].

				If in[u] + out[u] is equal to n - 1, then u is an important node.
				If in[u] + out[u] is equal to n - 2, then u is a semi-important node.

			 */

			int[] in = new int[n];
			int[] out = new int[n];

			for(int i = 0 ; i < p.length ; i++)
				visited[p[i]] = true;
			for(int i = 0 ; i < p.length ; i++) {
				int reachableNodes = 0;
				q.add(p[i]);
				while(!q.isEmpty()) {
					int u = q.pop();
					for(int v : rev[u]) {
						if(!visited[v]) {
							visited[v] = true;
							q.add(v);
							reachableNodes++;
						}
					}
				}
				in[p[i]] += reachableNodes;
				if(i > 0) in[p[i]] += in[p[i - 1]] + 1;
			}
			Arrays.fill(visited, false);
			for(int i = 0 ; i < p.length ; i++)
				visited[p[i]] = true;
			for(int i = p.length - 1 ; i >= 0 ; i--) {
				int reachableNodes = 0;
				q.add(p[i]);
				while(!q.isEmpty()) {
					int u = q.pop();
					for(int v : adj[u]) {
						if(!visited[v]) {
							visited[v] = true;
							q.add(v);
							reachableNodes++;
						}
					}
				}
				out[p[i]] += reachableNodes;
				if(i < p.length - 1) out[p[i]] += out[p[i + 1]] + 1;
			}

			for(int i = 0 ; i < p.length ; i++)
				if(in[p[i]] + out[p[i]] >= n - 2)
					res++;

			/*

			All nodes along P are done, now the rest of the nodes must be dealt with.

			For all nodes u that are not in P, we need to determine whether they
			are semi-important or not. We will need to find candidates that might
			possibly be semi-important
			Let L[u] = p[i] = rightmost node from the left that can reach u
			Let R[u] = p[j] = leftmost node from the right that u can reach

			If for any u, j - i - 1 > 1, then u is not semi-important.
			Side-note* if j - i - 1 is 0, then u is on the longest path, so for u
			           to be semi-important, j - i - 1 must only be 1

			p[i] -> j - i - 1 nodes -> p[j]
			 |                          ^
			 |-----------> u -----------|

			 All the nodes on p that are between p[i] and p[j] are not reachable from u,
			 and u cannot reach any of them, so if there is more than 1 node along that path,
			 u cannot be semi-important.

			 If u does not fail this, add it to the candidates list.

			 If there are 2 candidate nodes, u and v, where L[v] = L[u]
			 (which also means R[v] = R[u]), then both u and v cannot be
			 candidates. Here are the cases:

			 Let d be the node in between p[i] and p[j].

			 p[i] -> d -> p[j]
			 | |           ^ ^
			 | |---> u ----| |
			 |               |
			 |-----> v ------|

			 In this case, d cannot be reached by u and u cannot be reached by d. This same
			 statement can be applied to v if d is replaced by v. Meaning there are at least
			 2 nodes that are both unreachable and do not reach u. Same goes for v.
			 u and v can be removed from the candidates list.

			 p[i] -> d -> p[j]
			  |            ^
			  |-> v --> u -|

			 p[i] -> d -> p[j]
			  |            ^
			  |-> u --> v -|

			 In these cases, u and v combined with P create a longer path than the already
			 established longest path, so these cases are impossible.

			 */

			int[] L = new int[n];
			int[] R = new int[n];
			Arrays.fill(L, -1);
			Arrays.fill(R, -1);
			Arrays.fill(visited, false);
			for(int i = 0 ; i < p.length ; i++)
				visited[p[i]] = true;
			for(int i = p.length - 1 ; i >= 0 ; i--) {
				q.add(p[i]);
				while(!q.isEmpty()) {
					int u = q.pop();
					for(int v : adj[u]) {
						if(!visited[v]) {
							visited[v] = true;
							L[v] = i;
							q.add(v);
						}
					}
				}
			}
			Arrays.fill(visited, false);
			for(int i = 0 ; i < p.length ; i++)
				visited[p[i]] = true;
			for(int i = 0 ; i < p.length ; i++) {
				q.add(p[i]);
				while(!q.isEmpty()) {
					int u = q.pop();
					for(int v : rev[u]) {
						if(!visited[v]) {
							visited[v] = true;
							R[v] = i;
							q.add(v);
						}
					}
				}
			}
			int[] candidate = new int[p.length];
			Arrays.fill(candidate, -1);
			boolean[] valid = new boolean[p.length];
			Arrays.fill(valid, true);
			for(int i = 0 ; i < n ; i++) {
				if(L[i] != -1 && R[i] != -1) {
					if(R[i] - L[i] - 1 == 1) {
						if(candidate[L[i]] != -1) {
							valid[L[i]] = false;
						} else {
							candidate[L[i]] = i;
						}
					}
				}
			}
			List<Integer> specialCase1 = new ArrayList<>();
			List<Integer> specialCase2 = new ArrayList<>();
			for(int i = 0 ; i < n ; i++) {
				if(L[i] == -1 && R[i] == 1)
					specialCase1.add(i);
				if(L[i] == p.length - 2 && R[i] == -1)
					specialCase2.add(i);
			}
			for(int i = 0 ; i < p.length ; i++)
				if(valid[i] && candidate[i] == -1)
					valid[i] = false;

			/*

			All candidate nodes have some p[i] -> candidate[c] -> p[i + 2]. Because of this, the
			same process can be used to calculate the in[candidate[c]] and out[candidate[c]] that
			was used on all nodes in P. It's just that instead of starting the BFS at some p[i], it
			starts at candidate[c].

			 */

			Deque<Integer> unvisit = new ArrayDeque<>();
			Arrays.fill(in, 0);
			Arrays.fill(out, 0);
			Arrays.fill(visited, false);
			for(int i = 0 ; i < p.length ; i++)
				visited[p[i]] = true;
			for(int i = 0 ; i < p.length ; i++) {
				if(i > 0 && valid[i - 1]) {
					int reachableNodes = 0;
					q.add(candidate[i - 1]);
					visited[candidate[i - 1]] = true;
					while(!q.isEmpty()) {
						int u = q.pop();
						unvisit.add(u);
						for(int v : rev[u]) {
							if(!visited[v]) {
								visited[v] = true;
								q.add(v);
								reachableNodes++;
							}
						}
					}
					in[candidate[i - 1]] += reachableNodes;
					if(i > 0) in[candidate[i - 1]] += in[p[i - 1]] + 1;
					while(!unvisit.isEmpty())
						visited[unvisit.pop()] = false;
				}
				int reachableNodes = 0;
				q.add(p[i]);
				while(!q.isEmpty()) {
					int u = q.pop();
					for(int v : rev[u]) {
						if(!visited[v]) {
							visited[v] = true;
							q.add(v);
							reachableNodes++;
						}
					}
				}
				in[p[i]] += reachableNodes;
				if(i > 0) in[p[i]] += in[p[i - 1]] + 1;
			}

			Arrays.fill(visited, false);
			for(int i = 0 ; i < p.length ; i++)
				visited[p[i]] = true;
			for(int i = p.length - 1 ; i >= 0 ; i--) {
				if(i > 0 && valid[i - 1]) {
					int reachableNodes = 0;
					q.add(candidate[i - 1]);
					visited[candidate[i - 1]] = true;
					while(!q.isEmpty()) {
						int u = q.pop();
						unvisit.add(u);
						for(int v : adj[u]) {
							if(!visited[v]) {
								visited[v] = true;
								q.add(v);
								reachableNodes++;
							}
						}
					}
					out[candidate[i - 1]] += reachableNodes;
					if(i < p.length - 1) out[candidate[i - 1]] += out[p[i + 1]] + 1;
					while(!unvisit.isEmpty())
						visited[unvisit.pop()] = false;
				}
				int reachableNodes = 0;
				q.add(p[i]);
				while(!q.isEmpty()) {
					int u = q.pop();
					for(int v : adj[u]) {
						if(!visited[v]) {
							visited[v] = true;
							q.add(v);
							reachableNodes++;
						}
					}
				}
				out[p[i]] += reachableNodes;
				if(i < p.length - 1) out[p[i]] += out[p[i + 1]] + 1;
			}

			/*

			Lastly, for each candidate node, if in[candidate[i]] + out[candidate[i]] is n - 2, then
			candidate[i] is semi-important.

			 */

			for(int i = 0 ; i < p.length ; i++)
				if(valid[i])
					if(in[candidate[i]] + out[candidate[i]] == n - 2)
						res++;

			/*

			Oh, and there are gay special cases, like this:

			p[1] -> p[2] -> ... -> p[k]
			          ^
			          |
			candidate[c]

			All nodes that are in this special case are in list specialCase1

			Ahh, and this one:

			p[1] -> ... p[k - 1] -> p[k]
			                |
			                v
			                candidate[c]

			All nodes that are in this special case are int list specialCase2

			If there is more than 1 node in either special case list, then that whole
			special case list cannot work.

			 */

			res += specialCaseCheck(specialCase1) + specialCaseCheck(specialCase2);

			return res;
		}
		int specialCaseCheck(List<Integer> specialCases) {
			if(specialCases.size() != 1)
				return 0;
			int sc = specialCases.get(0);
			Arrays.fill(visited, false);
			visited[sc] = true;
			q.add(sc);
			int incnt = 0, outcnt = 0;
			while(!q.isEmpty()) {
				int u = q.pop();
				for(int v : adj[u]) {
					if(!visited[v]) {
						visited[v] = true;
						q.add(v);
						outcnt++;
					}
				}
			}
			Arrays.fill(visited, false);
			visited[sc] = true;
			q.add(sc);
			while(!q.isEmpty()) {
				int u = q.pop();
				for(int v : rev[u]) {
					if(!visited[v]) {
						visited[v] = true;
						q.add(v);
						incnt++;
					}
				}
			}
			if(incnt + outcnt == n - 2)
				return 1;
			return 0;
		}
		int[] dp, path;
		int go(int u) {
			if(dp[u] != -1) return dp[u];
			int res = 0;
			for(int v : adj[u]) {
				int tmp = go(v) + 1;
				if(tmp > res) {
					res = tmp;
					path[u] = v;
				}
			}
			dp[u] = res;
			return res;
		}
	}
	public CF1062F() {
		FS scan = new FS();
		int n = scan.nextInt(), e = scan.nextInt();
		int[][] edge = new int[2][e];
		for(int i = 0 ; i < e ; i++) {
			edge[0][i] = scan.nextInt() - 1;
			edge[1][i] = scan.nextInt() - 1;
		}
		Graph g = new Graph(n, edge);
		System.out.println(g.solve());
	}
	class FS {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");
		public String next() {
			while(!st.hasMoreTokens()) {
				try { st = new StringTokenizer(br.readLine()); }
				catch(Exception e) { e.printStackTrace(); }
			}
			return st.nextToken();
		}
		public int nextInt() { return Integer.parseInt(next()); }
	}
	public static void main(String[] args) { new CF1062F(); }
}
/*

10 10
1 2
2 3
4 2
4 3
5 4
6 4
3 7
7 8
7 9
7 10
  0 -> 1
  1 -> 2
  3 -> 1
  3 -> 2
  4 -> 3
  5 -> 3
  2 -> 6
  6 -> 7
  6 -> 8
  6 -> 9

 */


import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Scanner;

public class CF_Edu_108_F {
	static boolean verb=true;
	static void log(Object X){if (verb) System.err.println(X);}
	static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}}
	static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}}
	static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}}
	static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}}

	static InputReader reader;

	static class Composite {
		int msk;
		int cost;

		Composite left;
		Composite right;

	}

	static String name(int w) {
		int it=0;
		String s="key:[";
		while (w>0) {
			if ((w&1)!=0) {
				s+=(it+1)+" ";
			}
			w>>=1;
			it++;
		}
		return s+"]";
	}

	static	int FX=Integer.MAX_VALUE;
	static int solveAlt(int[] a,int[] b,int[][] c) {
		int n=a.length;
		int m=b.length;
		int MX=1<<m;

		int tot=0;
		for (int i=0;i<n;i++)
			tot+=a[i];
		int bob=0;
		for (int j=0;j<m;j++)
			bob+=b[j];
		if (tot>bob)
			return -1;

		int[][] cost=new int[n][MX];
		int[] bill=new int[MX];

		for (int w=0;w<MX;w++)
			for (int j=0;j<m;j++)
				if (((1<<j)&w)!=0) {
					bill[w]+=b[j];
					for (int i=0;i<n;i++)
						cost[i][w]+=c[i][j];
				}

		int[] stack=new int[n];
		int[] ptr=new int[n];
		Arrays.fill(ptr, 1);
		int best=FX;
		int st=0;
		int[] curspend=new int[n];

		while (st>=0) {
			if (ptr[st]<MX) {
				int w=ptr[st]++;
				int spend=0;
				if (st>0)
					spend=curspend[st-1];
				//log("trying :"+name(w)+" at pos:"+st);
				//log((bill[w]-a[st])+"  "+(spend+cost[st][w])+" vs "+best);
				if (bill[w]>=a[st] && spend+cost[st][w]<best) {
					// now check all the previous ones
					int TX=1<<st;
					boolean ok=true;
					for (int z=1;z<TX;z++) {
						int gain=a[st];
						int msk=w;
						for (int j=0;j<st;j++) {
							if ((z&(1<<j))!=0) {
								msk|=stack[j];
								gain+=a[j];
							}
						}
						if (gain>bill[msk]) {
							ok=false;
							break;
						}

					}
					if (ok) {
						spend+=cost[st][w];
						curspend[st]=spend;
						if (st==n-1) {
							//log("this is ok");
							stack[st]=w;
							//for (int x:stack) {
							//	log(name(x));
							//}
							best=spend;
						} else {
							stack[st++]=w;
						}
					}

				}
			} else {
				ptr[st]=1;
				st--;
			}
		}

		if (best==FX)
			best=-1;
		return best;

	}
	static int solveKO(int[] a,int[] b,int[][] c) {
		int n=a.length;
		int m=b.length;
		int MX=1<<m;

		int[][] cost=new int[n][MX];
		int[] bill=new int[MX];

		for (int w=0;w<MX;w++)
			for (int j=0;j<m;j++)
				if (((1<<j)&w)!=0) {
					bill[w]+=b[j];
					for (int i=0;i<n;i++)
						cost[i][w]+=c[i][j];
				}

		int VX=m*4+1;
		int[][] spend=new int[MX][VX];

		for (int w=0;w<MX;w++) {
			Arrays.fill(spend[w], FX);
			if (bill[w]>=a[0]) {
				// ok for me
				// now we look at the previous
				spend[w][bill[w]-a[0]]=cost[0][w];
				//log("adding:"+w);
			}
		}

		// this is taking some shortcut...

		for (int i=1;i<n;i++) {
			int[][] nxt=new int[MX][VX];
			for (int w=0;w<MX;w++)
				Arrays.fill(nxt[w],FX);
			for (int w=0;w<MX;w++) {
				if (bill[w]>=a[i]) {
					//log("considering:"+w);
					// ok for me
					// now we look at the previous
					for (int u=0;u<MX;u++)
						for (int v=0;v<VX;v++) {
							if (spend[u][v]<FX) {
								//log("----interested by u:"+u);
								int nu=u|w;
								int nv=v+bill[w]-bill[w&u]-a[i];
								//log("nv:"+nv);
								if (nv>=0) {
									//log("----merging");
									nxt[nu][nv]=Math.min(spend[u][v]+cost[i][w], nxt[nu][nv]);
								}
							}
						}
				}

			}
			spend=nxt;
		}
		int ans=FX;
		for (int w=0;w<MX;w++)
			for (int v=0;v<VX;v++)
				ans=Math.min(ans, spend[w][v]);

		if (ans==FX)
			ans=-1;
		return ans;
	}

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

		reader=new InputReader(System.in);
		int n=reader.readInt();
		int m=reader.readInt();
		int[] a=new int[n];
		int tot=0;
		for (int i=0;i<n;i++) {
			a[i]=reader.readInt();
			tot+=a[i];
		}
		int[] b=new int[m];
		for (int i=0;i<m;i++)
			b[i]=reader.readInt();

		int[][] c=new int[n][m];
		for (int i=0;i<n;i++)
			for (int j=0;j<m;j++)
				c[i][j]=reader.readInt();

		int ans=solveAlt(a,b,c);

		System.out.println(ans);

	}

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

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

		private int read() throws IOException {
			if (curChar >= numChars) {
				curChar = 0;
				numChars = stream.read(buf);
				if (numChars <= 0) {
					return -1;
				}
			}
			return buf[curChar++];
		}

		public final String readString() throws IOException {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuilder res=new StringBuilder();
			do {
				res.append((char)c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public final int readInt() throws IOException {
			int c = read();
			boolean neg=false;
			while (isSpaceChar(c)) {
				c = read();
			}
			char d=(char)c;
			//log("d:"+d);
			if (d=='-') {
				neg=true;
				c = read();
			}
			int res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			//log("res:"+res);
			if (neg)
				return -res;
			return res;

		}

		public final long readLong() throws IOException {
			int c = read();
			boolean neg=false;
			while (isSpaceChar(c)) {
				c = read();
			}
			char d=(char)c;
			//log("d:"+d);
			if (d=='-') {
				neg=true;
				c = read();
			}
			long res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			//log("res:"+res);
			if (neg)
				return -res;
			return res;

		}

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


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

			    				public class Practice {
			    					private static InputStream stream;
			    					private static byte[] buf = new byte[1024];
			    					private static int curChar;
			    					private static int numChars;
			    					private static SpaceCharFilter filter;
			    					private static PrintWriter pw;
			    					private static long mod=1000000007;

			    				    public static void main(String[] args) {
			    				    	InputReader(System.in);
			    						pw = new PrintWriter(System.out);
			    				        new Thread(null ,new Runnable(){
			    				           public void run(){
			    				               try{
			    				                   solve();//This is solution method
			    				                   pw.close();
			    				               } catch(Exception e){
			    				                   e.printStackTrace();
			    				               }
			    				           }
			    				       },"1",1<<26).start();
			    				   }
			    				    static long sort(int a[])
			        		    	{  int n=a.length;
			        		    		int b[]=new int[n];
			        		    		return mergeSort(a,b,0,n-1);}
			        		    	static long mergeSort(int a[],int b[],long left,long right)
			        		    	{ long c=0;if(left<right)
			        		    	 {   long mid=left+(right-left)/2;
			        		    		 c= mergeSort(a,b,left,mid);
			        		    		 c+=mergeSort(a,b,mid+1,right);
			        		    		 c+=merge(a,b,left,mid+1,right); }
			        		    		return c;	 }
			        		    	static long merge(int a[],int  b[],long left,long mid,long right)
			        		    	{long c=0;int i=(int)left;int j=(int)mid; int k=(int)left;
			        		    	while(i<=(int)mid-1&&j<=(int)right)
			        		    	{ if(a[i]>a[j]) {b[k++]=a[i++]; }
			        		    	else	{ b[k++]=a[j++];c+=mid-i;}}
			        		    	while (i <= (int)mid - 1)   b[k++] = a[i++];
			        		    	while (j <= (int)right) b[k++] = a[j++];
			        		    	for (i=(int)left; i <= (int)right; i++)
			        		    		a[i] = b[i];  return c;  }

			        		    	public static void solve() {

			        		    		HashSet<Integer> prime=new HashSet<Integer>();

			        		    		for(int i=2;i<=1;i++){
			        		    			if(isPrime(i))
			        		    				prime.add(i);
			        		    		}

			        		    		int n=nextInt();
			        		    		if(n==1){
			        		    			pw.println(0);
			        		    			return;
			        		    		}
			        		    		int curr=0,start=1,end=n,pos=1;
			        		    		long price=0;
			        		    		while(end-start>=1){
			        		    			price++;
			        		    			end--;
			        		    			start++;
			        		    		}
			        		    		if(n%2!=0)
			        		    			price++;
			        		    		pw.println(price-1);

			        		    	}
			        		    	public static long trailingZeroes(long n) {
			        		    	    long count = 0;

			        		    	    while (n > 0) {
			        		    	        n /= 5;
			        		    	        count += n;
			        		    	    }

			        		    	    return count;
			        		    	}

			    				    public static int largestPrimeFactor(long number) {
			    				        int i;

			    				        for (i = 2; i <= number; i++) {
			    				            if (number % i == 0) {
			    				                number /= i;
			    				                i--;
			    				            }
			    				        }

			    				        return i;
			    				}

			    					public static long pow(long n, long p) {
							    		if(p==0)
							    			return 1;
							    		if(p==1)
							    			return n;
							    		if(p%2==0){
							    			long temp=pow(n, p/2);
							    		return (temp*temp);
							    		}else{
							    			 	long temp=pow(n,p/2);
							    			 	temp=(temp*temp);
							    			 	return(temp*n);

							    		}
							    	}
			    				    public static String reverseString(String s) {
			    			    		StringBuilder sb = new StringBuilder(s);
			    			    		sb.reverse();
			    			    		return (sb.toString());
			    			    	}
			    				    private static BigInteger ncr(int n,int k){
			    				    	if (k < 0 || k > n) return BigInteger.ZERO;
			    				        if (n-k < k) k = n-k;

			    				        BigInteger x = BigInteger.ONE;
			    				        for (int i = 1; i <= k; i++) {
			    				            x = x.multiply(new BigInteger(""+(n-i+1)));
			    				            x = x.divide(new BigInteger(""+i));
			    				        }

			    				return x;
			    				    }
			    				    static long fact[];
			    				private static void factmod(int n,int mod){
			    					fact=new long[n+1];
			    					fact[0]=1;
			    					for(int i=1;i<=n;i++){
			    						fact[i]=i*fact[i-1];
			    						//fact[i]%=mod;

			    					}

			    				}
			    				private static long phi(long n){
			    					long ans=n;
			    					if(n%2==0)
			    						ans-=(ans/2);
			    					while(n%2==0)
			    					{
			    						n/=2;

			    					}
			    					for(int i=3;i*i<=n;i+=2){
			    						if(n%i==0){
			    						while(n%i==0){
			    							n/=i;
			    						}
			    						ans-=(ans/i);
			    						}
			    					}
			    					if(n>2)
			    						ans-=(ans/n);

			    					return ans;
			    				}
			    				static long d,x,y;
			    				static long modInverse(long A, long M)
			    				{
			    				extendedEuclid(A,M);
			    				return (x%M+M)%M;    //x may be negative
			    				}
			    				static void extendedEuclid(long A, long B) {
			    				if(B == 0) {
			    				d = A;
			    				x = 1;
			    				y = 0;
			    				}
			    				else {
			    				extendedEuclid(B, A%B);
			    				long temp = x;
			    				x = y;
			    				y = temp - (A/B)*y;
			    				}
			    				}
			    				static boolean prime[];
			    				static int spf[];
			    				public static void sieve(int n){
			    					prime=new boolean[n+1];
			    					spf=new int[n+1];

			    					Arrays.fill(spf, 1);
			    					Arrays.fill(prime, true);
			    				prime[1]=false;
			    					spf[2]=2;
			    					for(int i=4;i<=n;i+=2)
			    					{
			    						spf[i]=2;
			    						prime[i]=false;
			    					}

			    				for(int i=3;i<=n;i+=2){
			    					if(prime[i]){
			    						spf[i]=i;
			    						for(int j=2*i;j<=n;j+=i){

			    							prime[j]=false;
			    						if(spf[j]==1){
			    							spf[j]=i;
			    						}
			    						}
			    					}
			    				}

			    				}
			    				public static boolean isPrime(long n) {

			    					if (n <= 1)
			    						return false;
			    					if (n <= 3)
			    						return true;

			    					if (n % 2 == 0 || n % 3 == 0)
			    						return false;

			    					for (long i = 5; i * i <= n; i = i + 6)
			    						if (n % i == 0 || n % (i + 2) == 0)
			    							return false;

			    					return true;
			    				}

			    				    public static long pow(long n, long p,long mod) {
			    						if(p==0)
			    							return 1;
			    						if(p==1)
			    							return n%mod;
			    						if(p%2==0){
			    							long temp=pow(n, p/2,mod);
			    						return (temp*temp)%mod;
			    						}else{
			    							 	long temp=pow(n,p/2,mod);
			    							 	temp=(temp*temp)%mod;
			    							 	return(temp*n)%mod;

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

			    					public static void InputReader(InputStream stream1) {
			    						stream = stream1;
			    					}

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

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

			    					private static 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++];
			    					}

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

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

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

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

			    					private static int[] nextIntArray(int n) {
			    						int[] arr = new int[n];
			    						for (int i = 0; i < n; i++) {
			    							arr[i] = nextInt();
			    						}
			    						return arr;
			    					}

			    					private static int[][] next2dArray(int n, int m) {
			    						int[][] arr = new int[n][m];
			    						for (int i = 0; i < n; i++) {
			    							for (int j = 0; j < m; j++) {
			    								arr[i][j] = nextInt();
			    							}
			    						}
			    						return arr;
			    					}
			    					private static char[][] nextCharArray(int n,int m){
			    						char [][]c=new char[n][m];
			    						for(int i=0;i<n;i++){
			    							String s=nextLine();
			    							for(int j=0;j<s.length();j++){
			    								c[i][j]=s.charAt(j);
			    							}
			    						}
			    						return c;
			    					}

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

			    					private static void pArray(int[] arr) {
			    						for (int i = 0; i < arr.length; i++) {
			    							pw.print(arr[i] + " ");
			    						}
			    						pw.println();
			    						return;
			    					}

			    					private static void pArray(long[] arr) {
			    						for (int i = 0; i < arr.length; i++) {
			    							pw.print(arr[i] + " ");
			    						}
			    						pw.println();
			    						return;
			    					}

			    					private static void pArray(boolean[] arr) {
			    						for (int i = 0; i < arr.length; i++) {
			    							pw.print(arr[i] + " ");
			    						}
			    						pw.println();
			    						return;
			    					}

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

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

			    				}

			    				/*class Pair implements Comparable<Pair>{

			    					int  from,to,a,b;
			    					Pair(int from,int to,int a,int b){

			    						this.from=from;
			    						this.to=to;
			    						this.a=a;
			    						this.b=b;
			    					}
			    					@Override
			    					public int compareTo(Pair o) {

			    						return 0;
			    						//return (int)(o.size-size);
			    					}
			    					public int hashCode() {
			    							//int hu = (int) (x ^ (x >>> 32));
			    							//int hv = (int) (y ^ (y >>> 32));
			    							//int hw = (int) (mass ^ (mass >>> 32));
			    							//return 31 * hu + hv ;
			    						return 0;
			    						}
			    						public boolean equals(Object o) {
			    							Pair other = (Pair) o;
			    						//	return x == other.x && y == other.y;
			    						return false;
			    						}
			    				}*/


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

public class Main{
	public static void main(String args[]) throws Exception{
		PrintWriter out = new PrintWriter(System.out);
		Input in = new Input();
		int test = in.getInt();
		while(test-->0){
			int s = in.getInt();
			StringBuilder st = new StringBuilder();
			for(int i=9;i>0;i--){
				if(s<=i){
					st.append(s);
					break;
				}
				else{
					st.append(i);
					s = s-i;
				}
			}
			out.println(st.reverse());
			out.flush();
		}
		out.close();
		in.close();
	}

	public static int match(String tx, String pt){
		int len1 = tx.length();
		int len2 = pt.length();
		int pf[] = new int[len2];
		preFunction(pt,pf,len2);
		int q = 0;
		for(int i=0;i<len1;i++){
			while(q>0&&tx.charAt(i)!=pt.charAt(q)){
				q = pf[q-1];
			}
			if(tx.charAt(i)==pt.charAt(q)){
				q++;
			}
			if(q==len2){
				return i+1;
			}
		}
		return -1;
	}
	public static void preFunction(String pt, int pf[], int len2){
		int q = 0;
		pf[0] = 0;
		for(int i=1;i<len2;i++){
			while(q>0&&pt.charAt(q)!=pt.charAt(i)){
				q = pf[q-1];
			}
			if(pt.charAt(q)==pt.charAt(i)){
				q++;
			}
			pf[i] = q;
		}
	}
}

class Input{
	BufferedReader in;
	StringTokenizer st;
	public Input() throws Exception{
		in = new BufferedReader(new InputStreamReader(System.in));
		st = null;
	}

	public String getString() throws Exception{
		while(st==null||st.hasMoreTokens()==false){
			st = new StringTokenizer(in.readLine());
		}

		return st.nextToken();
	}
	public boolean getBoolean() throws Exception{
		return Boolean.parseBoolean(getString());
	}
	public byte getByte() throws Exception{
		return Byte.parseByte(getString());
	}
	public short getShort() throws Exception{
		return Short.parseShort(getString());
	}
	public int getInt() throws Exception{
		return Integer.parseInt(getString());
	}
	public long getLong() throws Exception{
		return Long.parseLong(getString());
	}
	public float getFloat() throws Exception{
		return Float.parseFloat(getString());
	}
	public double getDouble() throws Exception{
		return Double.parseDouble(getString());
	}
	public String getLine() throws Exception{
		return in.readLine();
	}
	public void close() throws Exception{
		if(in!=null) in.close();
	}
}


import java.util.*;

public class Main {
    static Scanner in = new Scanner(System.in);
    static int mod = 1000_000_000 + 7;
    public static void main(String[] args) {
        long[] ar = new long[6];
        int n;
        ar[0] = in.nextInt();
        ar[1] = in.nextInt();
        n = in.nextInt();
        n--;
        for (int i = 2; i < 6; i++)
            ar[i] = (ar[i - 1] - ar[i - 2] + mod) % mod;
        System.out.println((ar[n % 6] + mod) % mod);
    }
}


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

/**
 * 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);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskB {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        long n = in.nextLong();
        long m = in.nextLong();
        long ans = 0;
        ans = calc(n, m);
        out.println(ans);
    }

    private long calc(long n, long m) {
        if (n >= m) {
            return n - m;
        }
        long mid = (m + 1) / 2;
        long ans = 0;
        ans += calc(n, mid) + 1;
        if ((m & 1) != 0)
            ans++;
        return ans;
    }

}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

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

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

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

}


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

public final class AGenerateLogin {

    private void solve(InputReader in, PrintWriter out, int tn) {
        String fn = in.next();
        String ln = in.next();
        char c = ln.charAt(0);
        String ans = fn + c;
        int i;
        for (int j = 1; j < fn.length(); j++) {
            if (fn.charAt(j)==c){
                ans = fn.substring(0,j+1);
                break;
            }
        }
        if (ans.length() == 1) {
            ans += c;
        }
        for (int j = 1; j < ans.length()-1; j++) {
            if (c < ans.charAt(j)){
                ans = ans.substring(0,j)+c;
            }
        }
        out.println(ans);
    }

    private int gcd(int x, int y) {
        if (y == 0) {
            return x;
        } else {
            return gcd(y, x % y);
        }
    }

    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);
        new AGenerateLogin().solve(in, out, 1);
        out.close();
    }

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

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

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

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

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


import java.util.*;

	import java.io.*;;
	public class java2 {
	  public static void main(String[]arg) {
		  Scanner sc=new Scanner(System.in);
		  int n=sc.nextInt();
		 int k=sc.nextInt();
		 int c=0;
		 if(n>=k) {
			 for(int i=1;i<=n;i++) {
				 if(k%i==0)c++;
			 }
		 }else if(n<k) {
			 if(k%n==0) {
				 for(int i=k/n;i<=n;i++) {
					 if(k%i==0)c++;
				 }
			 }else {
				 for(int i=(k/n)+1;i<=n;i++) {
					 if(k%i==0)c++;
				 }
			 }
		 }
		 System.out.print(c);
	  }

	}


/**
 * Created by Anna on 08.02.2015.
 */

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

public class TaskC {

    StringTokenizer st;
    BufferedReader in;
    PrintWriter out;

    public static void main(String[] args) throws NumberFormatException,
            IOException {
        TaskC solver = new TaskC();
        solver.open();
        solver.solve();
        solver.close();
    }

    public void open() throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        //in = new BufferedReader(new FileReader("input.txt"));
        //out = new PrintWriter(new FileWriter("output.txt"));
    }

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

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

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

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

    boolean hasMoreTokens() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            String line = in.readLine();
            if (line == null)
                return false;
            st = new StringTokenizer(line);
        }
        return true;
    }

    class Company {
        int left, right;

        public Company(int left, int right) {
            this.left = left;
            this.right = right;
        }
    }

    public void solve() throws NumberFormatException, IOException {
        int n = nextInt();
        Company[] companies = new Company[n];
        for (int i = 0; i < n; i++) {
            companies[i] = new Company(nextInt(), nextInt());
        }
        double answ = 0;

        for (int money = 1; money <= 10000; money++) {
            for (int companyInd = 0; companyInd < n; companyInd++) {
                Company winner = companies[companyInd];
                double pWin;
                if (winner.right <= money) continue;
                if (winner.left > money) pWin = 1;
                else pWin = (winner.right - money + 0D) / (winner.right - winner.left + 1);
                double pOther1 = 1;
                for (int i = 0; i < n; i++) {
                    if (i == companyInd) continue;
                    Company cur = companies[i];
                    if (cur.left > money) {
                        pOther1 = 0;
                        break;
                    }
                    pOther1 *= (Math.min(money, cur.right) - cur.left + 1D) / (cur.right - cur.left + 1);
                }

                if (pOther1 == 0) continue;

                double pOther2 = 1;
                for (int i = 0; i < n; i++) {
                    if (i == companyInd) continue;
                    Company cur = companies[i];
                    pOther2 *= (Math.min(money - 1, cur.right) - cur.left + 1D) / (cur.right - cur.left + 1);
                }
                answ += money * pWin * (pOther1 - pOther2);
//                out.println(money + ": win " + answ);
            }

            int maskMax = 1 << n;
            for (int mask = 3; mask < maskMax; mask++) {
                if (isPow(mask)) continue;
                double pMask = 1;
                boolean badMask = false;
                for (int i = 0; i < n; i++) {
                    if ((mask & (1 << i)) != 0) {
                        if (companies[i].left > money || companies[i].right < money) {
                            badMask = true;
                            break;
                        } else pMask /= (companies[i].right - companies[i].left + 1);
                    } else {
                        Company cur = companies[i];
                        if (cur.left > (money - 1)) {
                            badMask = true;
                            break;
                        }
                        pMask *= (Math.min(money - 1, cur.right) - cur.left + 1D) / (cur.right - cur.left + 1);
                    }
                }
                if (!badMask) {
                    answ += money * pMask;
//                    out.println("mask " + mask + " " + answ);
                }
            }
        }

        out.println(answ);
    }

    boolean isPow(int a) {
        int[] pow = {1, 2, 4, 8, 16, 32, 64, 128};
        for (int i : pow) {
            if (a == i) return true;
        }
        return false;
    }

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


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

public class Solution {

    static void olympiad(int []a, int n) {
    Arrays.sort(a);
    int max=0;
    int c=0;
    for(int i=0;i<n;i++){
        if(a[i]>max){
            max=a[i];
            c++;
        }

    }
    System.out.print(c);

   }

 public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int a[]=new int [n];
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();
        }
        olympiad(a,n);

   }
}


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

	public static void main(String[] args) throws Exception{
		long num = 1000000007;

		// TODO Auto-generated method stub
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 		PrintWriter out = new PrintWriter(System.out);
 		int n = Integer.parseInt(bf.readLine());
 		long[] dp = new long[n+1];
 		int[] isPrime = new int[n+1];
		Arrays.fill(isPrime, 1);
		int[] mu = new int[n+1];
		Arrays.fill(mu,  1);
		for(int i = 2;i<=n;i++){
			if (isPrime[i] == 1){
				for(int j = i;j<=n;j+=i){
					if (j > i)
						isPrime[j] = 0;
					if (j%(i*i) == 0)
						mu[j] = 0;
					mu[j] = -mu[j];
				}
			}
		}
		long sum = 0;
		for(int i = 2;i<=n;i++){
			sum+=(((long)(0-mu[i])*((((long)(n/i))*power(n-n/i, num-2, num))%num))%num+num)%num;
			sum%=num;

		}
		sum+=1;
		sum%=num;
 		out.println(sum);

 		out.close();

 	}
	public static long power(long x, long y, long mod){
		long ans = 1;
		while(y>0){
			if (y%2==1)
				ans = (ans*x)%mod;
			x = (x*x)%mod;
			y/=2;
		}
		return ans;
	}
}

//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is


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

public class cfShit {

	int pow(int a, int b, int p) {
		int ret = 1;
		for (; b > 0; b >>= 1) {
			if ((b & 1) == 1) {
				ret = (int) ((long) ret * a % p);
			}
			a = (int) ((long) a * a % p);
		}
		return ret;
	}

	int primitiveRoot(int p) {
		loop: for (int i = 2;; i++) {
			for (int j = 2; j * j <= p - 1; j++) {
				if ((p - 1) % j != 0) {
					continue;
				}
				if (pow(i, (p - 1) / j, p) == 1) {
					continue loop;
				}
				if (pow(i, j, p) == 1) {
					continue loop;
				}
			}
			return i;
		}
	}

	static final int C = 5000;

	int[] chirp(int[] a, int[] pz, int p) {
		int n = a.length;
		// pz[1] has order 2 * n modulo prime p

		a = a.clone();
		for (int i = 0; i < n; i++) {
			a[i] %= p;
		}

		int[] one = new int[n];
		for (int i = 0; i < n; i++) {
			one[i] = (int) ((long) a[i] * pz[(int) ((long)i * i % (2 * n))] % p);
		}

		int[] two = new int[2 * n - 1];
		for (int i = 0; i <= 2 * n - 2; i++) {
			two[i] = pz[(int) Math.floorMod(-(long)(n - 1 - i) * (n - 1 - i), 2 * n)];
		}

		int[] prod = mult(one, two, p);

		prod = Arrays.copyOfRange(prod, n - 1, 2 * n - 1);
		for (int i = 0; i < n; i++) {
			prod[i] = (int) ((long) prod[i] * pz[(int) ((long)i * i % (2 * n))] % p);
		}

		return prod;
		// returns polynomial a modulo p at points 1, rootZ^2, rootZ^4, ...
	}

	void submit() {
		int n = nextInt();
		int[] b = new int[n];
		int[] c = new int[n];

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

		int[] initB = b.clone();

		b = makeInvPows(b);

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

		int z, p;
		int[] pz;

		int[] fb;

		for (int i = 1;; i++) {
			p = 2 * n * i + 1;
			if (p < C) {
				continue;
			}
			if (!BigInteger.valueOf(p).isProbablePrime(30)) {
				continue;
			}
			z = pow(primitiveRoot(p), (p - 1) / (2 * n), p);
			pz = new int[2 * n];
			pz[0] = 1;
			for (int j = 1; j < pz.length; j++) {
				pz[j] = (int) ((long) pz[j - 1] * z % p);
			}

			fb = chirp(b, pz, p);
			boolean allGood = true;
			for (int coef : fb) {
				allGood &= coef != 0;
			}

			if (allGood) {
				break;
			}
		}

		int[] cc = new int[n];
		int inv2 = (p + 1) / 2;
		for (int i = 0; i < n; i++) {
			cc[i] = (c[(i + n - 1) % n] - c[i]) % p;
			if (cc[i] < 0) {
				cc[i] += p;
			}
			cc[i] = (int)((long)cc[i] * inv2 % p);
		}

		int[] fc = chirp(cc, pz, p);

		int[] fa = new int[n];
		for (int i = 0; i < n; i++) {
			fa[i] = (int)((long)fc[i] * pow(fb[i], p - 2, p) % p);
		}

		int[] aa = chirp(fa, makeInvPows(pz), p);
		int invN = pow(n, p - 2, p);
		for (int i = 0; i < n; i++) {
			aa[i] = (int)((long)aa[i] * invN % p);
		}

		long[] d = new long[n];
		for (int i = 1; i < n; i++) {
			d[i] = d[i - 1] + makeInt(aa[i], p);
		}

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

		long A = n;
		long B = 0;
		long C = -c[0];
		for (int i = 0; i < n; i++) {
			long diff = initB[i] - d[i];
			B -= diff;
			C += diff * diff;
		}

//		System.err.println(A + " " + B + " " + C);

		List<Long> x0s = new ArrayList<>();

		long D = B * B - A * C;
		if (D < 0) {
		} else if (D == 0) {
			if ((-B % A) == 0) {
				x0s.add((-B) / A);
			}
		} else {
			long rd = (long) Math.sqrt(D);
			if (rd * rd == D) {
				if ((-B - rd) % A == 0) {
					x0s.add((-B - rd) / A);
				}
				if ((-B + rd) % A == 0) {
					x0s.add((-B + rd) / A);
				}
			}
		}

		out.println(x0s.size());
		for (long x0 : x0s) {
			for(int i = 0; i < n; i++) {
				out.print(d[i] + x0 + " ");
			}
			out.println();
		}

	}

	int makeInt(int x, int p) {
		return x < p / 2 ? x : x - p;
	}

	int[] makeInvPows(int[] a) {
		int[] b = new int[a.length];
		b[0] = a[0];
		for (int i = 1, j = a.length - 1; i < a.length; i++, j--) {
			b[i] = a[j];
		}
		return b;
	}

	void preCalc() {

	}

	void stress() {

	}

	void test() {

	}

	cfShit() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		preCalc();
		submit();
		// stress();
		// test();
		out.close();
	}

	static int nextPowerOf2(int x) {
		return x == 1 ? 1 : Integer.highestOneBit(x - 1) << 1;
	}

	static double[][] foo(int[] intV, int len, int L) {
		// pack into one complex vector
		double[] f = new double[len << 1];
		for (int i = 0; i < intV.length; i++) {
			f[i << 1] = intV[i] % L;
			f[i << 1 | 1] = intV[i] / L;
		}

		fft(f, false);

		// unpack
		double[] f1 = new double[len << 1];
		double[] f2 = new double[len << 1];

		// LEN MUST BE A POWER OF 2!!!
		int zzz = (len << 1) - 1;
		for (int i = 0; i < len << 1; i += 2) {
			int j = (-i) & zzz;
			f1[i] = .5 * (f[i] + f[j]);
			f1[i + 1] = .5 * (f[i + 1] - f[j + 1]);
			f2[i] = .5 * (f[i + 1] + f[j + 1]);
			f2[i + 1] = .5 * (f[j] - f[i]);
		}

		return new double[][] { f1, f2 };
	}

	public static int[] mult(int[] a, int[] b, int pMod) {

		int k = (int) (Math.sqrt(pMod) + 1);
		int k2 = k * k % pMod;

		int len = nextPowerOf2(a.length + b.length - 1);
		int len2 = len << 1;

		double[][] fA = foo(a, len, k);
		double[][] fB = foo(b, len, k);

		double[][] invF = new double[2][len2];

		for (int ia = 0; ia < 2; ia++) {
			for (int ib = 0; ib < 2; ib++) {

				double[] r = invF[(ia + ib) >> 1];
				double[] p = fA[ia];
				double[] q = fB[ib];

				if (((ia + ib) & 1) == 0) {
					for (int i = 0; i < len2; i += 2) {
						r[i] += p[i] * q[i] - p[i + 1] * q[i + 1];
						r[i + 1] += p[i] * q[i + 1] + p[i + 1] * q[i];
					}

				} else {
					for (int i = 0; i < len2; i += 2) {
						r[i] -= p[i] * q[i + 1] + p[i + 1] * q[i];
						r[i + 1] += p[i] * q[i] - p[i + 1] * q[i + 1];
					}
				}

			}
		}

		fft(invF[0], true);
		fft(invF[1], true);

		int[] ret = new int[len];

		for (int i = 0; i < len2; i += 2) {
//			long v0 = Math.round(invF[0][i]) % P;
//			long v1 = Math.round(invF[0][i + 1]) % P * L % P;
//			long v2 = Math.round(invF[1][i]) % P * L2 % P;
			long v0 = (long)(invF[0][i] + .5);
			long v1 = ((long)(invF[0][i + 1] + .5)) % pMod * k;
			long v2 = ((long)(invF[1][i] + .5)) % pMod * k2;
			ret[i >> 1] = (int) ((v0 + v1 + v2) % pMod);
		}

		return ret;
	}

	public static void fft(double[] v, boolean invert) {
		int n2 = v.length;
		int n = n2 >> 1;
		int logN = Integer.numberOfTrailingZeros(n);
		prepareArrays(logN);
		int[] rev = rev2D[logN];

		for (int i = 0; i < n2; i += 2) {
			int j = rev[i >> 1] << 1;
			if (i < j) {
				double t = v[i];
				v[i] = v[j];
				v[j] = t;
				t = v[i + 1];
				v[i + 1] = v[j + 1];
				v[j + 1] = t;
			}
		}

		double conj = invert ? -1 : 1;

		for (int len = 2, row = 0; len <= n; len <<= 1, row++) {
			double[] pow = pow2D[row];
			for (int i = 0; i < n; i += len) {
				for (int j1 = i << 1, j2 = j1 + len, k = 0; k < len; j1 += 2, j2 += 2, k += 2) {
					double uA = v[j1];
					double uB = v[j1 + 1];

					double mRe = pow[k];
					double mIm = pow[k + 1] * conj;

					double vA = v[j2] * mRe - v[j2 + 1] * mIm;
					double vB = v[j2] * mIm + v[j2 + 1] * mRe;

					v[j1] = uA + vA;
					v[j1 + 1] = uB + vB;
					v[j2] = uA - vA;
					v[j2 + 1] = uB - vB;
				}
			}
		}
		if (invert) {
			for (int i = 0; i < n2; i++) {
				v[i] /= n;
			}
		}
	}

	static double[][] pow2D = { { 1, 0 } };
	static int[][] rev2D = {};

	static void prepareArrays(int n) {
		if (rev2D.length < n + 1) {
			rev2D = Arrays.copyOf(rev2D, n + 1);
		}

		if (rev2D[n] == null) {
			int[] tmp = rev2D[n] = new int[1 << n];
			for (int i = 0; i < (1 << n); i++) {
				tmp[i] = (tmp[i >> 1] >> 1) | ((i & 1) << (n - 1));
			}
		}

		int oldN = pow2D.length;

		if (oldN >= n) {
			return;
		}

		pow2D = Arrays.copyOf(pow2D, n);

		for (int i = oldN; i < n; i++) {
			double angle = Math.PI / (1 << i);

			double mRe = Math.cos(angle);
			double mIm = Math.sin(angle);

			double[] dst = pow2D[i] = new double[2 << i];
			double[] src = pow2D[i - 1];

			for (int j = 0; j < 1 << i; j += 2) {
				double re = src[j];
				double im = src[j + 1];

				dst[j << 1] = re;
				dst[j << 1 | 1] = im;
				dst[j << 1 | 2] = re * mRe - im * mIm;
				dst[j << 1 | 3] = re * mIm + im * mRe;
			}
		}
	}

	static final Random rng = new Random();

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

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

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;

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

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

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

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

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


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

public class internationalolympiad {
  private static InputReader in;
  private static PrintWriter out;

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

    int n = in.nextInt();
    int[] first = new int[]
        {0, 1989, 1999, 2099, 3099, 13099, 113099, 1113099, 11113099, 111113099, 1111113099};
    while(n-->0) {
      String s = in.next().substring(4);
      int atleast = first[s.length()];
      int clen = (""+atleast).length();
      String suff = (""+atleast).substring(clen-s.length());
      String front = (""+atleast).substring(0, clen-s.length());
      boolean add = Integer.parseInt(s) < Integer.parseInt(suff);
      if (add) {
        if (front.length() == 0) front = "1";
        else front = ""+(Integer.parseInt(front)+1);
      }
      System.out.println(front+""+s);
    }

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

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

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

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

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

}


//package bubblecup13.f;

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

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

	void solve()
	{
		int n = ni(), Q = ni(), K = ni();
		int[][] qs = new int[Q][];
		int[] day = new int[Q+1];
		int d = 1;
		for(int z = 0;z < Q;z++){
			int t = ni();
			if(t == 1){
				qs[z] = new int[]{t, ni()-1, ni()-1};
			}else if(t == 2){
				qs[z] = new int[]{t, ni()-1};
			}else{
				qs[z] = new int[]{t};
			}
		}
		for(int i = 0;i < Q;i++){
			if(qs[i][0] == 3){
				day[d++] = i;
			}
		}

		QuerySegmentTree qst = new QuerySegmentTree(Q);
		int da = 0;
		for(int i = 0;i < Q;i++){
			if(qs[i][0] == 1){
				if(da+K >= d){
					qst.put(i, Q, i);
				}else{
					qst.put(i, day[da+K], i);
				}
			}else if(qs[i][0] == 3){
				da++;
			}
		}

		RestorableDisjointSet2 rds = new RestorableDisjointSet2(n, Q);

		// process
		int[] stack = new int[30];
		int sp = 0;

		for(int i = 0;i < qs.length;i++){
			int upd = Integer.bitCount((i^i-1)&(qst.M-1));
			if(i > 0){
				for(int j = 0;j < upd;j++){
					// pop
					rds.revert(stack[--sp]);
				}
			}
			for(int j = upd-1;j >= 0;j--){
				// push
				L node = qst.st[qst.H+i>>>j];
				stack[sp++] = rds.hp;
				if(node != null){
					for(int k = 0;k < node.size();k++){
						int id = node.a[k];
						rds.union(qs[id][1], qs[id][2]);
					}
				}
			}

			if(qs[i][0] == 2){
				int[] q = qs[i];
				out.println(-rds.upper[rds.root(q[1])]);
			}
		}
	}

	public static class L {
		public int[] a;
		public int p = 0;

		public L(int n) { a = new int[n]; }
		public L add(int n)
		{
			if(p >= a.length)a = Arrays.copyOf(a, a.length*3/2+1);
			a[p++] = n;
			return this;
		}
		public int size() { return p; }
		public L clear() { p = 0; return this; }
		public int[] toArray() { return Arrays.copyOf(a, p); }
		@Override
		public String toString() {return Arrays.toString(toArray());}
	}

	public static class RestorableDisjointSet2 {
		public int[] upper; // minus:num_element(root) plus:root(normal)
		private int[] targets;
		private int[] histupper;
		public int hp = 0;

		public RestorableDisjointSet2(int n, int m)
		{
			upper = new int[n];
			Arrays.fill(upper, -1);

			targets = new int[2*m];
			histupper = new int[2*m];
			//
			//			w = new int[n];
		}

		public RestorableDisjointSet2(RestorableDisjointSet2 ds)
		{
			this.upper = Arrays.copyOf(ds.upper, ds.upper.length);
			this.histupper = Arrays.copyOf(ds.histupper, ds.histupper.length);
			//
			this.hp = ds.hp;
		}

		public int root(int x)
		{
			return upper[x] < 0 ? x : root(upper[x]);
		}

		public boolean equiv(int x, int y)
		{
			return root(x) == root(y);
		}

		public boolean union(int x, int y)
		{
			x = root(x);
			y = root(y);
			if(x != y) {
				if(upper[y] < upper[x]) {
					int d = x; x = y; y = d;
				}
				//				w[x] += w[y];
				record(x); record(y);
				upper[x] += upper[y];
				//
				upper[y] = x;
			}
			return x == y;
		}

		public int time() { return hp; }

		private void record(int x)
		{
			targets[hp] = x;
			histupper[hp] = upper[x];
			//
			hp++;
		}

		public void revert(int to)
		{
			while(hp > to){
				upper[targets[hp-1]] = histupper[hp-1];
				//
				hp--;
			}
		}

		public int count()
		{
			int ct = 0;
			for(int u : upper){
				if(u < 0)ct++;
			}
			return ct;
		}

		public int[][] makeUp()
		{
			int n = upper.length;
			int[][] ret = new int[n][];
			int[] rp = new int[n];
			for(int i = 0;i < n;i++){
				if(upper[i] < 0)ret[i] = new int[-upper[i]];
			}
			for(int i = 0;i < n;i++){
				int r = root(i);
				ret[r][rp[r]++] = i;
			}
			return ret;
		}

	}

	public static class QuerySegmentTree
	{
		public int M, H, N;
		public L[] st;

		public QuerySegmentTree(int n)
		{
			N = n;
			M = Integer.highestOneBit(Math.max(N-1, 1))<<2;
			H = M>>>1;
			st = new L[M];
		}

		public void put(int l, int r, int qid){
			if(l >= r)return;
			while(l != 0){
				int f = l&-l;
				if(l+f > r)break;
				L q = st[(H+l)/f];
				if(q == null)q = st[(H+l)/f] = new L(1);
				q.add(qid);
				l += f;
			}

			while(l < r){
				int f = r&-r;
				L q = st[(H+r)/f-1];
				if(q == null)q = st[(H+r)/f-1] = new L(1);
				q.add(qid);
				r -= f;
			}
		}

	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

public class BOI2020C {

	static class StackDSU {
		int[] p;

		int[] idxH;
		int[] valH;
		int time = 0;

		int[] snap;
		int snapSz;

		public StackDSU(int n, int maxStackSize, int histSize) {
			p = new int[n];
			Arrays.fill(p, -1);
			idxH = new int[histSize];
			valH = new int[histSize];
			snap = new int[maxStackSize + 1];
		}

		int get(int v) {
			int ret = 0;
			while (p[v] >= 0) {
				ret ^= p[v];
				v = p[v] >> 1;
			}
			return (v << 1) | (ret & 1);
		}

		boolean canAdd(int v, int u) {
			v = get(v);
			u = get(u);

			int diff = (v ^ u) & 1;
			return ((v >> 1) != (u >> 1)) || (diff == 1);
		}

		void unite(int v, int u) {
			v = get(v);
			u = get(u);

			int diff = (v ^ u) & 1;
			v >>= 1;
			u >>= 1;

			if (v == u) {
				if (diff == 0) {
					throw new AssertionError();
				}
				return;
			}
			if (p[v] < p[u]) {
				int tmp = v;
				v = u;
				u = tmp;
			}
			set(u, p[v] + p[u]);
			set(v, (u << 1) | (diff ^ 1));
			return;
		}

		void set(int idx, int val) {
			if (p[idx] != val) {
				idxH[time] = idx;
				valH[time] = p[idx];
				p[idx] = val;
				++time;
			}
		}

		public void push(UpdateInfo upd) {
			snap[snapSz++] = time;
			unite(upd.u, upd.v);
		}

		public void pop() {
			int rollTo = snap[--snapSz];
			while (time > rollTo) {
				--time;
				p[idxH[time]] = valH[time];
			}
		}
	}

	// Must have auto-incrementable ID.
	static class UpdateInfo {
		private static int count;
		int id;

		int v, u;

		public UpdateInfo(int v, int u) {
			id = count++;
			this.v = v;
			this.u = u;
		}
	}

	static class QueueFromStack {
		public StackDSU ds;

		// stack of updates that shows in which order they were actually applied
		private UpdateInfo[] stack;
		private int size;

		private UpdateInfo[] buf;

		// updates with id < mark are grouped into power-of-2 length blocks
		// and are in the "correct"(i.e. reversed) order
		private int mark;

		// id of queue's head or mark if queue is empty
		private int head;

		public QueueFromStack(StackDSU ds, int maxQueueSize) {
			UpdateInfo.count = 0;
			this.ds = ds;
			stack = new UpdateInfo[maxQueueSize];
			buf = new UpdateInfo[maxQueueSize];
		}

		public void add(UpdateInfo upd) {
			stack[size++] = upd;
			ds.push(upd);
		}

		public void poll() {
			if (mark == head) {
				mark = UpdateInfo.count;
				for (int i = size - 1; i >= 0; --i) {
					ds.pop();
					buf[i] = stack[i];
				}
				for (int i = 0; i < size; ++i) {
					ds.push(stack[i] = buf[size - 1 - i]);
				}
			}
			int ptr = 0;
			while (stack[size - 1].id != head) {
				// must have id >= mark
				buf[ptr++] = stack[--size];
				ds.pop();
			}

			if (ptr == 0) {
				ds.pop();
				--size;
				++head;
				return;
			}

			int skip = ptr;
			int chunk = Integer.lowestOneBit(mark - head);
			for (int i = 0; i < chunk; ++i) {
				// must have id < mark
				buf[ptr++] = stack[--size];
				ds.pop();
			}

			// putting back id >= mark
			for (int i = skip - 1; i >= 0; --i) {
				ds.push(stack[size++] = buf[i]);
			}
			// putting back id < mark except id == head
			for (int i = ptr - 1; i > skip; --i) {
				ds.push(stack[size++] = buf[i]);
			}

			++head;
		}
	}

	boolean[] fast(int n, int[] vs, int[] us, int[] ls, int[] rs) {
		int m = vs.length;
		int q = ls.length;

		StackDSU dsu = new StackDSU(n, 2 * m, 2_000_000);
		QueueFromStack que = new QueueFromStack(dsu, 2 * m);

		int[] stop = new int[m];

		for (int i = 0, j = 0; i < m; i++) {
			while (j < 2 * m) {
				int v = vs[j % m];
				int u = us[j % m];
				if (!dsu.canAdd(v, u)) {
					break;
				}
				que.add(new UpdateInfo(v, u));
				++j;
			}
			stop[i] = j;
			que.poll();
		}

		boolean[] ret = new boolean[q];

		for (int i = 0; i < q; i++) {
			int l = ls[i];
			int r = rs[i];
			boolean ans;
			if (r == m - 1) {
				ans = stop[0] < l;
			} else {
				ans = stop[r + 1] < l + m;
			}
			ret[i] = ans;
		}

		return ret;
	}

	static boolean test(int mask, int i) {
		return ((mask >> i) & 1) == 1;
	}

	boolean slowSingle(int n, int[] vs, int[] us, int l, int r) {
		outer: for (int mask = 0; mask < 1 << n; mask++) {
			for (int i = 0; i < vs.length; i++) {
				if (l <= i && i <= r) {
					continue;
				}
				if (test(mask, vs[i]) == test(mask, us[i])) {
					continue outer;
				}
			}
			return false;
		}
		return true;
	}

	boolean[] slow(int n, int[] vs, int[] us, int[] ls, int[] rs) {
		boolean[] ret = new boolean[ls.length];

		for (int i = 0; i < ls.length; i++) {
			ret[i] = slowSingle(n, vs, us, ls[i], rs[i]);
		}

		return ret;
	}

	void submit() {
		int n = nextInt();
		int m = nextInt();
		int q = nextInt();
		int[] vs = new int[m];
		int[] us = new int[m];
		for (int i = 0; i < m; i++) {
			vs[i] = nextInt() - 1;
			us[i] = nextInt() - 1;
		}

		int[] ls = new int[q];
		int[] rs = new int[q];

		for (int i = 0; i < q; i++) {
			ls[i] = nextInt() - 1;
			rs[i] = nextInt() - 1;
		}

		boolean[] ans = fast(n, vs, us, ls, rs);
//		boolean[] ans = slow(n, vs, us, ls, rs);

		for (boolean b : ans) {
			out.println(b ? "YES" : "NO");
		}
	}

	void test() {

	}

	static final int B = 10;

	void stress() {
		for (int tst = 0;; tst++) {
			int n = rand(2, B);
			int m = rand(1, B);
			int q = B;
			int[] vs = new int[m];
			int[] us = new int[m];
			for (int i = 0; i < m; i++) {
				vs[i] = rand(0, n - 1);
				us[i] = (vs[i] + rand(1, n - 1)) % n;
			}
			int[] ls = new int[q];
			int[] rs = new int[q];
			for (int i = 0; i < q; i++) {
				ls[i] = rand(0, m - 1);
				rs[i] = rand(ls[i], m - 1);
			}

			boolean[] fast = fast(n, vs, us, ls, rs);
			boolean[] slow = slow(n, vs, us, ls, rs);

			if (!Arrays.equals(fast, slow)) {
				System.err.println(n + " " + m + " " + q);
				System.err.println(Arrays.toString(vs));
				System.err.println(Arrays.toString(us));
				System.err.println(Arrays.toString(ls));
				System.err.println(Arrays.toString(rs));

				System.err.println("fast " + Arrays.toString(fast));
				System.err.println("slow " + Arrays.toString(slow));
				throw new AssertionError();
			}
			System.err.println(tst);
		}
	}

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

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

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

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

	private InputStream is;
	PrintWriter out;

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

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

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

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

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

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

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

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

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

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


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int y = scanner.nextInt();
        int b = scanner.nextInt();
        int r = scanner.nextInt();

        int q = 0;
        int w = 0;
        int e = 0;
        if (y < b) {
            q = y;
            w = y+1;
        } else {
            q = b - 1;
            w = b;
        }

        if (r > q && r > w) {
            e = w + 1;
        } else {
            e = r;
            w = e - 1;
            q = w - 1;
        }

        System.out.println(q + w + e);
    }
}


import static java.lang.Math.*;
import static java.math.BigInteger.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;

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

@SuppressWarnings("unused")
public class D {

	private final static boolean autoflush = false;

	private static final int MOD = (int) 1e9 + 7;
	private static BigInteger BMOD = valueOf(MOD);
	private static final double eps = 1e-9;
	private static final int INF = (int) 1e9;
	private static final Error NO = new Error("NO");

	public D () {
		N = sc.nextInt();
		M = sc.nextInt();

		int X = sc.nextInt();
		int Y = sc.nextInt();

		int [] T = dir(sc.next());

		A = new int [N]; B = new int [N];
		C = new int [M]; D = new int [M];

		Z = 2 * (N/2 + M/2); int P = (X+Y) % 2;
		if (N % 2 == 1 && P == 0) ++Z;
		if (N % 2 == 1 && P == (1 + M) % 2) ++Z;
		if (M % 2 == 1 && P == 0) ++Z;
		if (M % 2 == 1 && P == (1 + N) % 2) ++Z;

		long res = 1;
		for (;;) {
			if (mark(X, Y))
				break;
			res += next(X, Y, T);
			X = W[0]; Y = W[1];
		}

		exit(res);
	}

	int N, M, Z, K = 0;
	int [] A, B, C, D, W = new int [2];

	int next(int X, int Y, int [] T) {
		if (X == 1) T[0] = 1;
		if (X == N) T[0] = -1;
		if (Y == 1) T[1] = 1;
		if (Y == M) T[1] = -1;

		int t = INF, dx = T[0], dy = T[1];
		if (dx ==  1) t = min(t, N - X);
		if (dx == -1) t = min(t, X - 1);
		if (dy ==  1) t = min(t, M - Y);
		if (dy == -1) t = min(t, Y - 1);

		X += t * dx; Y += t * dy;
		W[0] = X; W[1] = Y;
		return t;
	}

	void set(int [] T, int dx, int dy) {
		T[0] = dx; T[1] = dy;
	}

	boolean mark(int X, int Y) {
		boolean res = false;
		if (Y == 1)
			res = res || inc(A, X);
		if (Y == M)
			res = res || inc(B, X);
		if (X == 1)
			res = res || inc(C, Y);
		if (X == N)
			res = res || inc(D, Y);
		return res;
	}

	boolean inc(int [] A, int X) {
		if (A[X-1] == 0) ++K; ++A[X-1];
		if (A[X-1] == 3)
			exit(-1);
		return K == Z;
	}

	int [] dir (String D) {
		String [] S = { "UL", "UR", "DL", "DR" };
		int [][] T = { { -1, -1 }, { -1, 1 }, { 1, -1 }, { 1, 1 }};
		for (int i : rep(4))
			if (D.equals(S[i]))
				return T[i];
		throw new Error();
	}

	////////////////////////////////////////////////////////////////////////////////////
	private static int [] rep(int N) { return rep(0, N); }
	private static int [] rep(int S, int T) { int [] res = new int [max(T-S, 0)]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
	private static int [] req(int S, int T) { return rep(S, T+1); }
	private static int [] sep(int N) { return sep(0, N); }
	private static int [] sep(int S, int T) { int [] res = new int [max(T-S, 0)]; for (int i = S; i < T; ++i) res[T-1-i] = i; return res; }
	private static int [] seq(int S, int T) { return sep(S, T+1); }
	private static int [] ccw (int [] D) { return new int [] { -D[1], D[0] }; }
	private static int [] cw (int [] D) { return new int [] { D[1], -D[0] }; }
	private static <T> T ceiling(SortedSet<T> S, T x) { // Java 1.5 and below
		SortedSet<T> T = S.tailSet(x);
		return T.isEmpty() ? null : T.first();
	}
	private static <T> T floor(SortedSet<T> S, T x) { // Java 1.5 and below
		if (S.contains(x))
			return x;
		else {
			SortedSet<T> T = S.headSet(x);
			return T.isEmpty() ? null : T.last();
		}
	}
	private static int [] digits(long N, int B, int sz) {
		int [] res = new int [sz];
		for (int i : sep(sz))
			if (N > 0) {
				res[i] = (int)(N % B);
				N /= B;
			}
		return res;
	}
	private static int digitSum(long N) {
		int res = 0;
		for (; N > 0; res += N % 10, N /= 10);
		return res;
	}
	private static <T extends Comparable<T>> T max(T x, T y) { return x.compareTo(y) > 0 ? x : y; }
	private static long mod(long x) { return mod(x, MOD); }
	private static long mod(long x, long mod) { return ((x % mod) + mod) % mod; }
	private static long modPow(long b, long e) { return valueOf(b).modPow(valueOf(e), BMOD).longValue(); }
	private static long pow(long b, long e) { return round(Math.pow(b, e)); }
	private static String reverse (String S) { return new StringBuilder(S).reverse().toString(); }
	private static char [][] toCharArrays(String [] S) {
		int N = S.length;
		char [][] res = new char [N][];
		for (int i : rep(N))
			res[i] = S[i].toCharArray();
		return res;
	}
	////////////////////////////////////////////////////////////////////////////////////
	private final static MyScanner sc = new MyScanner();
	private static class MyScanner {
		private String next() {
			newLine();
			return line[index++];
		}
		private char nextChar() {
			return next().charAt(0);
		}
		private int nextInt() {
			return Integer.parseInt(next());
		}
		private long nextLong() {
			return Long.parseLong(next());
		}
		private double nextDouble() {
			return Double.parseDouble(next());
		}
		private String nextLine() {
			line = null;
			return readLine();
		}
		private String [] nextStrings() {
			line = null;
			return readLine().split(" ");
		}
		private char [] nextChars() {
			return next ().toCharArray ();
		}
		private Integer [] nextInts() {
			String [] L = nextStrings();
			Integer [] res = new Integer [L.length];
			for (int i : rep(L.length))
				res[i] = Integer.parseInt(L[i]);
			return res;
		}
		private Long [] nextLongs() {
			String [] L = nextStrings();
			Long [] res = new Long [L.length];
			for (int i : rep(L.length))
				res[i] = Long.parseLong(L[i]);
			return res;
		}
		private Double [] nextDoubles() {
			String [] L = nextStrings();
			Double [] res = new Double [L.length];
			for (int i : rep(L.length))
				res[i] = Double.parseDouble(L[i]);
			return res;
		}
		private String [] next (int N) {
			String [] res = new String [N];
			for (int i : rep(N))
				res[i] = next();
			return res;
		}
		private Integer [] nextInt (int N) {
			Integer [] res = new Integer [N];
			for (int i : rep(N))
				res[i] = nextInt();
			return res;
		}
		private Long [] nextLong (int N) {
			Long [] res = new Long [N];
			for (int i : rep(N))
				res[i] = nextLong();
			return res;
		}
		private Double [] nextDouble (int N) {
			Double [] res = new Double [N];
			for (int i : rep(N))
				res[i] = nextDouble();
			return res;
		}
		private String [][] nextStrings (int N) {
			String [][] res = new String [N][];
			for (int i : rep(N))
				res[i] = nextStrings();
			return res;
		}
		private Integer [][] nextInts (int N) {
			Integer [][] res = new Integer [N][];
			for (int i : rep(N))
				res[i] = nextInts();
			return res;
		}
		private Long [][] nextLongs (int N) {
			Long [][] res = new Long [N][];
			for (int i : rep(N))
				res[i] = nextLongs();
			return res;
		}
		private Double [][] nextDoubles (int N) {
			Double [][] res = new Double [N][];
			for (int i : rep(N))
				res[i] = nextDoubles();
			return res;
		}
		//////////////////////////////////////////////
		private boolean eol() {
			return index == line.length;
		}
		private String readLine() {
			try {
				return r.readLine();
			} catch (Exception e) {
				throw new Error (e);
			}
		}
		private final java.io.BufferedReader r;
		private MyScanner () {
			this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in)));
		}
		private MyScanner (java.io.BufferedReader r) {
			try {
				this.r = r;
				while (!r.ready())
					Thread.sleep(1);
				start();
			} catch (Exception e) {
				throw new Error(e);
			}
		}
		private String [] line;
		private int index;
		private void newLine() {
			if (line == null || eol()) {
				line = readLine().split(" ");
				index = 0;
			}
		}
	}
	private static void print (Object o, Object... a) {
		printDelim(" ", o, a);
	}
	private static void cprint (Object o, Object... a) {
		printDelim("", o, a);
	}
	private static void printDelim (String delim, Object o, Object... a) {
		pw.println(build(delim, o, a));
	}
	private static void exit (Object o, Object... a) {
		print(o, a);
		exit();
	}
	private static void exit() {
		pw.close();
		System.out.flush();
		System.err.println("------------------");
		System.err.println("Time: " + ((millis() - t) / 1000.0));
		System.exit(0);
	}
	private static void NO() {
		throw new Error("NO!");
	}
	////////////////////////////////////////////////////////////////////////////////////
	private static String build (String delim, Object o, Object... a) {
		StringBuilder b = new StringBuilder();
		append(b, o, delim);
		for (Object p : a)
			append(b, p, delim);
		return b.toString().trim();
	}
	private static void append(StringBuilder b, Object o, String delim) {
		if (o.getClass().isArray()) {
			int L = java.lang.reflect.Array.getLength(o);
			for (int i : rep(L))
				append(b, java.lang.reflect.Array.get(o, i), delim);
		} else if (o instanceof Iterable<?>)
			for (Object p : (Iterable<?>)o)
				append(b, p, delim);
		else
			b.append(delim).append(o);
	}
	////////////////////////////////////////////////////////////////////////////////////
	private static void start() {
		t = millis();
	}
	private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out, autoflush);
	private static long t;
	private static long millis() {
		return System.currentTimeMillis();
	}
	private static void statics() {
		abs(0); valueOf(0); asList(new Object [0]); reverseOrder();
	}
	public static void main (String[] args) {
		new D();
		exit();
	}
}


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

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		PrintWriter pw=new PrintWriter(System.out);
		while(sc.hasNext()){
			int n=sc.nextInt();
			int w=sc.nextInt();
			int[]shu=new int[n];
			S[]s=new S[n];
			int sum=0;
			for(int i=0;i<n;i++){
				shu[i]=sc.nextInt();
				sum+=(shu[i]+1)/2;
				s[i]=new S(shu[i],i);
			}
			if(sum>w)pw.println(-1);
			else{
				w-=sum;
				int []d=new int[n];
				Arrays.sort(s);
				for(int i=0;i<n;i++){
					if(s[i].s-(s[i].s+1)/2<=w){
						d[s[i].index]=s[i].s-(s[i].s+1)/2;
						w-=s[i].s-(s[i].s+1)/2;
					}else{
							d[s[i].index]=w;
							w=0;
					}
				}
				if(w>0)pw.println(-1);
				else{
					for(int i=0;i<n;i++){
						pw.print(d[i]+(shu[i]+1)/2+" ");
					}
				}
			}
			pw.flush();
		}

	}

}
class S implements Comparable<S>{
	int s;
	int index;
	S(int a,int b){
		s=a;
		index=b;
	}
	@Override
	public int compareTo(S a) {
		if(this.s<a.s)return 1;
		if(this.s>a.s)return -1;
		return 0;
	}
}
class FastScanner {
	BufferedReader br;
	StringTokenizer st;

	public FastScanner() {
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			st = new StringTokenizer("");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

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

	public String next() {
		if (st.hasMoreTokens())
			return st.nextToken();
		try {
			st = new StringTokenizer(br.readLine());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return st.nextToken();
	}

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

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

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

	public String nextLine() {
		String line = "";
		try {
			line = br.readLine();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return line;
	}
}


import java.util.Scanner;

public class A {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in).useDelimiter("(:|\\s)+");
        scanner.nextInt();
        int ta = scanner.nextInt();
        int b = scanner.nextInt();
        int tb = scanner.nextInt();

        int hh = scanner.nextInt();
        int mm = scanner.nextInt();

        int sa = hh*60 + mm;
        int ea = sa + ta;

        int res = 0;
        for (int sb=5*60; sb <= 23*60 + 59; sb+= b) {
            int eb = sb + tb;

            if (eb > sa && ea > sb) res++;
        }
        System.out.print(res);

    }
}


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

public class B354 {
	public static void main(String [] args) throws IOException{
		double[][] g;
		int h;
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());
		h = Integer.parseInt(st.nextToken());
		int sec = Integer.parseInt(st.nextToken());
//		int totCups = cups*(cups+1)/2;
		g = new double[12][12];
		g[1][1]=sec;
		int glasses=0;
		for(int i =1;i<=h;i++){
			for(int j =1;j<=i;j++){
				if(g[i][j]>1){
//					glasses++;
					g[i+1][j]+=(g[i][j]-1)/2;
					g[i+1][j+1]+=(g[i][j]-1)/2;
				}
				if(g[i][j]>=1)
					glasses++;
			}
		}

		System.out.println(glasses);

	}

}


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

public class MainD {
	static final StdIn in = new StdIn();
	static final PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) {
		int n=in.nextInt(), w=in.nextInt();
		Pair[] ps = new Pair[n];
		for(int i=0; i<n; ++i)
			ps[i] = new Pair(in.nextInt(), in.nextInt());
//		List<Pair> p1 = new ArrayList<Pair>(), p2 = new ArrayList<Pair>();
//		for(int i=0; i<n; ++i) {
//			Pair p=new Pair(in.nextInt(), in.nextInt());
//			if(p.a<0)
//				p1.add(p);
//			else
//				p2.add(p);
//		}
//		int m=p2.size();
		Pair[] x = new Pair[n], y = new Pair[n];
		for(int i=0; i<n; ++i) {
//			x[i] = new Pair(p2.get(i).a, -(p2.get(i).b-w));
//			y[i] = new Pair(p2.get(i).a, -(p2.get(i).b+w));
			x[i] = new Pair(Math.abs(ps[i].a), Math.abs(ps[i].b-w));
			y[i] = new Pair(Math.abs(ps[i].a), Math.abs(ps[i].b+w));
		}
		Arrays.sort(x);
		Arrays.sort(y);
		List<Integer>[] toAdd = new List[n];
		for(int i=0; i<n; ++i)
			toAdd[i] = new ArrayList<Integer>();
		for(int i=0; i<n; ++i)
			toAdd[Arrays.binarySearch(y, new Pair(Math.abs(ps[i].a), Math.abs(ps[i].b+w)))]
					.add(Arrays.binarySearch(x, new Pair(Math.abs(ps[i].a), Math.abs(ps[i].b-w))));
		long ans=0;
		FenTree ft = new FenTree(n);
		for(int i=n-1; i>=0; --i) {
			Collections.sort(toAdd[i]);
			for(int ta : toAdd[i]) {
				ans+=ft.qry(ta+1);
				ft.upd(ta, 1);
			}
		}
		out.println(ans);
		out.close();
	}

	static class FenTree {
		int[] a;
		FenTree(int n) {
			a = new int[n+1];
		}
		void upd(int i, int x) {
			for(++i; i<a.length; i+=i&-i)
				a[i]+=x;
		}
		int qry(int i) {
			int r=0;
			for(; i>0; i-=i&-i)
				r+=a[i];
			return r;
		}
	}

	static class Pair implements Comparable<Pair> {
		int a, b;
		Pair(int a, int b) {
			this.a=a;
			this.b=b;
		}
		public int compareTo(Pair o) {
			return Double.compare((double)a/b, (double)o.a/o.b);
		}
	}

	static class StdIn {
		final private int BUFFER_SIZE = 1 << 16;
		private DataInputStream din;
		private byte[] buffer;
		private int bufferPointer, bytesRead;
		public StdIn() {
			din = new DataInputStream(System.in);
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}
		public StdIn(InputStream in) {
			try{
				din = new DataInputStream(in);
			} catch(Exception e) {
				throw new RuntimeException();
			}
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}
		public String next() {
			int c;
			while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
			StringBuilder s = new StringBuilder();
			while (c != -1)
			{
				if (c == ' ' || c == '\n'||c=='\r')
					break;
				s.append((char)c);
				c=read();
			}
			return s.toString();
		}
		public String nextLine() {
			int c;
			while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
			StringBuilder s = new StringBuilder();
			while (c != -1)
			{
				if (c == '\n'||c=='\r')
					break;
				s.append((char)c);
				c = read();
			}
			return s.toString();
		}
		public int nextInt() {
			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 int[] readIntArray(int n, int os) {
			int[] ar = new int[n];
			for(int i=0; i<n; ++i)
				ar[i]=nextInt()+os;
			return ar;
		}
		public long nextLong() {
			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 long[] readLongArray(int n, long os) {
			long[] ar = new long[n];
			for(int i=0; i<n; ++i)
				ar[i]=nextLong()+os;
			return ar;
		}
		public double nextDouble() {
			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() {
			try{
				if (bufferPointer == bytesRead)
					fillBuffer();
				return buffer[bufferPointer++];
			} catch(IOException e) {
				throw new RuntimeException();
			}
		}
		public void close() throws IOException {
			if (din == null)
				return;
			din.close();
		}
	}
}


import java.util.Scanner;

public class FleaTravel {
    public static void main(String[] args) {
        int n;
        Scanner cin = new Scanner(System.in);
        n = cin.nextInt();
        boolean h[] = new boolean[n];
        for (int i = 0; i < n; i++) {
            h[i] = false;
        }
        int x = 0;
        h[x] = true;
        for (int k = 0; k < n; k++) {
            x += k;
            if (x >= n) {
                x -= n;
            }
            h[x] = true;
        }
        for (int i = 0; i < n; i++) {
            if (h[i] == false) {
                System.out.println("NO");
                System.exit(0);
            }
        }
        System.out.println("YES");
    }
}


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

public class CuttingBanner implements Closeable {

    private InputReader in = new InputReader(System.in);
    private PrintWriter out = new PrintWriter(System.out);

    public void solve() {
        char[] codeforces = "CODEFORCES".toCharArray();
        char[] word = in.next().toCharArray();
        int idx = 0, i = 0, j = 0, n = word.length;
        while (idx < Math.min(10, n)) {
            if (word[idx] == codeforces[j]) {
                j++;
            } else break;
            idx++;
        }
        idx = 0;
        while (idx < Math.min(10, n)) {
            if (word[word.length - idx - 1] == codeforces[9 - i]) {
                i++;
            } else break;
            idx++;
        }

        out.println((i + j >= 10) ? "YES" : "NO");
    }

    @Override
    public void close() throws IOException {
        in.close();
        out.close();
    }

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

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

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

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

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

        public void close() throws IOException {
            reader.close();
        }
    }

    public static void main(String[] args) throws IOException {
        try (CuttingBanner instance = new CuttingBanner()) {
            instance.solve();
        }
    }
}


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

public class NextRound {

    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String []w=br.readLine().trim().split("\\s+");
        int n=Integer.parseInt(w[0]);
        int k=Integer.parseInt(w[1])-1;
        String []ww=br.readLine().trim().split("\\s+");
        int c=0;
        for (int i = 0; i < ww.length; i++) {
            if(Integer.parseInt(ww[i])>=Integer.parseInt(ww[k]) && Integer.parseInt(ww[i])>0 ){
                c++;
            }

        }
        System.out.print(c);

    }

}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class ATM
{
	 static class FastReader
	    {
	        BufferedReader br;
	        StringTokenizer st;

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

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

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

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

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

	        String nextLine()
	        {
	            String str = "";
	            try
	            {
	                str = br.readLine();
	            }
	            catch (IOException e)
	            {
	                e.printStackTrace();
	            }
	            return str;
	        }
	    }
	 public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));

  static int MAX = (int) 1e6;
    public static void main(String[] args) {

    	FastReader sc = new FastReader();
    	int a1 = sc.nextInt();
    	int a2 = sc.nextInt();
    	int k1 = sc.nextInt();
    	int k2 = sc.nextInt();
    	int n = sc.nextInt();
    	int min1 = Math.max(0,n-(k1-1)*a1 - (k2-1)*a2);
    	int min = Math.min(a1+a2,min1);
    	int max1 = 0;
    	if(k1 < k2) {
    	   max1+=Math.min(n/k1,a1);
    	   n-=k1*max1;
    	   if(n > 0) {
    		   max1+=Math.min(n/k2, a2);
    	   }
    	}else if(k2 < k1) {
    		max1+=Math.min(n/k2, a2);
    		n-=k2*max1;
    		if(n > 0) {
    			max1+=Math.min(n/k1, a1);
    		}
    	}else {
    		max1+=Math.min(n/k1, a1+a2);
    	}
    	out.print(min+" "+max1);
    	out.close();
    	}
}