import javax.swing.plaf.IconUIResource;
import java.io.*;
import java.util.*;

import static java.lang.Math.*;

public class Main {
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        int n = nextInt();
        int a1 = nextInt();
        int a2 = nextInt();
        int a3 = nextInt();
        int b1 = nextInt();
        int b2 = nextInt();
        int b3 = nextInt();
        int max = min(a1, b2) + min(a2, b3) + min(a3, b1);

        int a11 = a1;
        int a21 = a2;
        int a31 = a3;
        int b11 = b1;
        int b21 = b2;
        int b31 = b3;
        if (b11 > a21) {
            b11 -= a21;
            a21 = 0;
            if (b11 > a11) {
                b11 -= a11;
                a11 = 0;
            } else {
                a11 -= b11;
                b11 = 0;
            }
        } else {
            a21 -= b11;
            b11 = 0;
        }
        if (b21 > a31) {
            b21 -= a31;
            a31 = 0;
            if (b21 > a21) {
                b21 -= a21;
                a21 = 0;
            } else {
                a21 -= b21;
                b21 = 0;
            }
        } else {
            a31 -= b21;
            b21 = 0;
        }
        if (b31 > a11) {
            b31 -= a11;
            a11 = 0;
            if (b31 > a31) {
                b31 -= a31;
                a31 = 0;
            } else {
                a31 -= b31;
                b31 = 0;
            }
        } else {
            a11 -= b31;
            b31 = 0;
        }
        int ans1 = a11 + a21 + a31;

        a11 = a1;
        a21 = a2;
        a31 = a3;
        b11 = b1;
        b21 = b2;
        b31 = b3;

        if (b11 > a21) {
            b11 -= a21;
            a21 = 0;
            if (b11 > a11) {
                b11 -= a11;
                a11 = 0;
            } else {
                a11 -= b11;
                b11 = 0;
            }
        } else {
            a21 -= b11;
            b11 = 0;
        }
        if (b31 > a11) {
            b31 -= a11;
            a11 = 0;
            if (b31 > a31) {
                b31 -= a31;
                a31 = 0;
            } else {
                a31 -= b31;
                b31 = 0;
            }
        } else {
            a11 -= b31;
            b31 = 0;
        }
        if (b21 > a31) {
            b21 -= a31;
            a31 = 0;
            if (b21 > a21) {
                b21 -= a21;
                a21 = 0;
            } else {
                a21 -= b21;
                b21 = 0;
            }
        } else {
            a31 -= b21;
            b21 = 0;
        }
        int ans2 = a11 + a21 + a31;

        a11 = a1;
        a21 = a2;
        a31 = a3;
        b11 = b1;
        b21 = b2;
        b31 = b3;

        if (b21 > a31) {
            b21 -= a31;
            a31 = 0;
            if (b21 > a21) {
                b21 -= a21;
                a21 = 0;
            } else {
                a21 -= b21;
                b21 = 0;
            }
        } else {
            a31 -= b21;
            b21 = 0;
        }
        if (b11 > a21) {
            b11 -= a21;
            a21 = 0;
            if (b11 > a11) {
                b11 -= a11;
                a11 = 0;
            } else {
                a11 -= b11;
                b11 = 0;
            }
        } else {
            a21 -= b11;
            b11 = 0;
        }
        if (b31 > a11) {
            b31 -= a11;
            a11 = 0;
            if (b31 > a31) {
                b31 -= a31;
                a31 = 0;
            } else {
                a31 -= b31;
                b31 = 0;
            }
        } else {
            a11 -= b31;
            b31 = 0;
        }
        int ans3 = a11 + a21 + a31;

        a11 = a1;
        a21 = a2;
        a31 = a3;
        b11 = b1;
        b21 = b2;
        b31 = b3;

        if (b21 > a31) {
            b21 -= a31;
            a31 = 0;
            if (b21 > a21) {
                b21 -= a21;
                a21 = 0;
            } else {
                a21 -= b21;
                b21 = 0;
            }
        } else {
            a31 -= b21;
            b21 = 0;
        }
        if (b31 > a11) {
            b31 -= a11;
            a11 = 0;
            if (b31 > a31) {
                b31 -= a31;
                a31 = 0;
            } else {
                a31 -= b31;
                b31 = 0;
            }
        } else {
            a11 -= b31;
            b31 = 0;
        }
        if (b11 > a21) {
            b11 -= a21;
            a21 = 0;
            if (b11 > a11) {
                b11 -= a11;
                a11 = 0;
            } else {
                a11 -= b11;
                b11 = 0;
            }
        } else {
            a21 -= b11;
            b11 = 0;
        }
        int ans4 = a11 + a21 + a31;

        a11 = a1;
        a21 = a2;
        a31 = a3;
        b11 = b1;
        b21 = b2;
        b31 = b3;

        if (b31 > a11) {
            b31 -= a11;
            a11 = 0;
            if (b31 > a31) {
                b31 -= a31;
                a31 = 0;
            } else {
                a31 -= b31;
                b31 = 0;
            }
        } else {
            a11 -= b31;
            b31 = 0;
        }
        if (b21 > a31) {
            b21 -= a31;
            a31 = 0;
            if (b21 > a21) {
                b21 -= a21;
                a21 = 0;
            } else {
                a21 -= b21;
                b21 = 0;
            }
        } else {
            a31 -= b21;
            b21 = 0;
        }
        if (b11 > a21) {
            b11 -= a21;
            a21 = 0;
            if (b11 > a11) {
                b11 -= a11;
                a11 = 0;
            } else {
                a11 -= b11;
                b11 = 0;
            }
        } else {
            a21 -= b11;
            b11 = 0;
        }
        int ans5 = a11 + a21 + a31;

        a11 = a1;
        a21 = a2;
        a31 = a3;
        b11 = b1;
        b21 = b2;
        b31 = b3;

        if (b31 > a11) {
            b31 -= a11;
            a11 = 0;
            if (b31 > a31) {
                b31 -= a31;
                a31 = 0;
            } else {
                a31 -= b31;
                b31 = 0;
            }
        } else {
            a11 -= b31;
            b31 = 0;
        }
        if (b11 > a21) {
            b11 -= a21;
            a21 = 0;
            if (b11 > a11) {
                b11 -= a11;
                a11 = 0;
            } else {
                a11 -= b11;
                b11 = 0;
            }
        } else {
            a21 -= b11;
            b11 = 0;
        }
        if (b21 > a31) {
            b21 -= a31;
            a31 = 0;
            if (b21 > a21) {
                b21 -= a21;
                a21 = 0;
            } else {
                a21 -= b21;
                b21 = 0;
            }
        } else {
            a31 -= b21;
            b21 = 0;
        }
        int ans6 = a11 + a21 + a31;

        pw.print(min(ans1, min(ans2, min(ans3, min(ans4, min(ans5, ans6))))) + " ");
        pw.println(max);
        pw.close();
    }

    static StringTokenizer st = new StringTokenizer("");
    static BufferedReader br;

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

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

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

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


import java.util.*;
import java.io.*;
import java.math.BigInteger;
import java.text.*;
public class Main implements Runnable {
    static int mod = 1000_000_007;
    static long lim = (long)1e9 + 6;
    static long mod1 = 998244353;
    static boolean fileIO = false;
    static FastScanner f;
    static int inf = 2000_000_000;
    static PrintWriter pw = new PrintWriter(System.out);
    static Scanner S = new Scanner(System.in);
    static long x0; static long y0;
    static long iinf = Long.MAX_VALUE;
    static double eps = (double)1e-4;
    public static void solve() {
        int n = f.ni();
        long r1 = f.nl(); long s1 = f.nl(); long p1 = f.nl();
        long r2 = f.nl(); long s2 = f.nl(); long p2 = f.nl();

        // Find maximum wins
        long min = 0l , max = 0l;
        max = Math.min(p1 , r2) + Math.min(r1 , s2) + Math.min(s1 , p2);

        // Find minimum wins
        min = Math.max(0 , r1 - r2 - p2) + Math.max(0 , s1 - s2 - r2) + Math.max(0 , p1 - p2 - s2);
        pn(min + " " + max);
    }
    public static void main(String[] args)throws IOException {
        new Thread(null , new Main() , "F*uckit" , 1 << 28).start();
    }

/******************************END OF MAIN PROGRAM*******************************************/
    public void run() {
        f = new FastScanner(System.in);
        //pre();
        int t = 1;
        int tt = 1;
        //fw = new FileWriter("!out.txt");
        while(t --> 0) {
            //fw.write("Case #" + (tt++) + ": ");
            //fw.write("\n");
            solve();
        }
        pw.flush();
        pw.close();
        //fw.close();
    }
    public static class FastScanner {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private FastScanner.SpaceCharFilter filter;

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

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

            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int ni() {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

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

            return res * sgn;
        }

        public long nl() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;

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

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

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
    public static void pn(Object o){pw.println(o);}
    public static void pi(Object o){pw.print(o);pw.flush();}
    public static void p(Object o){pw.print(o);}
    public static void pni(Object o){pw.println(o);pw.flush();}
    static int gcd(int a,int b){if(b==0)return a;else{return gcd(b,a%b);}}
    static long gcd(long a,long b){if(b==0l)return a;else{return gcd(b,a%b);}}
    static long lcm(long a,long b){return (a*b/gcd(a,b));}
    static long exgcd(long a,long b){if(b==0){x0=1;y0=0;return a;}long temp=exgcd(b,a%b);long t=x0;x0=y0;y0=t-a/b*y0;return temp;}
    static long pow(long a,long b){long res=1;while(b>0){if((b&1)==1)res=res*a;b>>=1;a=a*a;}return res;}
    static long mpow(long a,long b, long m){long res=1;while(b>0){if((b&1)==1)res=((res%m)*(a%m))%m;b>>=1;a=((a%m)*(a%m))%m;}return res;}
    static long mul(long a , long b){return ((a%mod)*(b%mod)%mod);}
    static long adp(long a , long b){return ((a%mod)+(b%mod)%mod);}
    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;}
    static boolean isPrime(int n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(int i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}
    static HashSet<Long> factors(long n){HashSet<Long> hs=new HashSet<Long>();for(long i=1;i<=(long)Math.sqrt(n);i++){if(n%i==0){hs.add(i);hs.add(n/i);}}return hs;}
    static HashSet<Integer> factors(int n){HashSet<Integer> hs=new HashSet<Integer>();for(int i=1;i<=(int)Math.sqrt(n);i++){if(n%i==0){hs.add(i);hs.add(n/i);}}return hs;}
    static HashSet<Long> pf(long n){HashSet<Long> ff=factors(n);HashSet<Long> ans=new HashSet<Long>();for(Long i:ff)if(isPrime(i))ans.add(i);return ans;}
    static HashSet<Integer> pf(int n){HashSet<Integer> ff=factors(n);HashSet<Integer> ans=new HashSet<Integer>();for(Integer i:ff)if(isPrime(i))ans.add(i);return ans;}
    static int[] inpint(int n){int arr[]=new int[n+1];for(int i=1;i<=n;++i){arr[i]=f.ni();}return arr;}
    static long[] inplong(int n){long arr[] = new long[n+1];for(int i=1;i<=n;++i){arr[i]=f.nl();}return arr;}
    static boolean ise(int x){return ((x&1)==0);}static boolean ise(long x){return ((x&1)==0);}
    static int gnv(char c){return Character.getNumericValue(c);} // Credits - KharYusuf for Multiset Class
    static class MultiSet<T extends Comparable<T>>{public int size=0;public TreeMap<T,Integer> tr;public MultiSet(){tr=new TreeMap<>();}public void add(T u){tr.put(u,tr.getOrDefault(u,0)+1);++size;}public boolean contains(T u){return tr.containsKey(u);}public void clear(){tr.clear();}}
    static int log(long x){return x==1?0:(1+log(x/2));} static int log(int x){return x==1?0:(1+log(x/2));}
    static void sort(int[] a){ArrayList<Integer> l=new ArrayList<>();for(int i:a)l.add(i);Collections.sort(l);for(int i=0;i<a.length;++i)a[i]=l.get(i);}
    static void sort(long[] a){ArrayList<Long> l=new ArrayList<>();for(long i:a)l.add(i);Collections.sort(l);for(int i=0;i<a.length;++i)a[i]=l.get(i);}
    static void sort(ArrayList<Integer> a){Collections.sort(a);}
}


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

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

class TaskB {
    public void solve(int testNumber, InputReader in, OutputWriter out){
	    String input = in.next();
	    int hours = Character.getNumericValue(input.charAt(0)) * 10 + Character.getNumericValue(input.charAt(1));
	    int minutes = Character.getNumericValue(input.charAt(3)) * 10 + Character.getNumericValue(input.charAt(4));
	    if (hours >= 12) hours -= 12;
	    int originalhours = hours * 30;
	    int originalminutes = minutes * 6;
	    out.print(originalhours + (double) originalminutes / 12, originalminutes);
    }
}

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

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

	public int read() {
		if (numChars == -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}
	public String readString() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			if (Character.isValidCodePoint(c))
				res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}

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

	public static boolean isWhitespace(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}
	public String next() {
		return readString();
	}
	public interface SpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}

class OutputWriter {
	private final PrintWriter writer;

	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
	}
	public void print(Object...objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}
	public void close() {
		writer.close();
	}
}


//package CodeForces;

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

public class MathShow {

	static int solve(int n,int arr[],int index,int m)
	{
		if(index==arr.length)
		{
			return 0;
		}
		int ans=0;
		if(arr[index]*n<=m)
		{
			m=m-n*arr[index];
			ans=n;
			return ans+solve(n,arr,index+1,m);
		}
		else
		{
			int temp=m/arr[index];
			return temp;
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s=new Scanner(System.in);
		int n=s.nextInt();
		int k=s.nextInt();
		int m=s.nextInt();
		int arr[]=new int[k];
		int sum=0;
		for(int i=0;i<k;i++)
		{
			arr[i]=s.nextInt();
			sum=sum+arr[i];
		}
		Arrays.sort(arr);
		if(sum*n<=m)
		{
			System.out.println((k+1)*n);
		}
		else
		{
			int ans=0;
			for(int i=0;i<n;i++)
			{
				int p=m;
				if(p>=i*sum)
				{
					p=p-i*sum;
				}
				else
				{
					break;
				}
				int points=i*(k+1);
				for(int j=0;j<k&&p>0;j++)
				{
					if(arr[j]*(n-i)<=p)
					{
						p=p-arr[j]*(n-i);
						points=points+n-i;
					}
					else
					{
						points=points+p/arr[j];
						break;
					}
				}
				ans=Math.max(ans, points);
			}
			System.out.println(Math.max(ans,solve(n,arr,0,m)));
		}

	}

}


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class BPetrAndACombinationLock {
    static class Solution {
        int[]mat = {1, -1};
        //Boolean[][]dp;
        Map<String, Boolean> map;
        public void find(int[] arr) {
            map = new HashMap();
            boolean res = possible(arr, 0, 0);
            System.out.println(res==true? "YES" : "NO");
        }

        boolean possible(int[] arr, int index, int sum) {
            if(index == arr.length) {
                if(sum%360 == 0) {
                    return true;
                }
                return false;
            }

            String key = index + "/" + sum;

            if(map.containsKey(key)) return map.get(key);
            for(int i=0; i<2; i++) {
                if(possible(arr, index+1, sum+(arr[index]*mat[i]))) {
                    map.put(key, true);
                    return true;
                }
            }
            map.put(key, false);
            return false;
        }
    }

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


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

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

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

    static class AClassroomWatch {
        public void solve(int testNumber, FastInput in, FastOutput out) {
            int n = in.readInt();
            IntegerList ans = new IntegerList();
            for (int i = Math.max(0, n - 100); i <= n; i++) {
                if (digits(i) + i == n) {
                    ans.add(i);
                }
            }
            out.println(ans.size());
            for (int i = 0; i < ans.size(); i++) {
                out.append(ans.get(i)).append(' ');
            }
        }

        public int digits(int n) {
            return n == 0 ? 0 : (n % 10 + digits(n / 10));
        }

    }

    static class SequenceUtils {
        public static boolean equal(int[] a, int al, int ar, int[] b, int bl, int br) {
            if ((ar - al) != (br - bl)) {
                return false;
            }
            for (int i = al, j = bl; i <= ar; i++, j++) {
                if (a[i] != b[j]) {
                    return false;
                }
            }
            return true;
        }

    }

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

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

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

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

        public int readInt() {
            int sign = 1;

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

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

            return val;
        }

    }

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

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

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

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

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

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

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

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

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

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

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

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

    }

    static class IntegerList implements Cloneable {
        private int size;
        private int cap;
        private int[] data;
        private static final int[] EMPTY = new int[0];

        public IntegerList(int cap) {
            this.cap = cap;
            if (cap == 0) {
                data = EMPTY;
            } else {
                data = new int[cap];
            }
        }

        public IntegerList(IntegerList list) {
            this.size = list.size;
            this.cap = list.cap;
            this.data = Arrays.copyOf(list.data, size);
        }

        public IntegerList() {
            this(0);
        }

        public void ensureSpace(int req) {
            if (req > cap) {
                while (cap < req) {
                    cap = Math.max(cap + 10, 2 * cap);
                }
                data = Arrays.copyOf(data, cap);
            }
        }

        private void checkRange(int i) {
            if (i < 0 || i >= size) {
                throw new ArrayIndexOutOfBoundsException("invalid index " + i);
            }
        }

        public int get(int i) {
            checkRange(i);
            return data[i];
        }

        public void add(int x) {
            ensureSpace(size + 1);
            data[size++] = x;
        }

        public void addAll(int[] x, int offset, int len) {
            ensureSpace(size + len);
            System.arraycopy(x, offset, data, size, len);
            size += len;
        }

        public void addAll(IntegerList list) {
            addAll(list.data, 0, list.size);
        }

        public int size() {
            return size;
        }

        public int[] toArray() {
            return Arrays.copyOf(data, size);
        }

        public String toString() {
            return Arrays.toString(toArray());
        }

        public boolean equals(Object obj) {
            if (!(obj instanceof IntegerList)) {
                return false;
            }
            IntegerList other = (IntegerList) obj;
            return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1);
        }

        public int hashCode() {
            int h = 1;
            for (int i = 0; i < size; i++) {
                h = h * 31 + Integer.hashCode(data[i]);
            }
            return h;
        }

        public IntegerList clone() {
            IntegerList ans = new IntegerList();
            ans.addAll(this);
            return ans;
        }

    }
}


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

public class Contest94_E {
    static int mod = 1000000007;
    static int comb[][];

    public static void comb(int n){
    comb = new int[n][n];
    for (int i = 0; i < n; i++) {
        comb[i][0] = 1;
    }
    for (int i = 1; i < n; i++) {
        comb[i][i] = 1;
        for (int j = i - 1; j > 0; j--) {
            comb[i][j] = (comb[i - 1][j - 1] + comb[i - 1][j]) %mod;
        }
    }
    }
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] s = in.readLine().split(" ");
        int n = Integer.parseInt(s[0]) - 1;
        int m = Integer.parseInt(s[1]) - 1;
        int k = Integer.parseInt(s[2]);
        comb(Math.max(n, m)+1);
        if(n<2*k || m<2*k)
            System.out.println(0);
        else {
            long c = comb[n][2*k];
            long d = comb[m][2*k];
            System.out.println((d*c)%mod);
        }
    }
}


import java.util.*;
import java.io.*;
public class not{
public static PrintWriter out = new PrintWriter(System.out);//HACER FLUSHHHH
public static void main(String args[]) throws IOException{
BufferedReader lector = new BufferedReader(new InputStreamReader(System.in));
String tmp[] =lector.readLine().split(" ");
int tmpp[] = new int[tmp.length];
String tal[] = {"C","C#","D","D#","E","F","F#","G","G#","A","B","H"};
int res = -1;
for(int n = 0;n<tmp.length;n++)
for(int m = 0;m<tal.length;m++)if(tmp[n].equals(tal[m]))tmpp[n]=m;
for(int a = 0;a<3 && res==-1;a++)
for(int b = 0;b<3 && res==-1;b++)
for(int c = 0;c<3 && res==-1;c++){
if(a==b || b==c || a==c)continue;
int dis1 = tmpp[b]>=tmpp[a]?(tmpp[b]-tmpp[a]):(tal.length-tmpp[a]+tmpp[b]);
int dis2 =tmpp[c]>=tmpp[b]?(tmpp[c]-tmpp[b]):(tal.length-tmpp[b]+tmpp[c]);
//System.out.println(a+" "+b+" "+c+" --> "+dis1+" "+dis2);
if(dis1==4 && dis2==3)res = 0;
if(dis1==3 && dis2==4)res = 1;
}
System.out.println(res==0?"major":(res==1?"minor":"strange"));
}

}


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

public class Main {

	static int[][] h;

	static void getInput(Scanner sc, int n, int m) throws IOException {
		int[] g = new int[n * m + n];
		g[0] = sc.nextInt();
		h = new int[n][m];
		int x = sc.nextInt(), y = sc.nextInt(), z = sc.nextInt();
		for (int i = 1; i < g.length; i++) {
			g[i] = (int) ((g[i - 1] * 1L * x + y) % z);
		}
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++) {
				h[i - 1][j - 1] = g[(i - 1) * m + j - 1];
			}
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner();
		PrintWriter out = new PrintWriter(System.out);
		int n = sc.nextInt(), m = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt();
		getInput(sc, n, m);
		int[][] mins = new int[n][m]; // mins[i][j] = min value from a[i][j] to a[i][j+b-1]
		for (int i = 0; i < n; i++) {
			SparseTable row = new SparseTable(h[i]);
			for (int j = 0; j + b <= m; j++) {
				mins[i][j] = row.query(j, j + b - 1);
			}
		}
		long ans = 0;
		for (int j = 0; j + b <= m; j++) {
			int[] x = new int[n];
			for (int i = 0; i < n; i++)
				x[i] = mins[i][j];
			SparseTable col = new SparseTable(x);
			for (int i = 0; i + a <= n; i++)
				ans += col.query(i, i + a - 1);
		}
		out.println(ans);

		out.close();

	}

	static class SparseTable {
		// table [i][j] = the index with the min value from a[j] to
		// a[j+(1<<i)-1];

		int[] log, a, table[];

		SparseTable(int[] a) {
			this.a = a;
			int n = a.length;
			log = new int[n + 1];
			for (int i = 2; i <= n; i++)
				log[i] = log[i / 2] + 1;
			table = new int[log[n] + 1][n];
			for (int i = 0; i < n; i++)
				table[0][i] = i;
			for (int i = 1, len = 2; len <= n; i++, len <<= 1)
				for (int j = 0; j + len <= n; j++) {
					int u = table[i - 1][j], v = table[i - 1][j + len / 2];
					table[i][j] = a[u] < a[v] ? u : v;
				}

		}

		int query(int l, int r) {
			int len = r - l + 1;
			int lg = log[len];
			int u = table[lg][l];
			int v = table[lg][r - (1 << lg) + 1];
			int bestIdx = a[u] < a[v] ? u : v;
			return a[bestIdx]; // return a[bestIdx] for value
		}

	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

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

		Scanner(String fileName) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(fileName));
		}

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

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

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

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

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

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

	}

}


import java.util.*;
import java.io.*;
public class A
{
      public static void main(String ar[]) throws Exception
      {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String s1[]=br.readLine().split(" ");
            String s2[]=br.readLine().split(" ");
            int n=Integer.parseInt(s1[0]);
            int m=Integer.parseInt(s1[1]);
            int l=Integer.parseInt(s1[2]);
            int r=Integer.parseInt(s1[3]);
            long g=Long.parseLong(s2[0]);
            long x=Long.parseLong(s2[1]);
            long y=Long.parseLong(s2[2]);
            long z=Long.parseLong(s2[3]);

            long a[][]=new long[n][m];
            for(int i=0;i<n;i++)
            {
                  for(int j=0;j<m;j++)
                  {
                        a[i][j]=g;
                        g=(g*x+y)%z;
                  }
            }

            long b[]=new long[n];
            for(int j=0;j<m;j++)
            {
                  Deque<Integer> dq=new LinkedList<Integer>();
                  for(int i=0;i<n;i++)
                   b[i]=a[i][j];
                  for(int i=0;i<l;i++)
                  {
                        while(!dq.isEmpty() && b[i]<=b[dq.peekLast()])
                         dq.pollLast();
                        dq.addLast(i);
                  }

                  for(int i=l;i<n;i++)
                  {
                        a[i-1][j]=b[dq.peek()];
                        while(!dq.isEmpty() && dq.peek()<=i-l)
                         dq.poll();
                        while(!dq.isEmpty() && b[i]<=b[dq.peekLast()])
                         dq.pollLast();
                        dq.addLast(i);
                  }
                  a[n-1][j]=b[dq.peek()];
            }

            long c[]=new long[m];
            for(int i=l-1;i<n;i++)
            {
               for(int j=0;j<m;j++)
                c[j]=a[i][j];
               Deque<Integer> dq=new LinkedList<Integer>();
               for(int j=0;j<r;j++)
               {
                     while(!dq.isEmpty() && c[j]<=c[dq.peekLast()])
                      dq.pollLast();
                     dq.addLast(j);
               }

               for(int j=r;j<m;j++)
               {
                    //System.out.println(i+" "+j);
                    a[i][j-1]=c[dq.peek()];
                    while(!dq.isEmpty() && dq.peek()<=j-r)
                     dq.poll();
                    while(!dq.isEmpty() && c[j]<=c[dq.peekLast()])
                      dq.pollLast();
                    dq.addLast(j);
               }
               a[i][m-1]=c[dq.peek()];
            }

            long S=0;
        //    StringBuffer sb=new StringBuffer();
        //    StringBuffer sb1=new StringBuffer();
            for(int i=l-1;i<n;i++)
            {
            for(int j=r-1;j<m;j++)
            {
              S+=a[i][j];
             //sb.append(b[i][j]).append(" ");
            // sb1.append(c[i][j]).append(" ");
            }
           // sb.append("\n");
      //      sb1.append("\n");
            }
         //   System.out.println(sb);
         //   System.out.println();
         //   System.out.println(sb1);
         //   System.out.println();
            System.out.println(S);
      }
}


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.ArrayDeque;
import java.util.StringTokenizer;

public class OpenStreetMap implements Closeable {

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

  public void solve() {
    int n = in.ni(), m = in.ni(), a = in.ni(), b = in.ni();
    long g = in.nl(), x = in.nl(), y = in.nl(), z = in.nl();
    long[][] table = new long[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[i][j] = g;
        g = (g * x + y) % z;
      }
    }
    ArrayDeque<Long>[] queues = new ArrayDeque[m];
    for (int i = 0; i < m; i++) {
      queues[i] = new ArrayDeque<>();
    }
    for (int i = 0; i < a - 1; i++) {
      for (int j = 0; j < m; j++) {
        long value = table[i][j];
        while (queues[j].size() > 0 && queues[j].peekLast() > value) {
          queues[j].pollLast();
        }
        queues[j].addLast(value);
      }
    }
    long result = 0;
    for (int row = a - 1; row < n; row++) {
      ArrayDeque<Long> window = new ArrayDeque<>();
      if (row >= a) {
        for (int col = 0; col < m; col++) {
          long remove = table[row - a][col];
          if (queues[col].size() > 0 && queues[col].peekFirst() == remove) {
            queues[col].pollFirst();
          }
        }
      }

      for (int col = 0; col < m; col++) {
        long add = table[row][col];
        while (queues[col].size() > 0 && queues[col].peekLast() > add) {
          queues[col].pollLast();
        }
        queues[col].addLast(add);

        long columnMin = queues[col].peekFirst();
        while (window.size() > 0 && window.peekLast() > columnMin) {
          window.pollLast();
        }
        window.addLast(columnMin);
        if (col >= b) {
          long remove = queues[col - b].peekFirst();
          if (window.size() > 0 && window.peekFirst() == remove) {
            window.pollFirst();
          }
        }
        if (col >= b - 1) {
          result += window.peekFirst();
        }

      }
    }
    out.println(result);
  }

  @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 (OpenStreetMap instance = new OpenStreetMap()) {
      instance.solve();
    }
  }
}


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

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

	FastReader in = new FastReader();
	PrintWriter out = new PrintWriter(System.out);
	void run(){
		work();
		out.flush();
	}
	long mod=998244353;
	long gcd(long a,long b) {
		return a==0?b:gcd(b%a,a);
	}
	void work() {
		int n=ni(),m=ni(),a=ni(),b=ni();
		long g=ni(),x=ni(),y=ni(),z=ni();
		long[][] A=new long[n][m];
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				A[i][j]=g;
				g=(g*x+y)%z;
			}
		}
		long[][] rec=new long[n][m];
		long sum=0;
		for(int i=0;i<n;i++) {
			LinkedList<Long> queue=new LinkedList<>();
			for(int j=0;j<m;j++) {
				while(queue.size()>0&&queue.peekLast()>A[i][j]) {
					queue.pollLast();
				}
				queue.add(A[i][j]);
				if(j>=b) {
					if(queue.peekFirst()==A[i][j-b]) {
						queue.pollFirst();
					}
				}
				rec[i][j]=queue.peekFirst();
			}
		}
		for(int j=b-1;j<m;j++) {
			LinkedList<Long> queue=new LinkedList<>();
			for(int i=0;i<n;i++) {
				while(queue.size()>0&&queue.peekLast()>rec[i][j]) {
					queue.pollLast();
				}
				queue.add(rec[i][j]);
				if(i>=a) {
					if(queue.peekFirst()==rec[i-a][j]) {
						queue.pollFirst();
					}
				}
				if(i>=a-1) {
					sum+=queue.peekFirst();
				}
			}
		}
		out.println(sum);
	}

	//input
	private ArrayList<Integer>[] ng(int n, int m) {
		ArrayList<Integer>[] graph=(ArrayList<Integer>[])new ArrayList[n];
		for(int i=0;i<n;i++) {
			graph[i]=new ArrayList<>();
		}
		for(int i=1;i<=m;i++) {
			int s=in.nextInt()-1,e=in.nextInt()-1;
			graph[s].add(e);
			graph[e].add(s);
		}
		return graph;
	}

	private int ni() {
		return in.nextInt();
	}

	private long nl() {
		return in.nextLong();
	}

	private long[] na(int n) {
		long[] A=new long[n];
		for(int i=0;i<n;i++) {
			A[i]=in.nextLong();
		}
		return A;
	}
	private int[] nia(int n) {
		int[] A=new int[n];
		for(int i=0;i<n;i++) {
			A[i]=in.nextInt();
		}
		return A;
	}
}

class FastReader
{
	BufferedReader br;
	StringTokenizer st;

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

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

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

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


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

public class CFD {

    BufferedReader br;
    PrintWriter out;
    StringTokenizer st;
    boolean eof;
    private static long MOD = 1000L * 1000L * 1000L + 7;
    private static final int[] dx = {0, -1, 0, 1};
    private static final int[] dy = {1, 0, -1, 0};
    private static final String yes = "Yes";
    private static final String no = "No";

    int n;
    int m;
    int a;
    int b;
    int LIMIT = 1000 * 1000 + 10;
    long[] fact;
    long[] invfact;
    long[] powN;
    long[] powM;
    void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        a = nextInt();
        b = nextInt();

        fact = new long[LIMIT];
        invfact = new long[LIMIT];
        fact[0] = 1;
        for (int i = 1; i < LIMIT; i++) {
            fact[i] = fact[i - 1] * i % MOD;
        }

        invfact[LIMIT - 1] = powMod(fact[LIMIT - 1], MOD - 2, MOD);
        for (int i = LIMIT - 2; i >= 0; i--) {
            invfact[i] = invfact[i + 1] * (i + 1) % MOD;
        }

        powN = new long[LIMIT];
        powM = new long[LIMIT];
        powN[0] = 1;
        powM[0] = 1;
        for (int i = 1; i < LIMIT; i++) {
            powN[i] = n * powN[i - 1] % MOD;
            powM[i] = m * powM[i - 1] % MOD;
        }

        long res = 0;
        for (int i = 0; i <= Math.min(m - 1, n - 2); i++) {
            int left = n - (i + 2);
            // (n - i - 1) * (n - i) *...* (n - 2)
            long extra = fact[n - 2] * invfact[n - i - 2] % MOD;
            extra *= comb(m - 1, i);
            extra %= MOD;
            extra *= caley(n, i + 2);
            extra %= MOD;
            extra *= powM[left];
            extra %= MOD;
            res += extra;
            if (res >= MOD) {
                res -= MOD;
            }

            if (res >= MOD) {
                res -= MOD;
            }
        }

        outln(res % MOD);
    }

    long caley(int n, int k) {
        if (k >= n) {
            return 1;
        }
        return k * powN[n - k - 1] % MOD;
    }

    long comb(int n, int r) {
        long res = fact[n] * invfact[r] % MOD;
        res = res * invfact[n - r] % MOD;
        return res;
    }

    // return 1/a module MOD, which is a ^ (MOD - 2) % MOD
    long inverseMod(long a, long MOD) {
        return powMod(a, MOD - 2, MOD);
    }

    // Power Calculate N^M % MOD, N and M can be long (up to 2^64, ~10^18)
    public long powMod(long N, long M, long MOD){//N^M % MOD
        if(M == 0L)
            return 1L;
        long[] hp = new long[64];
        boolean[] bp = new boolean[64];
        hp[0] = N;
        for(int i = 1; i < hp.length; i++) {
            hp[i] = (hp[i - 1] * hp[i - 1]) % MOD;
        }
        for(int j = 0; j < hp.length; j++) {
            if((M & (1L << j)) != 0)
                bp[j] = true;
        }
        long res = 1;
        for(int i = 0;i < bp.length; i++){
            if(bp[i]) {
                res = (res * hp[i]) % MOD;
            }
        }
        return res;
    }

    void shuffle(int[] a) {
        int n = a.length;
        for(int i = 0; i < n; i++) {
            int r = i + (int) (Math.random() * (n - i));
            int tmp = a[i];
            a[i] = a[r];
            a[r] = tmp;
        }
    }
    long gcd(long a, long b) {
        while(a != 0 && b != 0) {
            long c = b;
            b = a % b;
            a = c;
        }
        return a + b;
    }
    private void outln(Object o) {
        out.println(o);
    }
    private void out(Object o) {
        out.print(o);
    }
    private void formatPrint(double val) {
        outln(String.format("%.9f%n", val));
    }
    public CFD() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }
    public static void main(String[] args) throws IOException {
        new CFD();
    }

    public long[] nextLongArr(int n) throws IOException{
        long[] res = new long[n];
        for(int i = 0; i < n; i++)
            res[i] = nextLong();
        return res;
    }
    public int[] nextIntArr(int n) throws IOException {
        int[] res = new int[n];
        for(int i = 0; i < n; i++)
            res[i] = nextInt();
        return res;
    }
    public String nextToken() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                eof = true;
                return null;
            }
        }
        return st.nextToken();
    }
    public String nextString() {
        try {
            return br.readLine();
        } catch (IOException e) {
            eof = true;
            return null;
        }
    }
    public int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }
    public long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }
    public double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }
}


import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;

   public class E
   {
      public static void main(String hi[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int N = Integer.parseInt(st.nextToken());
         BigInteger res = new BigInteger("1");
         for(int i=N-1; i >= 1; i--)
            res = res.multiply(new BigInteger(i+""));
         res = res.divide(new BigInteger(N/2+""));
         System.out.println(res.longValue());
      }
   }


import java.util.Scanner;

public class Main {

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

    int p = 1000000009;

    private void solve() {
        Scanner sc = new Scanner (System.in);

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

        int howMany = 1;
        for (int i = 0; i < m; ++i)
        {
            howMany <<= 1;
            if (howMany >= p)
                howMany %= p;
        }

        howMany--;

        long ans = 1;
        for (int i = 0; i < n; ++i)
        {
            ans *= howMany;
            ans %= p;
            howMany--;
        }

        System.out.println(ans);
    }

}


import java.util.Scanner;

public class C {
    static long factorial[] = new long[(int) 2e5 + 3];
    static long inverseFactorial[] = new long[(int) 2e5 + 3];
    static int MOD = (int) 1e9 + 7;

    public static void main(String[] args) {
        factorial[0] = 1;
        inverseFactorial[0] = 1;
        for (int i = 1; i <= (int) 2e5; i++) {
            factorial[i] = mul(factorial[i - 1], i);
            inverseFactorial[i] = binpow(factorial[i], MOD - 2);
        }
        Scanner scn = new Scanner(System.in);
        int test = scn.nextInt();
        while (test-- > 0) {
            int n = scn.nextInt();
            int k = scn.nextInt();
            long[] dp = new long[k + 1];
            dp[k] = 1;
            long equal = 0;
            long moreThan = 0;
            if (n % 2 != 0)
                equal = 1l;
            else moreThan = 1l;
            for (int i = 0; i < n; i += 2) {
                equal = add(equal, nCr(n, i));
            }

            for (int i = k - 1; i >= 0; i--) {
                dp[i] = mul(equal, dp[i + 1]);
                dp[i] = add(dp[i], mul(moreThan, binpow(2, 1l*n * (k - i - 1))));
            }
            System.out.println(dp[0]);
        }

    }

    static long add(long a, long b) {
        if ((a += b) >= MOD)
            a -= MOD;
        else if (a < 0)
            a += MOD;
        return a;
    }

    static long mul(long x, long y) {
        return (1L * x * y) % MOD;
    }

    static long nCr(int n, int r) {
        if (r > n)
            return 0;
        return mul(mul(factorial[n], inverseFactorial[r]), inverseFactorial[n - r]);
    }

    public static long binpow(long x, long n) {
        assert (n >= 0);
        x %= MOD;  // note: MOD * MOD must be less than 2^63 to avoid ll overflow
        long res = 1;
        while (n > 0) {
            if (n % 2 == 1)  // if n is odd
                res = res * x % MOD;
            x = x * x % MOD;
            n /= 2;  // divide by two
        }
        return res;
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Collections;
import java.io.InputStream;

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

    static class TaskA {
        public void solve(int testNumber, FastReader in, PrintWriter out) {
            int n = in.nextInt(), m = in.nextInt(), k = in.nextInt(), f = 0;
            Integer[] a = new Integer[n];
            for (int i = 0; i < n; ++i) a[i] = in.nextInt();
            Arrays.sort(a, Collections.reverseOrder());
            if (m <= k) {
                out.println(0);
                return;
            }
            int cur = 0, cnt = 0;
            while (m > k && cur < n) {
                k += a[cur] - 1;
                if (m <= k) {
                    out.println(cur + 1);
                    return;
                }
                cur++;
            }
            out.println(-1);

        }

    }

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

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

        public int read() {

            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {

                curChar = 0;

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

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

            return buf[curChar++];
        }

        public int nextInt() {

            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;

            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();

                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public boolean isSpaceChar(int c) {

            if (filter != null)
                return filter.isSpaceChar(c);

            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

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

        }

    }
}


import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;

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

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

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

	        }

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

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

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

	        String nextLine()
	        {
	            String str = "";
	            try
	            {
	                str = br.readLine();
	            }
	            catch (IOException e)
	            {
	                e.printStackTrace();
	            }
	            return str;
	        }

			public Character charAt(int i) {
				// TODO Auto-generated method stub
				return null;
			}
	    }

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

	/**
	 * @param args
	 * @throws IOException
	 */

	public static void main(String[] args)throws IOException  {
		FastReader s=new FastReader();
		int n = s.nextInt();
		int m = s.nextInt();
		int min = (n+1)/2;
		int flag=0;
		for(int i=min;i<=n;i++) {
			if(i%m==0) {
				System.out.println(i);
				flag=1;
				break;
			}
		}
		if(flag==0)System.out.println("-1");
	}
}


import java.util.Scanner;

public class A {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        solve(n,n);
    }

    public static void solve(long n, long sum) {
        if (n == 1) {
            System.out.println(sum);
            System.exit(0);
        }
        if (n % 2 == 0)
            solve(n / 2, sum + n / 2);
        else {
            for (int i = 3; i < Math.sqrt(n)+1; i += 2) {
                if (n % i == 0)
                    solve(n / i, sum + n / i);
            }
            solve(1, sum+1);
        }
    }

}


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

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

class TaskC {
	public void solve(int testNumber, InputReader in, PrintWriter out) {
        int ans = 0;
        long n = in.readLong();
        for (int i = 1; numberOfCards(i) <= n; ++i) {
            long dif = n - numberOfCards(i);
            if (dif % 3 == 0) {
                ++ans;
            }
        }
        out.println(ans);
	}

    long numberOfCards(long stages) {
        return (1 + stages) * stages * 3 / 2 - stages;
    }
}

class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[102400]; //a byte buffer to store the characters in
    private int curChar; //to hold the count of current character
    private int numChars; //number of characters

    public InputReader(InputStream stream) { //pass the inputstream class
        this.stream = stream;
    }
    public final int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf); //this is the implicit function present in the inputStream which reads a Character into a temporary buffer
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

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

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


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

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

		int n= in.nextInt();
		double res=0;
		int ares=0;
		int bres=0;
		for (int a = 0; a < n; a++) {
			for (int b = 0; b < n; b++) {
				if(a+b==n){
					int g= gcd(a,b);
					if(g!=1)continue;
					double cur= (double) a/b;
					if(cur>res&&cur<1){
						ares=a;
						bres=b;
						res= cur;
					}
				}
			}
		}
		System.out.println(ares+" "+ bres);

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

	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

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

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

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


import java.util.*;
import java.util.regex.*;
import java.io.*;
import java.math.BigInteger;
import static java.lang.Math.*;

public class Sol implements Runnable {
	final long MOD = 1000000007;
	long half = (MOD + 1) / 2;

	void solve() throws Exception {
		int L = nextInt(), R = nextInt();
		long ans = ((getGoodAll(R) - getGoodAll(L - 1) + MOD) * half) % MOD;
		out.println(ans);
	}

	long getAll(int R) {
		long res = get(1);
		if (R == 1) return res;
		res += 2 * getK(R) + 4 * getK(1);
		res %= MOD;
		long add1 = R >= 3 ? 6 * (mpow(3, (R - 1) / 2) - 1) : 0;
		long add2 = R >= 4 ? 3 * (mpow(3, R / 2) - 3) : 0;
		return (res + add1 + add2) % MOD;
	}

	long getGoodAll(int R) {
		if (R == 0) return 0;
		long res = getAll(R);
		res += getAll((R + 1) / 2);
		return res % MOD;
	}

	long get(int n) {
		return 2 * getK(n) + 2 * getY(n);
	}

	long getY(int n) {
		return n == 1 ? 1 : 2 * getK(n - 1);
	}

	long getK(int n) {
		if (n % 2 == 0) {
			return (2 * mpow(3, n / 2 - 1)) % MOD;
		} else {
			return mpow(3, n / 2);
		}
	}

	long mpow(long a, long n) {
		if (n == 0) return 1;
		long ans = mpow(a, n / 2);
		ans = (ans * ans) % MOD;
		if (n % 2 == 1) {
			ans = (ans * a) % MOD;
		}
		return ans;
	}

	public static void main(String[] args) {
		new Thread(new Sol()).start();
	}

	public void run() {
		try {
			Locale.setDefault(Locale.US);
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			solve();

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

	StringTokenizer tokenizer = new StringTokenizer("");
	BufferedReader in;
	PrintWriter out;
	long time;

	void sTime() {
		time = System.currentTimeMillis();
	}

	long gTime() {
		return System.currentTimeMillis() - time;
	}

	void gMemory() {
		debug("Memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000 + " kb");
	}

	public void debug(Object o) {
		System.err.println(o);
	}

	boolean seekForToken() {
		while (!tokenizer.hasMoreTokens()) {
			String s = null;
			try {
				s = in.readLine();
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (s == null)
				return false;
			tokenizer = new StringTokenizer(s);
		}
		return true;
	}

	String nextToken() {
		return seekForToken() ? tokenizer.nextToken() : null;
	}

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

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

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

	BigInteger nextBig() {
		return new BigInteger(nextToken());
	}
}


import java.util.Scanner;

public class C_135B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long a[] = new long[19];
        for (int i = 1; i < 19; i++) {
            String s = "";
            for (int j = 0; j < i; j++) {
                s+='9';
            }
            a[i] = Long.parseLong(s);
        }
        long d = sc.nextLong();
        long p = sc.nextLong();
        long ans = d;
        for (int i = 1; i <= 18 ; i++) {
            if(d >= a[i]){
                int t = ((a[i]+"").length());
                long z = (long) ((long) (d-a[i]) /(long) Math.pow(10, t) *(long) Math.pow(10, t) + a[i]);
                if(d - z <= p) {
                    ans = z;
                }
            }
        }
        System.out.println(ans);
    }
}


import java.util.Scanner;

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

		int n;
		int [] arr = {0,2,3,1,2,1};
		n = sc.nextInt();
		System.out.println(arr[n]);
	}
}


import java.util.Scanner;

public class FirstJava {
    public static void main(String[] args) {
    Scanner aa=new Scanner(System.in);
    long n=aa.nextInt();
    long m=aa.nextInt();
    long z=0;

     if(m==1)
        z=n-1;
    else if(n==1)
        z=m-1;
    else
        z=(n*m)-n;

        System.out.println(z);

    }
}


import java.util.Scanner;

public class a {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int len = in.nextInt();
		int start = in.nextInt();
		int rate = in.nextInt();
		int ans = 0;
		while(true){
			ans++;
			if(start*rate >= len){
				break;
			}
			else{
				start *= rate;
			}
		}
		System.out.println(ans);
	}
}


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

public class E {
	FastScanner in;
	PrintWriter out;

	int[] cx = new int[] { 2, 1, -1, -2, -1, 1 };
	int[] cy = new int[] { 0, 2, 2, 0, -2, -2 };

	int[] dirx = new int[] { -1, -2, -1, 1, 2, 1 };
	int[] diry = new int[] { 2, 0, -2, -2, 0, 2 };

	long calc(long i) {
		return 3 * (i + 1) * i;
	}

	void solve() {
		long n = in.nextLong();
		if (n == 0) {
			out.println("0 0");
			return;
		}
		long l = 0, r = 1000 * 1000 * 1000;
		while (r - l > 1) {
			long m = (l + r) / 2;
			if (calc(m) < n) {
				l = m;
			} else {
				r = m;
			}
		}
		long i = l + 1;
		long cnt = calc(l);
	//	System.err.println("circle " + i);
		for (int j = 0; j < 6; j++) {
			if (cnt + i >= n) {
				long stx = cx[j] * i, sty = cy[j] * i;
				// System.out.println(stx + " " + sty);
				long num = n - cnt;
				long rx = stx + num * dirx[j], ry = sty + num * diry[j];
				out.println(rx + " " + ry);
				// System.out.println(num);
				return;
			}
			cnt += i;
		}
		return;
	}

	void run() {
		in = new FastScanner();
		out = new PrintWriter(System.out);

		solve();

		out.close();
	}

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

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

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(in.br.readLine());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

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

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

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

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


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

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

    static class ARomanAndBrowser {
        public void solve(int testNumber, inputClass sc, PrintWriter out) {
            int n = sc.nextInt();
            int k = sc.nextInt();
            int[] tab = new int[n + 1];
            int act1 = 0;
            int act2 = 0;
            for (int i = 1; i <= n; i++) {
                tab[i] = sc.nextInt();
                if (tab[i] == -1) {
                    act1++;
                } else {
                    act2++;
                }
            }
            int ans = 0;

            for (int b = 1; b <= n; b++) {
                act1 = 0;
                act2 = 0;
                for (int c = 1; c <= n; c++) {
                    if (Math.abs(c - b) % k == 0) {
                        continue;
                    } else {
                        if (tab[c] == -1) {
                            act1++;
                        } else {
                            act2++;
                        }
                    }
                }
                ans = Math.max(ans, Math.abs(act1 - act2));
            }
            out.println(ans);

        }

    }

    static class inputClass {
        BufferedReader br;
        StringTokenizer st;

        public inputClass(InputStream in) {

            br = new BufferedReader(new InputStreamReader(in));
        }

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

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

    }
}


import java.io.*;

public class SchoolTeamContest {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        args = br.readLine().split(" ");

        int n = Integer.parseInt(args[0]), a = Integer.parseInt(args[3]), b = Integer
                .parseInt(args[2]), c = Integer.parseInt(args[1]);
        int count = 0;
        for (int i = 0; i <= a; i++) {
            for (int j = 0; j <= b; j++) {
                if (i * 2 + j > n)
                    break;
                if (c / 2 >= n - (i * 2 + j)) count ++;
            }
        }
        System.out.println(count);

    }

}


/*
 * Code Author : Nachiket V Trivedi
 * University  : DAIICT
 */
import java.util.*;
import java.io.*;
import java.math.*;
public class TennisTournament
{
	public static void main(String[] args)
	{
		InputReader in=new InputReader(System.in);
		OutputStream outputStream = System.out;
		//-----start writing from here-----//
		int n=in.nextInt();
		int b=in.nextInt();
		int p=in.nextInt();
		int match=0;
		int m=n;
		int flag=0;
		while(m>1)
		{

			int tk=1;
			int k=1;
			while(tk<m)
			{
				tk=(int) Math.pow(2, k);
				k++;
			}
			flag+=tk/2;
			m=tk/2+(m-tk);

		}
		match=flag;
		System.out.println((2*b+1)*match+" "+(n*p));
	}

	//InputReader class starts from here
	public static class InputReader
	{
		public BufferedReader reader;
		public StringTokenizer tokenizer;

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

		public String nextLine()
		{
			String fullLine=null;
			while (tokenizer == null || !tokenizer.hasMoreTokens())
			{
				try
				{
					fullLine=reader.readLine();
				}
				catch (IOException e)
				{
					throw new RuntimeException(e);
				}
				return fullLine;
			}
			return fullLine;
		}
		public String next()
		{
			while (tokenizer == null || !tokenizer.hasMoreTokens())
			{
				try
				{
					tokenizer = new StringTokenizer(reader.readLine());
				}
				catch (IOException e)
				{
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}
		public long nextLong()
		{
			return Long.parseLong(next());
		}
		public int nextInt()
		{
			return Integer.parseInt(next());
		}
	}

}


	// Code Author: Rutul Patel
import java.io.*;
	import java.math.BigInteger;
	import java.math.RoundingMode;
	import java.text.DecimalFormat;
	import java.util.*;

public class Main {

		static double eps=(double)1e-6;
		static long mod=(int)1e9+6;
		static ArrayList<Integer>[] list=new ArrayList[1000003];
		static int[] dist=new int[1000003];
		public static void main(String args[]) throws IOException{
			InputReader sc = new InputReader(System.in);
			OutputStream outputStream = System.out;
			PrintWriter out = new PrintWriter(outputStream);
			//-------------------------------------

			int[][] arr=new int[5][5];
			for(int i=1;i<=4;i++)
			{
				for(int j=1;j<=4;j++)
					arr[i][j]=sc.nextInt();
			}

			boolean[] can=new boolean[5];
			for(int i=1;i<=4;i++)
			{
				for(int j=1;j<=3;j++)
				{
					if(arr[i][j]==1)
						can[i]=true;
					if(j==1 && arr[i][j]==1)
					{
						int l=i-1;
						if(l==0)
							l=4;
						can[l]=true;
					}
					else if(j==2 && arr[i][j]==1)
					{
						int s=(i+2)%4;
						if(s==0)
							s=4;
						can[s]=true;
					}
					else if(arr[i][j]==1)
					{
						int r=(i+1)%4;
						if(r==0)
							r=4;
						can[r]=true;
					}
				}
				/*for(int k=1;k<=4;k++)
					out.print(can[k]+" ");
				out.println();*/
			}
			/*for(int i=1;i<=4;i++)
				out.print(can[i]+" ");*/
			boolean ans=true;
			for(int i=1;i<=4;i++)
			{
				if(arr[i][4]==1 && can[i])
				{
					ans=false;
					break;
				}
			}
			if(ans)
				out.println("NO");
			else
				out.println("YES");
			//-------------------------------------
			out.close();
		}

		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) {
				// TODO Auto-generated method stub
				if (this.a != o.a)
					return Integer.compare(this.a, o.a);
				else
					return Integer.compare(this.b, o.b);
				//return 0;
			}

			public boolean equals(Object o) {
				if (o instanceof Pair) {
					Pair p = (Pair) o;
					return p.a == a && p.b == b;
				}
				return false;
			}

			public int hashCode() {
				return new Integer(a).hashCode() * 31 + new Integer(b).hashCode();
			}

			public String toString() {
				return a + " " + b;
			}
		}

		static int[] sieve(int n,int[] arr)
		{
			for(int i=2;i*i<=n;i++)
			{
				if(arr[i]==0)
				{
					for(int j=i*2;j<=n;j+=i)
						arr[j]=1;
				}
			}
			return arr;
		}

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

		static long lcm(long a, long b)
		{
		    return a * (b / gcd(a, b));
		}

		static long gcd(long a, long b)
		{
		    while (b > 0)
		    {
		        long temp = b;
		        b = a % b; // % is remainder
		        a = temp;
		    }
		    return a;
		}
		static long modulo(long a,long b,long c) {
			long x=1;
			long y=a;
			while(b > 0){
				if(b%2 == 1){
					x=(x*y)%c;
				}
				y = (y*y)%c; // squaring the base
				b /= 2;
			}
			return  x%c;
		}
		public static void debug(Object... o) {
			System.out.println(Arrays.deepToString(o));
		}

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

		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 int nextInt()throws IOException{return (int)nextLong();}
			public final long nextLong()throws IOException{
				int c=read();
				while(isSpaceChar(c)){
					c=read();
					if(c==-1) throw new IOException();
				}
				boolean negative=false;
				if(c=='-'){
					negative=true;
					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 negative?(-res):(res);
			}
			public final int[] readIntBrray(int size)throws IOException{
				int[] arr=new int[size];
				for(int i=0;i<size;i++)arr[i]=nextInt();
				return arr;
			}
			public final String next()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 String nextLine()throws IOException{
				int c=read();
				while(isSpaceChar(c))c=read();
				StringBuilder res=new StringBuilder();
				do{
					res.append((char)c);
					c=read();
				}while(c!='\n'&&c!=-1);
				return res.toString();
			}
			private boolean isSpaceChar(int c){
				return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
			}
		}
	}


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

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

    static class Task {
        public void solve(InputReader in, PrintWriter out) {
            double l = in.nextInt();//2 5
            double d = in.nextInt();//1 4
            double v = in.nextInt();//3 3
            double g = in.nextInt();//4 1
            double r = in.nextInt();//5 1
            double t1 = d / v;
            if (t1 < g) {
                out.println(l / v);
            } else {
                out.println((g + r) * Math.ceil(t1 / (g + r)) + (l - d) / v);
            }
        }
    }

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

        public String nextLine() {
            try {
                return reader.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

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

    }
}


import java.util.*;

public class StringTyping {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		String s = scan.next();
		int ans = 0;
		boolean bal = false;
		for(int i = n-1; i >= 0; i += 0){
			if(i % 2 == 1 && !bal){
				int mid = i/2;
				String a = s.substring(0, mid+1);
				String b = s.substring(mid+1, i+1);
				if(a.equals(b)){
				    bal = true;
					ans++;
					i /= 2;
					continue;
				}
			}
			ans++;
			i--;
		}
		System.out.println(ans);
	}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

/**
 * 1040A
 */
public class _1040A {
	public static void main(String[] args) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		int[] array = convert(reader.readLine());
		int n = array[0];

		int[] c = new int[]{array[1], array[2]};

		int[] a = convert(reader.readLine());
		int sum = 0;
		for (int i = 0; i < n / 2; i++) {
			int first = a[i];
			int last = a[n - i - 1];

			if (first == last) {
				if (first == 2) {
					sum += 2 * Math.min(array[1], array[2]);
				}
				continue;
			}
			if (first == 2 || last == 2) {
				int min = Math.min(first, last);
				sum += c[min];
			} else {
				System.out.println(-1);
				return;
			}
		}
		if ((n % 2 != 0 || n < 2) && a[n / 2] == 2) {
			sum += Math.min(array[1], array[2]);
		}
		System.out.println(sum);
	}

	private static int[] convert(String s) {
		return Arrays.stream(s.split(" ")).mapToInt(Integer::valueOf).toArray();
	}
}


import java.util.*;
public class Main {
	public static void main(String args[])
	{
		int mod = 1000000007;
		Scanner scan = new Scanner(System.in);
		long a=scan.nextInt(),b= scan.nextInt();
		long answer = 0;
		for(long i=1;i<=a;i++)
		{
			long c = (i*b+1)%mod;
			long d = (b*(b-1)/2)%mod;
			answer=(answer+c*d)%mod;
		}
		System.out.println(answer);
	}

}


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

public class JavaPuzzler {

    //Scanner sc;
    private FastScanner sc;
    private PrintWriter out;

    int[] nums;

    public void solve() {
        int n = sc.nextInt();
        int x = sc.nextInt();
        nums = new int[101];
        for (int i = 0; i < n; ++i) {
            ++nums[sc.nextInt()];
        }
        int cnt = 0;
        for (int i = 0; i < x; ++i) {
            if (nums[i] == 0) ++cnt;
        }
        if (nums[x] > 0) ++cnt;
        out.println(cnt);
    }

    private void run() {
//        try {
//            sc = new FastScanner(new File("input.txt"));
////            out = new PrintWriter(new File("output.txt"));
//        out = new PrintWriter(System.out);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

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

        solve();

        out.close();

    }

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

    private class FastScanner {

        BufferedReader br;
        StringTokenizer st;

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

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

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

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

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

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

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


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

public class C774E {

    static StringBuffer ans1 = new StringBuffer("");
    static FastScanner sc = new FastScanner();
    static PrintWriter printWriter = new PrintWriter(System.out);

    public static void solve(int n, int m) {
        int depth = 0, temp = n;
        while(temp > 1) {
            temp /= 2;
            depth++;
        }
        int[] dp = new int[depth + 1];
        boolean[] visited = new boolean[m * depth + 1];
        int size = 0;
        for (int i = 1; i <= depth; i++) {
            for(int j = 1; j <= m; j++) {
                if(!visited[i * j]) size++;
                visited[i * j] = true;
            }
            dp[i] = size;
        }
        long res = 1;
        boolean[] used = new boolean[n + 1];
        for(int i = 2; i <= n; i++) {
            if(used[i]) continue;
            int k = i, count = 0;
            while(k <= n) {
                used[k] = true;
                count++;
                if(k * 1L * i != k * i) break;
                k *= i;
            }
            res += dp[count];
        }
        printWriter.println(res);
    }

    public static void main(String[] args) {
        int n = sc.nextInt();
        int m = sc.nextInt();
        solve(n, m);
        printWriter.flush();
    }

    /*****************************************************************
     ******************** DO NOT READ AFTER THIS LINE *****************
     *****************************************************************/
    static class FastScanner {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

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

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

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

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

        int[][] read2dArray(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;
        }

        ArrayList<Integer> readArrayList(int n) {
            ArrayList<Integer> arr = new ArrayList<Integer>();
            for (int i = 0; i < n; i++) {
                int a = nextInt();
                arr.add(a);
            }
            return arr;
        }

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


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

public class C {

	static BufferedReader br;
	static PrintWriter out;
	static StringTokenizer tokenizer;

	static int[]a = new int[4];
	static int[]b = new int[4];

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

		for(int i=0;i<4;i++){
			tokenizer = new StringTokenizer(br.readLine());
			a[i] = Integer.parseInt(tokenizer.nextToken());
			b[i] = Integer.parseInt(tokenizer.nextToken());
		}

		boolean canWin1 = false;
		boolean canWin2 = false;
		//out.append(win(0,1,2,3)+" "+win(0,1,3,2)+" "+win(1,0,2,3)+" "+win(1,0,3,2)+"\n");
		//out.append(win(2,3,0,1)+" "+win(3,2,0,1)+" "+win(2,3,1,0)+" "+win(3,2,1,0)+"\n");
		if((win(0,1,2,3) && win(0,1,3,2)) || (win(1,0,2,3) && win(1,0,3,2))) canWin1 = true;
		if((win(2,3,0,1) || win(3,2,0,1)) && (win(2,3,1,0) || win(3,2,1,0))) canWin2 = true;

		//out.append("CANWIN1="+canWin1+"\n");
		//out.append("CANWIN2="+canWin1+"\n");

		if(canWin1)out.append("Team 1");
		else if(canWin2)out.append("Team 2");
		else out.append("Draw");
		out.flush();
		out.close();
	}

	public static boolean win(int p1,int p2, int p3, int p4){
		if(a[p1]>b[p3] && b[p2]>a[p4])return true;
		if(a[p1]<b[p3] && b[p2]<a[p4])return false;
		return false;
	}

}


import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
import java.util.concurrent.SynchronousQueue;

import javax.print.DocFlavor.INPUT_STREAM;

public class name
{

	static int pd(long n)
	{
		long i;
		/*
		 * if(n==1) return 1;
		 */
		for (i = 2; i * i <= n; i++)
		{
			if (n % i == 0)
				break;
		}
		if (i * i > n)
			return 1;
		return 0;
	}

	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		String s;
		Scanner sc = new Scanner(System.in);
		long n=0;
	//	while (sc.hasNext())
		{
			n=sc.nextLong();
			int count = 0;
			if (pd(n) == 1)
			{
				System.out.println(1);
			}
			else if(n%2==0||pd(n-2)==1||pd(n-3)==1||pd(n-5)==1)
			{
				System.out.println(2);
			}
			else
			{
				System.out.println(3);
			}
			//System.out.println(count);
		}
		// System.out.println(count);
	}

}


import java.util.Scanner;

public class JavaApplication35 {

    public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
   int a=input.nextInt();int b=input.nextInt();int c=input.nextInt();
int a1=0;int a2=0;int a3=0;
   while(a>=1 && b>=2 && c>=4){
a=a-1;
b=b-2;
c=c-4;
a1++;
a2+=2;
a3+=4;
   }
    System.out.println(a1+a2+a3);
    }

}


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

import static java.lang.Math.*;

public class Main implements Runnable {

	//«2», «3», «4», «5», «6», «7», «8», «9», «T», «J», «Q», «K», «A».

	class Triplet {
		String card1, card2, card3;

		public Triplet(String card1, String card2, String card3) {
			this.card1 = card1;
			this.card2 = card2;
			this.card3 = card3;
		}

		@Override
		public boolean equals(Object obj) {
			if (obj instanceof Triplet) {
				Triplet t = (Triplet)obj;
				return this.card1.equals(t.card1) && this.card2.equals(t.card2) && this.card3.equals(t.card3);
			} else {
				return false;
			}
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			return card1.hashCode() + prime * (card2.hashCode() + prime * card3.hashCode());
		}
	}

	boolean mayPutOn(String top, String bottom) {
		return top.charAt(0) == bottom.charAt(0) || top.charAt(1) == bottom.charAt(1);
	}

	void solve() throws Exception {
		int n = sc.nextInt();
		String[] cards = new String[n];
		for (int i = 0; i < n; i++) {
			cards[i] = sc.nextToken();
		}
		if (n == 1 || n == 2 && mayPutOn(cards[1], cards[0])) {
			out.println("YES");
			return;
		}
		if (n == 1 || n == 2) {
			out.println("NO");
			return;
		}
		HashSet<Triplet>[] dp = new HashSet[n];
		for (int i = 0; i < n; i++) {
			dp[i] = new HashSet<Triplet>();
		}
		dp[n - 1].add(new Triplet(cards[n - 1], cards[n - 2], cards[n - 3]));
		for (int i = n - 1; i >= 3; i--) {
			for (Triplet t : dp[i]) {
				if (mayPutOn(t.card1, cards[i - 3])) {
					dp[i - 1].add(new Triplet(t.card2, t.card3, t.card1));
				}
				if (mayPutOn(t.card1, t.card2)) {
					dp[i - 1].add(new Triplet(t.card1, t.card3, cards[i - 3]));
				}
			}
		}
		for (Triplet t : dp[2]) {
			if (mayPutOn(t.card1, t.card2) && mayPutOn(t.card1, t.card3)) {
				out.println("YES");
				return;
			}
		}
		out.println("NO");
	}

	BufferedReader in;
	PrintWriter out;
	FastScanner sc;

	static Throwable uncaught;

	@Override
	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			sc = new FastScanner(in);
			solve();
		} catch (Throwable t) {
			Main.uncaught = t;
		} finally {
			out.close();
		}
	}

	public static void main(String[] args) throws Throwable {
		Thread t = new Thread(null, new Main(), "", 128 * 1024 * 1024);
		t.start();
		t.join();
		if (uncaught != null) {
			throw uncaught;
		}
	}

}

class FastScanner {

	BufferedReader reader;
	StringTokenizer strTok;

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

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

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

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

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

}


 import java.util.*;

  public class A {
    public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      int n = in.nextInt();
      in.nextLine();
      String s = in.nextLine();
      System.out.println(f(s));
    }

    private static int f(String s) {
      int res = 0;
      int count = 0;
      try {
        String[] arr = s.split(" ");
        for (int i = 0; i < arr.length; i++) {
          for (Character c : arr[i].toCharArray()){
            if (c == Character.toUpperCase(c)){
              count++;
            }
          }
          res = Math.max(res,count);
          count = 0;
        }
      }catch (Exception e){
        char[] chars = s.toCharArray();
        for (Character c : chars){
          if (c == Character.toUpperCase(c)){
            res++;
          }
        }
      }
      return res;
    }

  }