import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

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

    static class div2C_C106Buns {
        int c0;
        int d0;
        int m;
        int doughAvailable;
        int[] cost;
        int[] stuffingAvailable;
        int[] stuffingNeeded;
        int[] doughNeeded;
        int memo;

        public void solve(int testNumber, InputReader in, OutputWriter out) {

            doughAvailable = in.nextInt();
            m = in.nextInt();
            c0 = in.nextInt();
            d0 = in.nextInt();
            cost = new int[m];
            stuffingAvailable = new int[m];
            stuffingNeeded = new int[m];
            doughNeeded = new int[m];

            memo = new int[102][102];

            for (int i = 0; i < memo.length; i++) {
                Arrays.fill(memo[i], -1);
            }

            for (int i = 0; i < m; i++) {
                stuffingAvailable[i] = in.nextInt();
                stuffingNeeded[i] = in.nextInt();
                doughNeeded[i] = in.nextInt();
                cost[i] = in.nextInt();
            }

            int ans = dp(doughAvailable, 0);

            out.print(ans);

        }

        int dp(int doughAvail, int i) {

            if (doughAvail == 0) return 0;

            //already taken sufficient m but still have dough use it with no stuffing
            if (i == m) return (doughAvail / c0) * d0;

            //try all combs of no stuffings
            //try all coming with a stuffing

            if (memo[doughAvail][i] != -1) return memo[doughAvail][i];

            int max = Integer.MIN_VALUE;

            for (int amountNoStuffing = 0; amountNoStuffing * c0 <= doughAvail; amountNoStuffing++) {
                max = Math.max(max, amountNoStuffing * d0 + dp(doughAvail - amountNoStuffing * c0, i + 1));
            }

            //take one of ms
            for (int amountOfStuffingOfM = 0;
                 doughAvail >= amountOfStuffingOfM * doughNeeded[i] && stuffingAvailable[i] >= amountOfStuffingOfM * stuffingNeeded[i];
                 amountOfStuffingOfM++) {

                max = Math.max(max, amountOfStuffingOfM * cost[i] + dp(doughAvail - amountOfStuffingOfM * doughNeeded[i], i + 1));
            }

            return memo[doughAvail][i] = max;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void print(int i) {
            writer.print(i);
        }

    }

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

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

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

        public int 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 isWhitespace(c);
        }

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

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

        }

    }
}


// https://codeforces.com/contest/106/problem/C

import java.util.*;

public class Main {
  public static void main(String[] args) throws {
    // read input
    Scanner sc = new Scanner(System.in);

    int totalDough = sc.nextInt();
    int totalBuns = sc.nextInt();
    int doughPerPlainBun = sc.nextInt();
    int pricePerPlainBun = sc.nextInt();

    int[] totalStuffing = new int[totalBuns];
    int[] stuffingCost = new int[totalBuns];
    int[] doughCost = new int[totalBuns];
    int[] prices = new int[totalBuns];

    for (int i = 0; i < totalBuns; i++) {
      totalStuffing[i] = sc.nextInt();
      stuffingCost[i] = sc.nextInt();
      doughCost[i] = sc.nextInt();
      prices[i] = sc.nextInt();
    }

    // dp[i][j] will store the max amount we can make if we have access to up to i dough and up to the jth type of bun

    int[][] dp = new int[totalDough+1][totalBuns+1];

    // for i = 0, we leave this row as 0s, because we can't make any buns with 0 dough
    // for j = 0, this means we can only make plain buns, so the max amount we can make is determined by the number of plain buns we can make
    for (int i = 1; i <= totalDough; i++) {
      dp[i][0] = pricePerPlainBun * i/doughPerPlainBun;
    }

    // for each remaining cell, we have to try each possible number of the jth bun we can create. for example, if the jth bun requires 3 stuffing and we have 10 of that stuffing, we have to try making 0, 1, 2, and 3 of that bun. for each of those tries, we see how much making money that number of buns yields, plus how much money we can make from the leftover dough (by looking at previous cells)

    for (int i = 2; i <= totalDough; i++) {
      for (int j = 1; j <= totalBuns; j++) {
        int maxBuns = totalStuffing[j-1]/stuffingCost[j-1];

        for (int k = 0; k <= maxBuns; k++) {
          int totalPriceFromSpecialtyBun = k*prices[j-1];
          int doughCostFromSpecialtyBun = k*doughCost[j-1];
          if (doughCostFromSpecialtyBun <= i) {
            int remainingDough = i - doughCostFromSpecialtyBun;
            int totalPrice = totalPriceFromSpecialtyBun + dp[remainingDough][j-1];
            dp[i][j] = Math.max(dp[i][j], totalPrice);
          }
        }
      }
    }

    // for (int[] row : dp) {
    //   for (int num : row) {
    //     System.out.print(num + " ");
    //   }
    //   System.out.println();
    // }

    // write output
    System.out.println(dp[totalDough][totalBuns]);
  }
}


import java.util.Scanner;

//此题就是一个背包，由于这个Each of such buns requires c0
//grams of dough and it can be sold for d0 tugriks
//条件比较简单，所以先处理一下dp[]

public class Main {

    static int n, m, c0, d0, a, b, c, d, ans;
    static int dp[] = new int[1010];

    static void DP() {
        Scanner cin = new Scanner(System.in);
        n = cin.nextInt(); m = cin.nextInt();
        c0 = cin.nextInt(); d0 = cin.nextInt();

        //预处理一下dp[]
        for (int i = c0; i <= n; i += c0)
            dp[i] = dp[i-c0] + d0;

        for (int cas = 0; cas < m; cas++) {
            a = cin.nextInt(); b = cin.nextInt();
            c = cin.nextInt(); d = cin.nextInt();
            a = a / b;
            //做一次背包
            for (int i = n; i >= 0; i--)
                for (int j = 1; j <= a && i >= c * j; j++)
                    dp[i] = Math.max(dp[i], dp[i-c*j] + d * j);
        }

        ans = 0;
        for (int i = 0; i <= n; i++)
            ans = Math.max(ans, dp[i]);
        System.out.println(ans);
    }

    public static void main(String args[]) {
        DP();
    }
}


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

public class Main{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		final int c0, d0;
		int n, m, a, b, c, d, array[] = new int[1005];
		n = scanner.nextInt();
		m = scanner.nextInt();

		c0 =  scanner.nextInt();
		d0 = scanner.nextInt();
		IntStream.rangeClosed(c0, n).forEach(i -> array[i] = array[i-c0] + d0);
		IntStream.range(0, m).forEach(i -> {
		    a = scanner.nextInt();
		    b = scanner.nextInt();
		    c = scanner.nextInt();
		    d = scanner.nextInt();
		    for(int j = 0; j < a / b; j++)
		        for(int k = n; k >= c; k--)
		            array[k] = Math.max(array[k], array[k-c] + d);
		});
		System.out.println(array[n]);
	}
}


import java.util.Scanner;

public class E988 {

private static int min;
//	public static void main(String[] args) {
//		// TODO Auto-generated method stub
//		Scanner sc = new Scanner(System.in);
//		while (sc.hasNext()) {
//			String str = sc.next();
//			StringBuffer s = new StringBuffer(str);
//			str = s.reverse().toString();
//			int min = Integer.MAX_VALUE;
//			int a = str.indexOf("0");
//			if (a != -1) {
//				String v = str.substring(a+1);
//				int a1 = v.indexOf("0");
//				if (a1 != -1) {
//					min = Math.min(min, a+a+a1);
//				}
//				int a2 = v.indexOf("5");
//				if (a2 != -1) {
//					min = Math.min(min, a+a+a2);
//				}
//			}
//			int b = str.indexOf("2");
//			if (b != -1) {
//				int b1 = str.substring(b+1).indexOf("5");
//				if (b1 != -1) {
//					if (b+b1+1 == str.length()-1 && str.charAt(b+b1) == '0') {
//						min = Math.min(min, f(b, b1, str));
//					}else {
//						min = Math.min(min, b+b+b1+1);
//					}
//				}
//			}
//
//			int c = str.indexOf("5");
//			if (c != -1) {
//				String v = str.substring(c+1);
//				int c1 = v.indexOf("0");
//				if (c1 != -1) {
//					min = Math.min(min, c+c+c1+1);
//				}
//				int c2 = v.indexOf("2");
//				if (c2 != -1) {
//					if (c+c2+1 == str.length()-1 && str.charAt(c+c2) == '0') {
//						min = Math.min(min, f(c, c2, str));
//					}else {
//						min = Math.min(min, c+c+c2);
//					}
//				}
//				int c3 = v.indexOf("7");
//				if (c3 != -1) {
//					if (c+c3+1 == str.length()-1 && str.charAt(c+c3) == '0') {
//						min = Math.min(min, f(c, c3, str));
//					}else {
//						min = Math.min(min, c+c+c3);
//					}
//				}
//			}
//			int d = str.indexOf("7");
//			if (d != -1) {
//				int d1 = str.substring(d+1).indexOf("5");
//				if (d1 != -1) {
//					if (d+d1+1 == str.length()-1 && str.charAt(d+d1) == '0') {
//						min = Math.min(min, f(d, d1, str));
//					}else {
//						min = Math.min(min, d+d+d1+1);
//					}
//				}
//			}
//			System.out.println(min == Integer.MAX_VALUE ? -1:min);
//		}
//	}
//
//	private static int f(int b, int b1, String str) {
//		// TODO Auto-generated method stub
//		int index = b1;
//		int k = 1;
//		while (str.charAt(index) == '0') {
//			k ++;
//			index --;
//		}
//		if (k == str.length() - 1) {
//			return Integer.MAX_VALUE;
//		}
//		return b+b+b1+k;
//	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			String str = sc.next();
			char [] arr = new char [str.length()];
			int [] flag = new int [10];
			for (int i = 0; i < arr.length; i++) {
				arr[i] = str.charAt(i);
				flag[arr[i]-'0'] ++;
			}
			min = Integer.MAX_VALUE;
			if (arr[0] > 2 || (arr[0] > 0 && arr[5] > 0) || (arr[5] > 0 && arr[2] > 0) || (arr[7] > 0 && arr[5] > 0)) {
				f(arr, 0, 0);
			}
			System.out.println(min == Integer.MAX_VALUE ? -1:min);
		}
	}
	private static void f(char[] a, int k, int num) {
		// TODO Auto-generated method stub
		if (k == a.length-1) {
			if (a[0] != '0') {
				long b = Long.valueOf(new String(a));
				if (b % 25 == 0) {
					min = Math.min(min, num);
				}
			}
			return ;
		}
		for (int i = k; i < a.length; i++) {
			char tmp = a[i];
			int r = 0;
			for (int j = i-1; j >= k; j--) {
				a[j+1] = a[j];
				r++;
			}
			a[k] = tmp;
			f(a, k+1, num+r);
			tmp = a[k];
			for (int j = k; j <= i-1; j++) {
				a[j] = a[j+1];
			}
			a[i] = tmp;
		}
	}
}


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

    public class bhaa {

        InputStream is;
        PrintWriter o;
        /////////////////// CODED++ BY++ ++ ++ ++ BHAVYA++ ARORA++ ++ ++ ++ FROM++ JAYPEE++ INSTITUTE++ OF++ INFORMATION++ TECHNOLOGY++ ////////////////

    ///////////////////////// Make it work, make it right, make it fast. Make it work, make it right, make it fast. Make it work, make it right, make it fast. Make it work, make it right, make it fast. /////////////////

        void solve() {
            char s[]=ns().toCharArray();
            int n=s.length;
            int min=Integer.MAX_VALUE;
            int f=0;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    if(i==j)
                    {
                        continue;
                    }
                    char t[]=new char[n];
                    for(int k=0;k<n;k++)
                    {
                        t[k]=s[k];
                    }
                    int c=0;
                    for(int k=i;k<n-1;k++)
                    {
                        char te=t[k];
                        t[k]=t[k+1];
                        t[k+1]=te;
                        c++;

                    }
                    if(j>i)
                    {
                        j--;

                    }
                    for(int k=j;k<n-2;k++)
                    {
                        char te=t[k];
                        t[k]=t[k+1];
                        t[k+1]=te;
                        c++;

                    }
                    int ind=0;
                    for(ind=0;ind<n;ind++)
                    {
                        if(t[ind]!='0')
                        {
                            break;
                        }
                    }
                    for(int k=ind;k>0;k--)
                    {

                        char te=t[k];
                        t[k]=t[k-1];
                        t[k-1]=te;
                        c++;

                    }
                    String st=new String(t);
                    long nu=Long.parseLong(st);
                    if(nu%25==0)
                    {
                        f=1;
                        min=Math.min(min,c);
                    }

                }

            }

     if(f==0)
                {
                    o.println(-1);
                }
                else
                {
                    o.println(min);
                }

        }


import com.sun.deploy.util.SystemUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeSet;

public class Main {
    public static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static int[] deg2;
    public static int[] points;

    public static long read() throws IOException {
        tokenizer.nextToken();
        return (long) tokenizer.nval;
    }

    public static void main(String[] args) throws IOException {
        long n=read();
        int l=Long.toString(n).length();
        int[] digits=new int[l];
        String s=Long.toString(n);
        for (int i=0; i<l; i++){
            digits[i]=s.charAt(i)-'0';
        }
        int max0=-1;
        int max2=-1;
        int max5=-1;
        int max7=-1;
        int max02=-1;
        for (int i=0; i<l; i++){
            if (digits[i]==0) max0=i;
            if (digits[i]==2) max2=i;
            if (digits[i]==5) max5=i;
            if (digits[i]==7) max7=i;
        }
        for (int i=0; i<l; i++){
            if (digits[i]==0 && i!=max0) max02=i;
        }
        int result1=1000;
        int result2=1000;
        int result3=1000;
        int result4=1000;
        if (max02!=-1){
            result1=(2*l-3)-max0-max02;
        }
        if (max2!=-1 && max5!=-1){
            if (max5>max2){
                result2=(2*l-3)-max2-max5;
            } else {
                result2=(2*l-3)-max2-max5+1;
            }
        }
        if (max5!=-1 && max0!=-1){
            if (max0>max5){
                result3=(2*l-3)-max0-max5;
            } else {
                result3=(2*l-3)-max0-max5+1;
            }
        }
        if (max5!=-1 && max7!=-1){
            if (max5>max7){
                result4=(2*l-3)-max7-max5;
            } else {
                result4=(2*l-3)-max7-max5+1;
            }
        }
        int result=Math.min(Math.min(result1,result2),Math.min(result3,result4));
        if (result==1000) System.out.println(-1); else System.out.println(result);
    }

}


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

public class TaskE
{
	public static void main(String[] args)
	{
		new TaskE(System.in, System.out);
	}

	static class Solver implements Runnable
	{
		static final int INFINITY = (int) 1e9;
		long n;
		char[] s;
//		BufferedReader in;
		InputReader in;
		PrintWriter out;

		void solve() throws IOException
		{
			n = in.nextLong();
			s = ("" + n).toCharArray();

			if (s.length < 2)
			{
				out.println(-1);

				return;
			}

			int a = find("00".toCharArray());
			int b = find("25".toCharArray());
			int c = find("50".toCharArray());
			int d = find("75".toCharArray());
			int min = CMath.min(a, b, c, d);

			if (min == INFINITY)
				out.println(-1);
			else
				out.println(min);
		}

		int find(char[] dig)
		{
			int len = s.length;
			char[] ss = new char[len];
			int pos = -1;

			System.arraycopy(s, 0, ss, 0, s.length);

			for (int i = len - 1; i >= 0; i--)
			{
				if (ss[i] == dig[1])
				{
					pos = i;

					break;
				}
			}

//			System.out.println("dig : " + Arrays.toString(dig) + ", pos : " + pos);
			if (pos == -1)
				return INFINITY;

			int swaps = 0;
			int ones = pos;
			boolean addExtra = false;

			for (int i = pos; i < len - 1; i++)
			{
				char temp = ss[i];

				ss[i] = ss[i + 1];
				ss[i + 1] = temp;
				swaps++;

				if (ss[0] == '0')
					addExtra = true;
//				System.out.println("\tss : " + Arrays.toString(ss));
			}

//			System.out.println("changed first");

			pos = -1;

			for (int i = len - 2; i >= 0; i--)
			{
				if (ss[i] == dig[0])
				{
					pos = i;

					break;
				}
			}

			if (pos == -1)
				return INFINITY;

			int tens = pos;

			for (int i = pos; i < len - 2; i++)
			{
				char temp = ss[i];

				ss[i] = ss[i + 1];
				ss[i + 1] = temp;
				swaps++;

				if (ss[0] == '0')
					addExtra = true;
//				System.out.println("\tss : " + Arrays.toString(ss));
			}

//			System.out.println("******ss : " + Arrays.toString(ss) + ", swaps : " + swaps);

			if (ss[0] == '0')
			{
				pos = -1;

				for (int i = 0; i < len - 2; i++)
				{
					if (ss[i] != '0')
					{
						pos = i;

						break;
					}
				}

				if (pos == -1)
					return INFINITY;

				for (int i = 0; i < pos - 1; i++)
				{
					char temp = ss[i];

					ss[i] = ss[i + 1];
					ss[i + 1] = temp;
					swaps++;

					if (ss[0] == '0')
						addExtra = true;
				}
			}

			if (addExtra)
			{
				int other = -1;

				for (int i = 0; i < len; i++)
				{
					if (i == ones || i == tens)
						continue;

					if (s[i] != '0')
					{
						other = i;

						break;
					}
				}

				if (other == -1)
					return INFINITY;

				for (int i = 0; i < other - 1; i++)
				{
					char temp = ss[i];

					ss[i] = ss[i + 1];
					ss[i + 1] = temp;
					swaps++;
				}
			}

			if (ss[0] == '0')
				return INFINITY;

			return swaps;
		}

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

//		uncomment below line to change to BufferedReader
//		public Solver(BufferedReader in, PrintWriter out)
		public Solver(InputReader in, PrintWriter out)
		{
			this.in = in;
			this.out = out;
		}

		@Override
		public void run()
		{
			try
			{
				solve();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}

	}

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

		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 & 15;

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

			return res * sgn;
		}

		public int[] nextIntArray(int arraySize)
		{
			int array[] = new int[arraySize];

			for (int i = 0; i < arraySize; i++)
				array[i] = nextInt();

			return array;
		}

		public long nextLong()
		{
			int c = read();

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

			int sign = 1;

			if (c == '-')
			{
				sign = -1;

				c = read();
			}

			long result = 0;

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

				result *= 10;
				result += c & 15;

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

			return result * sign;
		}

		public long[] nextLongArray(int arraySize)
		{
			long array[] = new long[arraySize];

			for (int i = 0; i < arraySize; i++)
				array[i] = nextLong();

			return array;
		}

		public float nextFloat()
		{
			float result, div;
			byte c;

			result = 0;
			div = 1;
			c = (byte) read();

			while (c <= ' ')
				c = (byte) read();

			boolean isNegative = (c == '-');

			if (isNegative)
				c = (byte) read();

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

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

			if (isNegative)
				return -result;

			return result;
		}

		public double nextDouble()
		{
			double ret = 0, div = 1;
			byte c = (byte) read();

			while (c <= ' ')
				c = (byte) read();

			boolean neg = (c == '-');

			if (neg)
				c = (byte) read();

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

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

			if (neg)
				return -ret;

			return ret;
		}

		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 String nextLine()
		{
			int c = read();

			StringBuilder result = new StringBuilder();

			do
			{
				result.appendCodePoint(c);

				c = read();
			} while (!isNewLine(c));

			return result.toString();
		}

		public boolean isNewLine(int c)
		{
			return c == '\n';
		}

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

		public void close()
		{
			try
			{
				stream.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}

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

	}

	static class CMath
	{
		static long power(long number, long power)
		{
			if (number == 1 || number == 0 || power == 0)
				return 1;

			if (power == 1)
				return number;

			if (power % 2 == 0)
				return power(number * number, power / 2);
			else
				return power(number * number, power / 2) * number;
		}

		static long modPower(long number, long power, long mod)
		{
			if (number == 1 || number == 0 || power == 0)
				return 1;

			number = mod(number, mod);

			if (power == 1)
				return number;

			long square = mod(number * number, mod);

			if (power % 2 == 0)
				return modPower(square, power / 2, mod);
			else
				return mod(modPower(square, power / 2, mod) * number, mod);
		}

		static long moduloInverse(long number, long mod)
		{
			return modPower(number, mod - 2, mod);
		}

		static long mod(long number, long mod)
		{
			return number - (number / mod) * mod;
		}

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

		static long min(long... arr)
		{
			long min = arr[0];

			for (int i = 1; i < arr.length; i++)
				min = Math.min(min, arr[i]);

			return min;
		}

		static long max(long... arr)
		{
			long max = arr[0];

			for (int i = 1; i < arr.length; i++)
				max = Math.max(max, arr[i]);

			return max;
		}

		static int min(int... arr)
		{
			int min = arr[0];

			for (int i = 1; i < arr.length; i++)
				min = Math.min(min, arr[i]);

			return min;
		}

		static int max(int... arr)
		{
			int max = arr[0];

			for (int i = 1; i < arr.length; i++)
				max = Math.max(max, arr[i]);

			return max;
		}

	}

	static class Utils
	{
		static boolean nextPermutation(int[] arr)
		{
			for (int a = arr.length - 2; a >= 0; --a)
			{
				if (arr[a] < arr[a + 1])
				{
					for (int b = arr.length - 1; ; --b)
					{
						if (arr[b] > arr[a])
						{
							int t = arr[a];

							arr[a] = arr[b];
							arr[b] = t;

							for (++a, b = arr.length - 1; a < b; ++a, --b)
							{
								t = arr[a];
								arr[a] = arr[b];
								arr[b] = t;
							}

							return true;
						}
					}
				}
			}

			return false;
		}

	}

	public TaskE(InputStream inputStream, OutputStream outputStream)
	{
//		uncomment below line to change to BufferedReader
//		BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Thread thread = new Thread(null, new Solver(in, out), "TaskE", 1 << 29);

		try
		{
			thread.start();
			thread.join();
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
		finally
		{
			in.close();
			out.flush();
			out.close();
		}
	}

}


import java.util.Scanner;
public class Main{
    public static void main(String [] args){
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        String alphabet = "abcdefghijklmnopqrstuvwxyz";

        in aPos = 1;
        char aChar = "a".charAt(0);
        int distance = 0;
        for(int i=0; i < input.length(); i++){
           int pos = alphabet.indexOf(input.charAt(i))+1;

           if(aChar <= input.charAt(i)){
               //d2 = 26 - b + a
               distance = 26 - input.charAt(i) + aChar;
           }
           else{
               distance = 26 - aChar + input.charAt(i) ;
           }
        }
        System.out.println(distance);
    }
}


public class PoblemA {

	public static char[] umdrehen(char[] e){
		char[] ausgabe = new char[e.length];
		int k = 0;
		for(int i = e.length-1 ; i >= 0; i--){
			ausgabe[k] = e[i];
			k++;
		}

		return ausgabe;
	}

	public static char[] renew(char[] e, char a){
		char[] ausgabe = new char[e.length];
		int index_newArray = 0;
		for(int i = 0; i < e.length; i++){
			if(e[i] == a){
				index_newArray = i;
				break;
			}
		}
		for(int i = 0; i< e.length;i++){
			ausgabe[i] = e[index_newArray];
			if(index_newArray == e.length-1){
				index_newArray = 0;
				continue;
			}
			index_newArray++;
		}
		return ausgabe;
	}

	public static void ausgebenarray(char[] e){
		for(int i = 0; i < e.length; i++){
			System.out.print(e[i]);
		}
		System.out.println();
	}

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String eingabe = scan.nextLine();
		char[] wheel = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
		int anz1 = 0, anz2 = 0, totalanz = 0;

		scan.close();
		for(int i = 0; i < eingabe.length(); i++){
			for(int k = 0; k < wheel.length;k++){
				if(wheel[k] == eingabe.charAt(i)){
					k = wheel.length;
				}
				anz1++;
			}
			wheel = umdrehen(wheel);
			for(int k = 0; k < wheel.length;k++){
				if(wheel[k] == eingabe.charAt(i)){
					k = wheel.length;
				}
				anz2++;
			}
			wheel = umdrehen(wheel);

			if(anz1 < anz2){
				totalanz += anz1;
			}else{
				totalanz += anz2;
			}
			anz1 = 0;
			anz2 = 0;
			wheel = renew(wheel, eingabe.charAt(i));
		}

		System.out.print(Integer.toString(totalanz-1));
	}

}


import java.util.Scanner;

public class JavaApplication11 {
    public static int f(int x)
    {
        if (x<0)
        return (-1)*x;
        return x;
    }

    public static void main(String[] args) {
        int max=26;
        Scanner sc=new Scanner(System.in);
        String a=sc.next();
        int b[]=new int[a.length()];
        for (int i=0;i<a.length();i++)
            b[i]=a.charAt(i)-96;
        int cnt=Math.min(b[0]-1,max-b[0]+1);
        for(int i=1;i<a.length();i++)
            cnt+=Math.min(f(b[i]-b[i-1]),max-f(b[i]-b[i-1]));
        System.out.println(cnt);

    }


public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String x = in.nextLine();
x = 'a' + x;
int s = 0;
for(int i = 1; i < x.length(); i++)
{
s += Math.min(Math.abs(x.charAt(i) - x.charAt(i - 1)), 26 - Math.abs(x.charAt(i) - x.charAt(i - 1)));
}

System.out.println(s);

}
}


h;

import java.util.Scanner;

/**
 * Created by dzvinka on 27.09.16.
 */
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        long n = input.nextInt();
        long a = input.nextInt();
        long b =input.nextInt();
        long p = input.nextInt();
        long q = input.nextInt();
        long sum_p = 0;
        long sum_q = 0;
        sum_p = (n / a) * p;
        sum_q = (n / b) * q;
        if (p > q){
            sum_q -= q * (n / (a * b));
        }else{
            sum_p -= p * (n / (a * b));
        }
        System.out.println(sum_q + sum_p);

    }
}


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

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

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

        String s[]=br.readLine().split(" ");
        int n=Integer.parseInt(s[0]);
        int a=Integer.parseInt(s[1]);
        int b=Integer.parseInt(s[2]);
        int p=Integer.parseInt(s[3]);
        int q=Integer.parseInt(s[4]);

        long ans=0;

        ans+=(long)n/a;
        ans+=(long)n/b;

        long lcm=a*b/gcd(a,b);
        ans-=(long)n/lcm;

        out.println(ans);
        out.close();

    }
}


import java.util.Scanner;

public class C {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a = sc.nextInt();
        int b = sc.nextInt();
        int p = sc.nextInt();
        int q = sc.nextInt();

        int lcm = a > b?lcm(a, b):lcm(b:a);
        long common = n / lcm;
        long numAOnly = n / a - common;
        long numBOnly = n / b - common;
        int big = p > q ? p : q;
        long res = common * big + numAOnly * p + numBOnly * q;
        System.out.println(res);

    }

    private int lcm(int big, int small){
        return big * small / gcd(big, small);
    }

    private int gcd(int big, int small){
        return small==0?  big : gcd(small, big%small);
    }

}


public class Main {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		long n = scanner.nextInt();
		long a = scanner.nextInt();
		long b = scanner.nextInt();
		long p = scanner.nextInt();
		long q = scanner.nextInt();

		long bigGet;
		long smallGet;
		long bigGetDivid;
		long smallGetDivid;
		if (p > q){
			bigGet = p;
			bigGetDivid = a;
			smallGet = q;
			smallGetDivid = b;
		}else{
			bigGet = q;
			bigGetDivid = b;
			smallGet = p;
			smallGetDivid = a;
		}

		long result = n/bigGetDivid*bigGet + n/smallGetDivid*smallGet - n/( LCM(Math.max(a, b), Math.min(a, b)))*smallGet;
		System.out.println(result);
	}

	public static long LCM (long a, long b){
		long gcd = GCD(a, b);
		return (a/gcd)*(b/gcd)*gcd;
	}

	public static long GCD (long a, long b){
		if (a % b == 0){
			return b;
		}else{
			return GCD(b, a%b);
		}
	}
}


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

public class Solution
{
 public static void main(String[] args) throws Exception
 {
     BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
     String l=in.readLine();
     String[] str=l.trim().split("\\s+");
     long n=Long.parseLong();
     long k=Long.parseLong();
     long t=Long.parseLong();
     if(t>=1 && t<=k)
     {
         System.out.println(t);
     }
     else if(t>=k+1 && t<=n)
     {
         System.out.println(k);
     }
     else if(t>=n+1 && t<=n+k)
     {
         System.out.println(k-t+n);
     }
 }
}


import java.util.*;

public class ArpaAndAResearchInMexicaWave {
    public static void main(String[] args) {

	long n;
	long k;
	long t;
	n = Long.parseLong(ConsoleInput.readToWhiteSpace(true));
	k = Long.parseLong(ConsoleInput.readToWhiteSpace(true));
	t = Long.parseLong(ConsoleInput.readToWhiteSpace(true));
	if (t < k)
	{
		System.out.print(t);
		System.out.print("\n");
	}
	else if ((k <= t) && (t <= n))
	{
		System.out.print(k);
		System.out.print("\n");
	}
	else
	{
		System.out.print(n - t + k);
		System.out.print("\n");
	}
	return 0;
    }
}


public class Arpa {

	/**
	 * @param args
	 */
	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 t=s.nextInt();
		if(t<=k){
			System.out.println(t);
		}
		else if(t<=n)
			System.out.println(k);
		else if(t<=(n+k)){
			System.out.println(k-t+n);
		}
		else{
			System.out.println("0");
		}
	}

}


import java.util.*;

public class Main {

	static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		int t = sc.nextInt();
		while (t-- > 0) {

			solve();
		}
	}

	public static void solve() {
		int n = sc.nextInt();
		int k = sc.nextInt();
            int t = sc.nextInt();

            if(t<=n)
		System.out.println(Math.min(t, k);
		else
		System.out.println(n-k);

	}

}


public class Main {
    private static final int Max=(int) (1e6+10);
    private static int n;
    private static String s;
    public static void main(String[] args) {
        InitData();
        GetAns();
    }

    private static void InitData() {
        Scanner cin=new Scanner(System.in);
        n=cin.nextInt();
        cin.nextLine();
        s=cin.nextLine();
    };
    private static void GetAns(){
        String []split=s.split(" ");

        int ans=Integer.MIN_VALUE;
        for(int i=0;i<split.length;i++){
            int cnt=0;
            for(int j=0;j<split[i].length();j++){
                if(split[i].charAt(j)>='A'&&split[i].charAt(j)<='Z'){
                    cnt++;
                }
            }
            ans=Math.max(ans, cnt);
//          System.out.println(split[i]);
        }
        System.out.println(ans);
    }

}


import java.util.*;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.io.*;
public class Main {

	static ArrayList<Integer> adj[];
	static char color[];
	static boolean b[],visited[],possible;
	static Map<Integer,HashSet<Integer>> s;
	static int totalnodes,colored;
	static int count[];
	static long sum[];
	public static void main(String[] args) throws IOException {

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

	 int n=in.nextInt();
     String s=in.nextLine();
     String[] st=s.split(" ");
     int max=0;
     for(int i=0;i<st.length;i++)
     {
    	 int c=0;
    	// w.println(st[i]+" ");
    	   for(int j=0;j<st[i].length();j++)
    	   {
    		   if(st[i].charAt(j)>=65 && st[i].charAt(j)<=90)
    			   c++;
    	   }
    	   if(max<c)
    		   max=c;
     }
     w.print(max);

       w.close();
		}
	public static long isCube(long n)
	{
		long x=0,y=1000001;
		while(x<=y)
		{
			long m1=x+(y-x)/3,m2=y-(y-x)/3;
			if(m1*m1*m1==n)
				return m1;
			else if(m2*m2*m2==n)
				return m2;
			else if(m1*m1*m1>n)
				  y=m1-1;
			else if(m2*m2*m2<n)
				  x=m2+1;
			else
			{
				x=m1+1;
				y=m2-1;
			}
		}
		return -1;

	}

	   public static int kadane (int a[])
	    {
	        int n = a.length;
	        int max_so_far = Integer.MIN_VALUE, max_ending_here = 0;
	        for (int i = 0; i < n; i++)
	        {
	            max_ending_here = max_ending_here + a[i];

	            if (max_so_far < max_ending_here)
	                max_so_far = max_ending_here;
	            if (max_ending_here < 0)
	                max_ending_here = 0;
	        }
	        return max_so_far;
	    }
	public static int find_pivot(int[] a,int k)
	{
		int pivot=pivot_element(a,0,a.length-1);
		if(a[pivot]==k)
			 return pivot;
		else if(pivot==-1)
			 return binary_search(a,0,a.length-1,k);
		else if(a[0]<=k)
			 return binary_search(a,0,pivot-1,k);
		else
			return binary_search(a,pivot+1,a.length-1,k);

	}
	public static int pivot_element(int[] a,int l,int r)
	{
	   if(r<l)
		   return -1;
	   if(r==l)
		   return l;
			int mid=(l+r)/2;
			 if (mid < r && a[mid] > a[mid + 1])
			       return mid;
			   if (mid > l && a[mid] < a[mid - 1])
			       return (mid-1);
			   if (a[l] >a[mid])
			       return pivot_element(a, l, mid-1);
			   return pivot_element(a, mid + 1, r);

	}
	public static int binary_search(int[] a,int l,int r,int k)
	{
		if(r<l)
			return -1;
		int mid=(l+r)/2;
		if(a[mid]==k)
			 return mid;
		else if(a[mid]>k)
			 return binary_search(a,l,mid-1,k);
		else
			 return binary_search(a,mid+1,r,k);
	}
	public static String[] subString(String s, int n)
	{
	    String[] st =new String[(n*n+n)/2];
	    for(int i=0;i<st.length;i++)
	    	st[i]="";
	    int l=0;
	    for (int len = 1; len <= n; len++)
	    {
	        // Pick ending point
	        for (int i = 0; i <= n - len; i++)
	        {
	            //  Print characters from current
	            // starting point to current ending
	            // point.
	            int j = i + len - 1;
	            for (int k = i; k <= j; k++)
	                st[l]+=s.charAt(k);
	      //w.println(st[l]);
	            l++;
	            //cout << endl;
	        }
	    }
	    return st;
	}
	  public static long factorial(long n)
	    {
	        long res = 1, i;
	        for (i=2; i<=n; i++)
	            res *= i;
	        return res;
	    }

	public static int factors(int n,int start){
		int count = 0;
		int i;
		for(i=1;i*i<n;i++)
			if(n%i == 0){
				if(i>start)	count++;
				if(n/i > start)	count++;
			}
		if(i*i == n && i>start)	count++;
		return count;
	}

 	public static int isPrime(int n){
	    int i;

	    if (n==2)
	        return 1;

	    if (n%2==0)
	        return 0;

	    for (i=3;i<=Math.sqrt(n);i+=2)
	        if (n%i==0)
	            return 0;

	    return 1;
	}

	static InputReader in;
	static PrintWriter w;
	public static void seive(long n){
		b = new boolean[(int) (n+1)];
	Arrays.fill(b, true);
		for(int i = 2;i*i<=n;i++){
			if(b[i]){
				sum[i]=count[i];
			//	System.out.println(sum[i]+" wf");
				for(int p = 2*i;p<=n;p+=i){
					b[p] = false;
					sum[i]+=count[p];
					//System.out.println(sum[i]);
				}
			}

		}
	}
	static class Pair implements Comparable<Pair> {
		int x;
		int y;
		int c;

		public Pair(){

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

		}
		public int compareTo(Pair other) {

				return 0;
		}

		/*public String toString() {
			return "[u=" + u + ", v=" + v + "]";
		}*/
	}
	static class Node2{
		Node2 left = null;
		Node2 right = null;
		Node2 parent = null;
		int data;
	}

	//binaryStree
	static class BinarySearchTree{
		Node2 root = null;
		int height = 0;
		int max = 0;
		int cnt = 1;
		ArrayList<Integer> parent = new ArrayList<Integer>();
		HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
		public void insert(int x){
			Node2 n = new Node2();
			n.data = x;
			if(root==null){
				root = n;
			}
			else{
				Node2 temp = root,temp2 = null;
				while(temp!=null){
					temp2 = temp;
					if(x>temp.data) temp = temp.right;
					else temp = temp.left;
				}
				if(x>temp2.data) temp2.right = n;
				else temp2.left = n;
				n.parent = temp2;
				parent.add(temp2.data);
			}
		}
		public Node2 getSomething(int x, int y, Node2 n){
			if(n.data==x || n.data==y) return n;
			else if(n.data>x && n.data<y) return n;
			else if(n.data<x && n.data<y) return getSomething(x,y,n.right);
			else return getSomething(x,y,n.left);
		}
		public Node2 search(int x,Node2 n){
			if(x==n.data){
				max = Math.max(max, n.data);
				return n;
			}
			if(x>n.data){
				max = Math.max(max, n.data);
				return search(x,n.right);
			}
			else{
				max = Math.max(max, n.data);
				return search(x,n.left);
			}
		}
		public int getHeight(Node2 n){
			if(n==null) return 0;
			height = 1+ Math.max(getHeight(n.left), getHeight(n.right));
			return height;
		}
	}
public static void debug(Object... o) {
	System.out.println(Arrays.deepToString(o));
}

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

public static long max(long x, long y, long z){
		if(x>=y && x>=z) return x;
		if(y>=x && y>=z) return y;
		return z;
	}
	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 class InputReader {

private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;

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

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

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

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

public int[] nextIntArray(int n) {
	 return nextIntArray(n, 0);
	    }

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

public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
	    }

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

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

public String nextLine() {
  int c = snext();
  while (isSpaceChar(c))
    c = snext();
  StringBuilder res = new StringBuilder();
  do {
    res.appendCodePoint(c);
    c = snext();
  } while (!isEndOfLine(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;
}

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

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

}


import java.util.*;
public class Works {
    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        List<Integer> counts = new ArrayList<>();
        int n = in.nextInt();
        int count = 0;
        int letters = 0;
        while (n >= 0){
            String s = in.next();
            letters=s.length() + 1;
            count = countVolume(s);
            counts.add(count);
            n-=letters;
        }
        if (counts.size() == 1) System.out.println(count);
        else System.out.println(max(counts));
        }

    private static int max(List<Integer> counts) {
        int max = 0;
        for (int i = 0; i < counts.size()-1; i++) {
            if (counts.get(i) > max) max = counts.get(i);
        }
        return max;
    }

    private static int countVolume(String s) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
             if (Character.isUpperCase(s.charAt(i))) count++;

        }
        return count;
    }
}


import java.uti.*;
public class Solution{
     public static void main(String args[])
     {
          Scanner in=new Scanner(System.in);
          int n=in.nextInt();
          String s=in.nextLine();
          String[] m=s.split(" ");
          for(int i=0;i<m.length;i++)
          {
               int m=0;
               int r=0;
               for(int j=0;j<m[i].length();j++)
               {
                    if((int)m[i].charAt(j)>=65 && (int)m[i].charAt(j)<=90 )
                     r++;
               }
               if(r>m)
               m=r;
          }
          System.out.println(m);
     }
}


import java.util.*;
public class Main
{
public static void main(String ar[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
Strinf str=s.nextLine();
int n1=n/2,c=0;
for(int i=0;i<n;i++)
{
if(str.charAt(i)=='S')
{
 if(str.charAt(i+1)=='F')
 {
   c++;
 }
}
}
if(c>n1/2)
System.out.println("YES");
else
System.out.println("NO");
}
}


import java.util.Scanner;

public class offices {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int length = sc.nextInt();
        String waste = sc.nextLine();
        String cities = sc.nextLine();
        int countFS = 0, countSF = 0;
        for (int i = 0; i < length; i++){
            if (charAt(i) == 'S'){
                if (charAt(i+1) == 'F'){
                    countSF++;
                }
            } else {
                if (charAt(i+1) == 'S'){
                    countFS++;
                }
            }
        }
        if (countFS > countSF){
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}


import java.util.Scanner;

public class BetweenOffice{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        String str = scanner.next();

        int seaF = 0;
        int franS = 0;
        for (int i=1; i<s.length(); i++){
            if(s.charAt(i-1) != s.charAt(i)){
                seaF++;
            }else{
                franS++
            }
        }
    }
    if(seaF > franS){
        System.out.println("YES";)
    }else{
        System.out.println("NO");
    }
}


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

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

                    int n = sc.nextInt();
                    String s = sc.next();
                    char[] ch = s.toCharArray();

                    int fs = 0;
                    int sf = 0;

                    for(int i=0;i<s.length()-1;i++)
                    {
                          if(s[i] == 'F' && s[i+1] == 'S')
                          {
                                fs++;
                          }
                          if(s[i] == 'S' && s[i+1] == 'F')
                          {
                                sf++;
                          }
                    }

                    if(sf > fs)
                    {
                          System.out.println("YES");
                    }
                    else
                    {
                          System.out.println("NO");
                    }

              }
        }


public class Codeforces{
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int a=sc.nextInt();
		int b=sc.nextInt();
		int x=sc.nextInt();
		String bin="";
		if(a>=b) {
			if(x%2!=0) {
				for(int i=1;i<=x+1;i++) {
					if(i%2!=0) {
						bin+="0";
						a--;
					}
					else {
						bin+="1";
						b--;
					}
				}
				for(int i=1;i<=b;i++) {
					bin="0"+bin+"1";
					a--;
				}
				for(int i=1;i<=a;i++) bin=bin+"1";
			}
			else {
				for(int i=1;i<=x+1;i++) {
					if(i==x) {
						for(int j=1;j<=b;j++) bin=bin+"1";
						continue;
					}
					if(i%2!=0) {
						bin+="0";
						a--;
					}
					else {
						bin+="1";
						b--;
					}
				}
				for(int i=1;i<=a;i++) bin="0"+bin;
			}
		}
		else {
			if(x%2!=0) {
				for(int i=1;i<=x+1;i++) {
					if(i%2!=0) {
						bin+="1";
						b--;
					}
					else {
						bin+="0";
						a--;
					}
				}
				for(int i=1;i<=a;i++) {
					bin="1"+bin+"0";
					b--;
				}
				for(int i=1;i<=b;i++) bin="1"+bin;
			}
			else {
				for(int i=1;i<=x+1;i++) {
					if(i==x) {
						for(int j=1;j<=a;j++) bin=bin+"0";
						continue;
					}
					if(i%2!=0) {
						bin+="1";
						b--;
					}
					else {
						bin+="0";
						a--;
					}
				}
				for(int i=1;i<=b;i++) bin="1"+bin;
			}
		}
		System.out.println(bin);
	}
}


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

public class Main {

    public static void main(String[] args) {

        Contest494_B w = new Contest494_B();
        w.qwerty();

        Scanner in = new Scanner (System.in);
        String s = in.nextLine();
        String ss[] = s.split(" ");
        int zeros = Integer.valueOf(ss[0]);
        int ones = Integer.valueOf(ss[1]);
        int goal = Integer.valueOf(ss[2]);
        int q = ones;
        ArrayList<Integer> binarString = new ArrayList<Integer>();
        for (int i = 0; i < ones; i++){
            binarString.add(1);
        }
        for (int i = ones; i < zeros + ones; i++){
            binarString.add(0);
        }
//        System.out.println(10 % 2);
        int count = 1;
        if ((zeros == 1) && (count != goal)){
            binarString.add(1, 0);
        }
        else {
            while (count != goal) {
                //            System.out.print(binarString);
                //            System.out.println(" " + ones);
                if (count % 2 != 0) {
                    binarString.remove(q + zeros - 1);
                    binarString.add(0, 0);
                    ones++;
                } else {
                    binarString.remove(ones - 1);
                    binarString.add(0, 1);
                }
                count++;
            }
        }
        for (int i = 0; i < q + zeros; i++) {
            System.out.print(binarString.get(i));
        }
    }
}


import java.io.*;
import java.util.*;
public class BinaryStringConstructing
{
	public static void main(String[] o)
	{
		int a,b,x,i;
		Scanner sc=new Scanner(System.in);
		a=sc.nextInt();
		b=sc.nextInt();
		x=sc.nextInt();
		if(a>b)
		{
			s="01";
			a--;
			b--;
			x--;
			for(i=0;i<x;i++)
			{
				if(i%2==0)
				{
					s+="0";
					a--;
				}
				else
				{
					s+="1";
					b--;
				}
			}
			for(i=0;i<a;i++)
			{
				s=("0"+s);
			}
			for(i=0;i<b;i++)
			{
				s=(s+"1");
			}
		}
		else
		{
			s="10";
			a--;
			b--;
			x--;
			for(i=0;i<x;i++)
			{
				if(i%2==0)
				{
					s+="1";
					b--;
				}
				else
				{
					s+="0";
					a--;
				}
			}
			for(i=0;i<a;i++)
			{
				s=(s+"0");
			}
			for(i=0;i<b;i++)
			{
				s=("1"+s);
			}
		}
		System.out.println(s);
	}
}


package codes;

import FastIO.InputReader;
import FastIO.OutputWriter;

import java.util.ArrayList;

public class BBinaryStringConstructing {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
        int a = in.ni() , b = in.ni() , x = in.ni();
        int[] arr = new int[a+b];
        for(int i = 0;i < b;i++)
            arr[i] = 1;
//        String initial = "10";
        StringBuffer initial = new StringBuffer("10");
        for(int i = 2;i <= x;i++){
            if(i%2 == 0)
                initial.append("1");
            else
                initial.append("0");
        }
        String req = new String(initial);
//        out.println(req);
        int count1 = 0 , count0 = 0;
        ArrayList<Integer> arylst = new ArrayList<Integer>();
        for(int i = 0;i < req.length() ;i++){
            int ele = Integer.parseInt(req.charAt(i)+"");
            arylst.add(ele);
            if(ele == 1)
                count1++;
            else
                count0++;
        }

        int lastele = arylst.get(arylst.size()-1);
        if(lastele == 0){
            for(int i =count0;i < (a );i++)
                arylst.add(0);
            for(int i = count1;i < b;i++){
                arylst.add(0,1);
            }
        }
        else{
            for(int i = count1;i < (b) ;i++)
                arylst.add(1);
            for(int i = count0;i < a;i++)
                arylst.add(1,0);
        }

        for(int i = 0;i < arylst.size() ;i++){
                out.print(arylst.get(i));
        }

    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

import javafx.util.Pair;

public class FirstApp {
 private static MyScanner in;
 private static PrintStream out;
 private static void solve() throws IOException
 {
    int n= in.nextInt();
    int[] a= new int[10];
    int[] b= new int[10];
    int second= n%10;
    n/=10;
    int first = n%10;
    int sum=1;
   for( int i=0 ; i <= 9;++i)
   {
	   a[i] =i;
   }
   b[0]=2;
   b[1]=7;
   b[2]=2;
   b[3]=3;
   b[4]=3;
   b[5]=4;
   b[6]=2;
   b[7]=5;
   b[8]=1;
   b[9]=2;
   for( int i=0 ; i <= 9;++i)
   {
	   if(a[i]==second)
		   sum*=b[i];
	   if(a[i]==first)
		   sum*=b[i];
   }
   System.out.println(sum);
 }

public static void main(String[] args) throws IOException {
	 in = new MyScanner();
	out = System.out;
  solve();
 }

 // =====================================
 static class MyScanner {
  BufferedReader bufReader;
  StringTokenizer strTok;

  public MyScanner() throws IOException
  {
   bufReader = new BufferedReader(new InputStreamReader(System.in));
   strTok = new StringTokenizer("");
  }

  String GetNextToken() throws IOException {
   if (!strTok.hasMoreTokens())
    strTok = new StringTokenizer(bufReader.readLine());
   return strTok.nextToken();
  }

  public int nextInt() throws IOException {
   return Integer.valueOf(GetNextToken());
  }
  public Long nextLong() throws IOException {
	   return Long.valueOf(GetNextToken());
	  }
  public String nextString() throws IOException {
   return GetNextToken();
  }

 }

}


import java.util.*;
public class Test {
    static Scanner input=new Scanner(System.in);
    public static void main(String[]args)

    {
        int x;
        int y=0;

            x = input.nextInt();
        if(x>=0 && x<10)
        {
        switch (x) {
        case 07:
        y=10;
        break;
        case 8;
        y=2;
          break;
        case  01:
        y=12;
        break;

        case 03:
        y=6;
        break;
        default :
        y=4;
        }
        }
        else if(x>=10 & x<20)
        {
            switch(x) {
                case 11:
                y=36;
                break;
                case 13:
                case 14:
                case 15:
                y=18;
                break;
                case 17:
                y=30;
                break;
                case 18:
                y=6;
                break;
                default:
                y=12;
        }
        }
        else if(x>=20 && x<30)
        {
            switch(x)
            {
              case 21:
                  y=12;
                  break;
              case 23:
              case 24:
              case 25:
                  y=6;
                  break;
              case 27:
                  y=10;
                  break;
              default:
                  y=4;
            }
        }
        else if(x>=30 && x<40)
        {
            switch(x)
            {
            case 31:
                y=18;
                break;
            case 33:
            case 34:
            case 35:
                y=9;
                break;
            case 37:
                y=15;
                break;
            case 38:
                y=3;
                break;
            default:
                y=6;
            }
        }

        else if(x>=40 && x<50)
        {
             switch(x)
                {
                case 41:
                    y=18;
                    break;
                case 43:
                case 44:
                case 45:
                    y=9;
                break;
                case 47:
                    y=15;
                    break;
                case 48:
                    y=3;
                    break;
                default:
                    y=6;

                }
        }
        else if (x>=50 && x<60)
        {
            switch(x)
            {
            case 51:
                y=18;
                break;
            case 53:
            case 54:
            case 55:
                y=9;
                break;
            case 57:
                y=15;
                break;
            case 58:
                y=3;
                break;
            default:
                y=6;

            }
        }
        else if (x>=60 && x<70)
        {
            switch(x)
            {
            case 61:
                y=12;
                break;
            case 63:
            case 65:
                y=6;
                break;
            case 67:
                y=10;
                break;
            case 68:
                y=2;
                break;
             default:
                    y=4;

            }

        }
        else if (x>=70 && x<80)
        {
            switch(x)
            {
            case 71:
                y=30;
                break;
            case 73:
            case 74:
            case 75:
                y=15;
                break;
            case 77:
                y=25;
                break;
            case 78:
                y=5;
                break;
            default:
                y=10;
                break;
            }
        }
        else if(x>=80 && x<90)
        {
            {
                switch(x)
                {
                case 80:
                case 86:
                case 89:
                    y=2;
                    break;
                case 81:
                    y=6;
                    break;
                case 82:
                    y=4;
                break;
                case 87:
                    y=5;
                    break;
                case 88:
                    y=1;
                    break;
                default:
                    y=3;

                }
            }

        }
        else if(x>=90 && x<=99)
        {
            switch(x)
            {
              case 91:
                y=12;
                break;
              case 93:
              case 94:
              case 95:
                  y=6;
                  break;
              case 97:
                  y=10;
                  break;
              case 98:
                  y=2;
              break;
              default:
                y=4;

            }

        }
        System.out.println(y);
    }
}


import java.util.Scanner;
public class DigitCounter
{

public static void main(String [] args)

{
  Scanner input = new Scanner(System.in);
  int n;
  int d;
  n = input.nextInt();
  int []options = {2,7,2,3,3,4,2,5,1,2};
  int goodInteger;
  int n1;
  int n2;
  d = n % 10;
  n1 = options[d];
  n = n /10;
  d = n % 10;
  n2 = options[d];
  goodIntegers = n1 * n2;
  System.out.println(goodIntegers);

}
}


import java.util.*;
public class Test {
    static Scanner input=new Scanner(System.in);
    public static void main(String[]args)

    {
        int x;
        int y=0;

            x = input.nextInt();
        if(x>=0 && x<10)
        {
        switch (x) {
        case 07:
        y=10;
        break;
        case 8:
        y=2;
          break;
        case  01:
        y=14;
        break;

        case 03:
        case 04:
        y=6;
        break;
          case 05:
        y=8;
       break;
        default :
        y=4;
        }
        }
        else if(x>=10 & x<20)
        {
            switch(x) {
                case 11:
                y=49;
                break;
                case 13:
                case 14:

                y=21;

                break;
                case 15:
                y=28;
                break;
                case 17:
                y=35;
                break;
                case 18:
                y=7;
                break;
                default:
                y=14;
        }
        }
        else if(x>=20 && x<30)
        {
            switch(x)
            {
              case 21:
                  y=14;
                  break;
              case 23:
              case 24:
                  y=6;
                  break;
              case 25:
              y=8;
              break;
              case 27:
                  y=10;
                  break;
              case 28:
                y=2;
              default:
                  y=4;
            }
        }
        else if(x>=30 && x<40)
        {
            switch(x)
            {
            case 31:
                y=21;
                break;
            case 33:
            case 34:
                y=9;
             break;
            case 35:
              y=12;
                break;

            case 37:
                y=15;
                break

            case 38:
                y=3;
                break;
            default:
                y=6;
            }
        }

        else if(x>=40 && x<50)
        {
             switch(x)
                {
                case 41:
                    y=21;
                    break;
                case 43:
                case 44:
                  y=9;
                break;
                case 45:
                y=12;
                break;
                case 47:
                    y=15;
                    break;
                case 48:
                    y=3;
                    break;
                default:
                    y=6;

                }
        }
        else if (x>=50 && x<60)
        {
            switch(x)
            {
            case 51:
                y=28;
                break;
            case 53:
            case 54:
              y=12;
                 break;
            case 55:
                y=16;
                break;
            case 57:
                y=20;
                break;
            case 58:
                y=4;
                break;
            default:
                y=8;

            }
        }
        else if (x>=60 && x<70)
        {
            switch(x)
            {
            case 61:
                y=14;
                break;
            case 63:
             case 64:
                y=6;
              break;
            case 65:
                y=8;
                break;
            case 67:
                y=10;
                break;
            case 68:
                y=2;
                break;
             default:
                    y=4;

            }

        }
        else if (x>=70 && x<80)
        {
            switch(x)
            {
            case 71:
                y=35;
                break;
            case 73:
            case 74:

            y=15;
            break;
            case 75:
           y=28;
           break;
            case 77:
                y=25;
                break;
            case 78:
                y=5;
                break;
            default:
                y=10;
                break;
            }
        }
        else if(x>=80 && x<90)
        {
            {
                switch(x)
                {
                case 80:
                case 82:
                case 86:
                case 89:
                    y=2;
                    break;
                case 81:
                    y=7;
                    break;
                break;
                case 85:
                y=4;
                break;
                case 87:
                    y=5;
                    break;
                case 88:
                    y=1;
                    break;
                default:
                    y=3;

                }
            }

        }
        else if(x>=90 && x<=99)
        {
            switch(x)
            {
              case 91:
                y=14;
                break;
              case 93:
              case 94:
                  y=6;
                  break;
               case 95:
               y=8;
                break;
              case 97:
                  y=10;
                  break;
              case 98:
                  y=2;
              break;
              default:
                y=4;

            }

        }
        System.out.println(y);
    }
}


import codeforces.b;
import codeforces.c;
import codeforces.d;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;

public class Main {

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

        // BufferedReader r=new BufferedReader(new FileReader("testcase.txt"));
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter p = new PrintWriter(System.out, false);

        StringTokenizer str;

        str = new StringTokenizer(r.readLine());
      BigInteger a=new BigInteger(str.nextToken());
      BigInteger b=new BigInteger(str.nextToken());
      BigInteger c=new BigInteger(str.nextToken());
      BigInteger d=new BigInteger(str.nextToken());

         BigInteger temp=a;
         a=a.multiply(c);
         b=b.multiply(c);
         c=c.multiply(temp);
         d=d.multiply(temp);

         temp=b;
         b=b.multiply(d);
         a=a.multiply(d);
         c=c.multiply(temp);
         d=d.multiply(temp);

      BigInteger num=a.multiply(b).subtract(c.multiply(d));
      BigInteger den=a.multiply(b);
      BigInteger x=gcd(num,den);
      p.println((num.divide(x))+"/"+(den.divide(x)));

       p.flush();
}

   private static BigInteger gcd(BigInteger a, BigInteger b) {
    if(b.compareTo(BigInteger.ZERO)==0)
        return a;
    return gcd(b,a.mod(b));
}
}


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

import practice.bookofevil.Main.EvilTree;

public class Main {

    public static int gcd(int a,int b) {
        return (BigInteger.valueOf(b).gcd(BigInteger.valueOf(a))).intValue();
    }

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

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        int c = in.nextInt();
        int d = in.nextInt();
        if (a*d < c*b) {
            a ^= b;
            b ^= a;
            a ^= b;
            c ^= d;
            d ^= c;
            c ^= d;
        }
        int lcm = lcm(b,d);
        int aNew = a*lcm/b;
        int cNew = c*lcm/d;
        int gcd = gcd(aNew,cNew);
        System.out.println((aNew-cNew)/gcd+"/"+aNew/gcd);
        in.close();
    }

}


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

public class Task337B {

	public static void main(String... args) throws NumberFormatException,
			IOException {
		Solution.main(System.in, System.out);
	}

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

	static class Solution {
		public static void main(InputStream is, OutputStream os)
				throws NumberFormatException, IOException {
			PrintWriter pw = new PrintWriter(os);
			Scanner s = new Scanner(is);
			int a = s.nextInt();
			int b = s.nextInt();
			int c = s.nextInt();
			int d = s.nextInt();

			if (a * d > b * c) {
				int newA = a * d;
				int newB = b * d;

				int newC = c * b;
				int newD = d * b;

				assert newC <= newA;
				assert newB == newD;

				int p = newA - newC;
				int q = newB;

				int nwdpq = gcd(p, q);

				p /= nwdpq;
				q /= nwdpq;

				pw.print(p);
				pw.print("/");
				pw.println(q);
			} else {
				int newA = a * c;
				int newB = b * c;

				int newC = c * a;
				int newD = d * a;

				assert newC == newA;
				assert newB <= newD;

				int p = newD - newB;
				int q = newCs;

				int nwdpq = gcd(p, q);

				p /= nwdpq;
				q /= nwdpq;

				pw.print(p);
				pw.print("/");
				pw.println(q);
			}
			pw.flush();
			s.close();
		}
	}

}


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

/*
 * Routine Problem
 * モニターのスクリーンの縦横比はA:Bである。
 * 映画の映像の縦横比はC:Dである。
 *
 * 両方の縦横比を保ったまま、画面を最大限に活用した映像の表示方法を考える。
 * この時、映像の映らない部分の比を求めよ。
 *
 * 考え方
 * 比の計算。画面の縦もしくは横の比を同値にし、同値ではない部分の差を考える。
 *
 * A : B = C : D　（式としては正しくない）を考え、A*D　と　B*C　を求める。
 * A*D と　B*C　の大小関係を求める。 （大きい方をL,小さい方をSとする）
 * L-S と　L の　最小公倍数を求め、求められた最小公倍数で両方の値を割る。
 *
 * 1:2　と 3:6　など、 A*D = B*C　となるケースに気をつける。
 * このような場合、画面の映像の映らない部分は必ず 0/1　となる。
 * (test4　がこのケースに当てはまる様子。)
 *
 */

public class Main {
    Scanner sc = new Scanner(System.in);
    void doIt(){

        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int d = sc.nextInt();

        int v1 = a * d;
        int v2 = b * c;

        int s=0, s1=0, s2=0;
        if(v1 > v2){
            s1 = v1-v2;
            s2 = v1;
            s = euc(s1,s2);
            System.out.println((s1/s) +"/"+ (s2/s));
        }else if(v1 < v2){
            s1 = v2-v1;
            s2 = v2;
            s = euc(s1,s2);
            System.out.println((s1/s) +"/"+ (s2/s));
        }else{
            System.out.println("0/1");
        }
    }
    public int euc(int m, int n){
        while(n!=0){
            int tmp = m;
            m = n;
            n = tmp%n;
        }
        return m;
    }

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

    // thanks to wata
    class Scanner {
        InputStream in;
        byte[] buf = new byte[1 << 10];
        int p, n;
        boolean[] isSpace = new boolean[128];
        Scanner(InputStream in) {
            this.in = in;
            isSpace[' '] = isSpace['\n'] = isSpace['\r'] = isSpace['\t'] = true;
        }
        int read() {
            if (n == -1) return -1;
            if (p >= n) {
                p = 0;
                try {
                    n = in.read(buf);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                if (n <= 0) return -1;
            }
            return buf[p++];
        }
        boolean hasNext() {
            int c = read();
            while (c >= 0 && isSpace[c]) c = read();
            if (c == -1) return false;
            p--;
            return true;
        }
        String next() {
            if (!hasNext()) throw new InputMismatchException();
            StringBuilder sb = new StringBuilder();
            int c = read();
            while (c >= 0 && !isSpace[c]) {
                sb.append((char)c);
                c = read();
            }
            return sb.toString();
        }
        int nextInt() {
            if (!hasNext()) throw new InputMismatchException();
            int 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 (c >= 0 && !isSpace[c]);
            return res * sgn;
        }
        long nextLong() {
            if (!hasNext()) throw new InputMismatchException();
            int 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 (c >= 0 && !isSpace[c]);
            return res * sgn;
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
    }

}


import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public class Codec {
    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        Queue<TreeNode> q = new LinkedList<>();

        q.add(root);

        while (!q.isEmpty()) {
            for (int i = 0; i < q.size(); i++) {
                TreeNode h = q.poll();
                if (h == null) {
                    sb.append(",");
                } else {
                    sb.append(h.val).append(",");
                    q.add(h.left);
                    q.add(h.right);
                }
            }
        }

        sb.setLength(sb.length() - 1);
        return sb.toString();
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        if (data == null || data.length() == 0)   return null;

        TreeNode root;
        String[] bits = data.split(",");
        Map<Integer, TreeNode> map = new HashMap<>();
        root = new TreeNode(Integer.parseInt(bits[0]));
        map.put(0, root);

        for (int i = 0; i < bits.length; i++) {
            if (bits[i].length() == 0) {
                continue;
            }

            TreeNode node = map.get(i);
            int leftIndex = 2 * i + 1;
            int rightIndex = 2 * i + 2;

            if (leftIndex < bits.length && bits[leftIndex].length() > 0) {
                TreeNode leftNode = new TreeNode(Integer.parseInt(bits[leftIndex]));
                map.put(leftIndex, leftNode);
                node.left = leftNode;
            }

            if (rightIndex < bits.length && bits[rightIndex].length() > 0) {
                TreeNode rightNode = new TreeNode(Integer.parseInt(bits[rightIndex]));
                map.put(rightIndex, rightNode);
                node.right = rightNode;
            }
        }

        return root;
    }

    public static void main(String[] args) {
        Codec c = new Codec();
        TreeNode n = new TreeNode(1);
        n.right = new TreeNode(2);
        n.right.right = new TreeNode(3);
        n.right.right.right = new TreeNode(4);

        String k = c.serialize(n);
        System.out.println(k);

        TreeNode x = c.deserialize(k);

        System.out.println(x.val);
        System.out.println(x.left);
        System.out.println(x.right.val);
        System.out.println(x.right.right.val);
    }
}


import java.io.PrintStream;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

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

        final String contestName = in.nextLine();
        final Set<String> names = new HashSet<>();
        names.add("Danil");
        names.add("Olya");
        names.add("Slava");
        names.add("Ann");
        names.add("Nikita");

        boolean matches = names.stream().map(name -> count(contestName, name)).mapToLong(Long::valueOf).sum() == 1L
        out.println(matches ? "YES" : "NO");
    }

    private static int count(String s, String t) {
        int count = 0;
        for (int idx = s.indexOf(t); idx != -1; idx = s.indexOf(t, idx + 1)) {
            count++;
        }
        return count;
    }
}


public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String cap = s;
        cap+="1";
        cap+="1";
        cap+="1";
        cap+="1";
        cap+="1";
        cap+="1";
        cap+="1";
        int count =0;
        for(int i =0; i<cap.length()-6;i++){
            if(cap.substring(i,i+5).equals("Danil") || cap.substring(i,i+4).equals("Olya")||cap.substring(i,i+3).equals("Ann") || cap.substring(i,i+6).equals("Nikita") || cap.substring(i,i+5).equals("Slava") ){
                count++;

            }
        }
        if(count==1){
            System.out.println("YES");
        }else {
            System.out.println("NO");
        }
    }
}


import java.util.*;
import java.io.*;
public class akram{
    public static void main(String[] args){
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String s=br.readLine();
        boolean[] arr=new boolean[5];
        String[] arr2=new String[5];
        arr[0]=s.contains("Danil");
        arr[1]=s.contains("Olya");
        arr[2]=s.contains("Slava");
        arr[3]=s.contains("Ann");
        arr[4]=s.contains("Nikita");
        arr2[0]="Danil";
        arr2[1]="Olya";
        arr2[2]="Slava";
        arr2[3]="Ann";
        arr2[4]="Nikita";
        int c=0;
        int idx=-1;
        for(int i=0; i<5; i++){
            if(arr[i]){
                c++;
                idx=i;
            }
        }
        if(c==0){
            pw.println("NO");
        }else{
            c=0;
            for(int i=0; i<s.length(); i++){
                if(s.charAt(i)==arr2[idx].charAt(0)){
                    tempi=i;
                    boolean x=true;
                    for(int j=0; j<arr2[idx].length() && x; j++,i++){
                        if(s.charAt(i)!=arr2[idx].charAt(j))
                            x=false;
                    }
                    if(x){
                        c++;
                    }else{
                       i=tempi;
                    }
                }
            }
            pw.println(c==1?"YES":"NO");
        }
    }
}


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

public class Solution{
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String str[] = br.readLine().trim().split("\\s+");
		int arr[] = new int[n];
		int sum = 0;
		for(int i = 0; i < n; i++){
			arr[i] = Integer.parseInt(str[i]);
			sum += arr[i];
		}
		if(sum/2 < arr[0])
			System.out.println(0);
		else{
			int count = 1;
			int sum2 = arr[0];
			ArrayList<Integer> ans = new ArrayList<Integer>();
			ans.add(1);
			int half = (arr[0]+1)/2;
			for(int i = 1; i < n; i++){
				if(arr[i] <= half){
					count++;
					ans.add(i);
					sum2 +=a arr[i];
					if(sum2 > sum/2)
						break;
				}
			}
			System.out.println(count);
			for(int i = 0; i < ans.size(); i++){
				System.out.println(ans.get(i));
			}
		}
	}
}


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

public class Solution{
	public static void main(String[] args) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String str[] = br.readLine().trim().split("\\s+");
		int arr[] = new int[n];
		int sum = 0;
		for(int i = 0; i < n; i++){
			arr[i] = Integer.parseInt(str[i]);
			sum += arr[i];
		}
		if((sum+1)/2 <= arr[i])
			System.out.println(0);
		else{
			int count = 0;
			ArrayList<Integer> ans = new AArrayList<Integer>();
			int half = (arr[0]+1)/2;
			for(int i = 1; i < n; i++){
				if(arr[i] <= half){
					count++;
					ans.add(i);
				}
			}
			System.out.println(count);
			for(int i = 0; i < ans.size(); i++){
				System.out.prinltn(ans.get(i));
			}
		}
	}
}


import java.util.Scanner;

public class Atask {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        StringBuilder ans = new StringBuilder("");
        int arr[] = new int[ n + 1 ];
        int sum = 0;
        for ( int i = 1; i <= n; i++ ) {
            arr[ i ] = sc.nextInt();
            sum += arr[ i ];
        }
       int threshold = sum/2+1;
        //System.out.println(threshold);
        ans.append(1+ " ");
        int toThres = arr[1];
        if(toThres>=threshold) {
            System.out.println(ans.length() / 2);
            System.out.println(ans.toString());
            return;
        }
            for ( int i =2;i<=n;i++){
             //   System.out.println(toThres);
                if(toThres>=threshold) {
                    break;
                }
                if(arr[i]%2==0?arr[i]*2:arr[i]*2-1<=(arr[1]%2==0?arr[1]:arr[1]-1)){
                    toThres+=arr[i];
                    ans.append(i+" ");
                }
        }
        if(toThres>=threshold) {
           String another = ans.toString();
           int num = 0;
           for ( int j = 0; j < another.length();j++ ){
              if(Character.isDigit(another.charAt(j)))
                  num++;
           }
            System.out.println(num);
            System.out.println(ans.toString().substring(0,ans.length()-1));
        }else{
            System.out.println(0);
        }
    }
}


import java.util.*;
import static java.lang.Math.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int  n = in.nextInt(), sum = 0, a[] = new int[n];

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

        int sum1 = a[0];
        ArrayList<Integer> ans = new ArrayList<>();
        for(int i = 1; i<n; i++){
            if(a[0]>=2*a[i]){
                ans.add(i+1);
                sum1+=a[i];
            }
            if(2*sum1>sum) break;
        }

        if(ans.size()==0 || 2*sum1<=sum) System.out.println(0);
        else{
            System.out.println(ans.size()+1);
            System.out.print("1 ");
            for(int x:ans){
                System.out.print(x+" ");
            }
        }
    }
}
            System.out.print("1 ");
            for(int x:ans){
                System.out.print(ans+" ");
            }
        }
    }
}


package gfdgfd;
import java.util.*;
public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int r = sc.nextInt();
		int c = sc.nextInt();
		int n = sc.nextInt();
		int k = sc.nextInt();
		char ch[][] = new char[r][c];
		for(int i=0; i<r; i++) {
			for(int j=0; j<c; j++) {
				ch[i][j] = '*';
			}
		}
		for(int i=0; i<n; i++) {
			int row = sc.nextInt()-1;
			int column = sc.nextInt()-1;
			ch[row][column] = '#';
		}

		int counter=0;
		for(int i=0; i<r; i++) {
			for(int j=0; j<c; j++) {
				for(int ii=i; ii<r; ii++) {
					for(int jj=j; jj<c; jj++) {
						int count=0;
						for(int iii=i; iii<=ii; iii++) {
							for(int jjj=j; jjj<=jj; jjj++) {
								if(ch[iii][jjj]=='#')
									count++;
							}
						}
						if(count>=k)
							counter++;
					}
				}
			}
		}

		System.out.println(counter);

	}
}


package codeforces;

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

public class Orchestra {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int r = sc.nextInt();
        int c = sc.nextInt();
        int n = sc.nextInt();
        int k = sc.nextInt();
        boolean[][] orchestra = new boolean[r][c];
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            orchestra[x-1][y-1] = true;
        }
        int result = 0;
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < r; j++) {
                for (int a = 0; a < c; a++) {
                    for (int b = 0; b < c; b++) {

                        int sum = 0;
                        for (int x = i; x <= j; x++) {
                            for (int y = a; y <= b; y++) {
                                if (orchestra[x][y]) sum++;
                            }
                        }
                        if (sum >= k) {
                            result++;
//                            if (i == a && j == b) {
//                                result--;
//                                out.println("the same");
//                            }
                        }
//                        out.println((i+1) + " " + (j+1) + " " + (a+1) + " " + (b+1) + ": " + sum);
                    }
                }
            }

        }
        out.println(result);
        out.close();
    }
}


package gfdgfd;
import java.util.*;
public class main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int r = sc.nextInt();
		int c = sc.nextInt();
		int n = sc.nextInt();
		int k = sc.nextInt();
		char ch[][] = new char[r][c];
		for(int i=0; i<r; i++) {
			for(int j=0; j<c; j++) {
				ch[i][j] = '*';
			}
		}
		for(int i=0; i<n; i++) {
			int row = sc.nextInt()-1;
			int column = sc.nextInt()-1;
			ch[row][column] = '#';
		}

		int counter=0;
		for(int i=0; i<r; i++) {
			for(int j=0; j<c; j++) {
				for(int ii=i; ii<r; ii++) {
					for(int jj=j; jj<c; jj++) {
						int count=0;
						for(int iii=i; iii<=ii; iii++) {
							for(int jjj=j; jjj<=jj; jjj++) {
								if(ch[iii][jjj]=='#')
									count++;
							}
						}
						if(count>=k)
							counter++;
					}
				}
			}
		}

		System.out.println(counter);

	}
}


import java.util.*;

public class A {
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int r=scan.nextInt();
		int c=scan.nextInt();
		int n=scan.nextInt();
		int q=scan.nextInt();
		boolean[][] check=new boolean[11][11];
		int[][] a=new int[11][11];
		for (int i=0;i<n ;i++ ) {
			check[scan.nextInt()][scan.nextInt()]=true;
		}
		for (int i=1;i<=r;i++ )
			for (int j=1;j<=c ;j++ ) {
			 	a[i][j]=a[i-1][j]+a[i][j-1]-a[i-1][j-1];
			 	if (check[i][j])
			 		a[i][j]++;
			 }

		int res=0;
		for (int i=1;i<=r;i++ )
			for (int j=1;j<=c;j++ )
				for (int k=i;k<=r;k++ )
					for (int l=j;l<=c;l++ )
					{
						//System.out.println("From "+i+" "+j+" to "+k+" "+l+" ="+
							(a[k][l]+a[i-1][j-1]-a[k][j-1]-a[i-1][l]));
						if (a[k][l]+a[i-1][j-1]-a[k][j-1]-a[i-1][l]>=q)
							res++;
					}
		System.out.println(res);

	}
}


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

public class ASwimmingPool {

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

        PrintWriter out = new PrintWriter(System.out, true);

        try {
            int n_vert = Integer.parseInt(br.readLine());
        }
        catch (IOException e) {

        }

/*        int[][] verts = new int[n_vert][2];

        for (int i=0; i <n_vert, i++) {
            verts[i][0] = sc.nextint();
            verts[i][1] = sc.nextint();
        }

        for (int[] v: verts) {
            system.out.println(v.tostring());
        } */

        System.out.println(n_vert);
    }
}


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

public class ASwimmingPool {

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

        PrintWriter out = new PrintWriter(System.out, true);

        int n_vert=0;

        try {
           n_vert = Integer.parseInt(br.readLine());
        }
        catch (IOException e) {
            System.err.println(e);

        }

        int[][] verts = new int[n_vert][2];

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

            try {
                StringTokenizer st;
                st = new StringTokenizer(br.readLine());
                verts[i][0] = Integer.parseInt(st.next);
            }
            catch (IOException e) {
                System.err.println(e);
            }

        }

        if (n_verts <= 1) {
            System.out.println("-1");
        }

        for (int[] v: verts) {
            System.out.println(v.tostring());
        }

        System.out.println(n_vert);
    }
}


//

import java.util.Scanner;

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

        int area;
        int lado1;
        int lado2;
        int numero = sc.nextInt();

        int[] coordenadasX = new int[numero];
        int[] coordenadasY = new int[numero];

        for(int i=0; i<=numero-1; i++)
        {
        	coordenadasX[i] = sc.nextInt();
        	coordenadasY[i] = sc.nextInt();
        }

        if(numero == 1)
        {
            System.out.println("-1");
        }
        if(numero!=1)
        {
        	if(numero==2)
        	{
        		if(coordenadasX[0]==coordenadasX[1] || coordenadasY[0]==coordenadasY[1])System.out.println("-1");
        		else
        		{
        			area = Math.abs((coordenadasX[0]-coordenadasX[1])*(coordenadasY[0]-coordenadasY[1]));
                                System.out.println(area);
        		}
        	}
        	if(numero==3)
        	{
        		if(coordenadasX[0]!=coordenadasX[1])
        		{
        			lado1= coordenadasX[1]-coordenadasX[0];
        			lado2= coordenadasY[2]-coordenadasY[0];
        		}
        		else
        		{
        			lado2 = coordenadasY[1]-coordenadasY[0];
        			lado1 = coordenadasX[2]-coordenadasX[1];
        		}
        		area = Math.abs(lado1*lado2);
                System.out.println(area);
        	}
            if(numero==4)
            {
                if(coordenadasX[0]!=coordenadasX[1])
                {
                    lado1= coordenadasX[1]-coordenadasX[0];
                    if(coordenadasY[2]!=coordenadasY[0])
                    lado2= coordenadasY[2]-coordenadasY[0];
                    else lado2= coordenadasY[2]-coordenadasY[1];
                }
                else
                {
                    lado2 = coordenadasY[1]-coordenadasY[0];
                    if(coordenadasY[2]!=coordenadasY[0])
                    lado2= coordenadasY[2]-coordenadasY[0];
                    else lado2= coordenadasY[2]-coordenadasY[1];

                }
                area = Math.abs(lado1*lado2);
                System.out.println(area);
            }
        }

    }

}


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

public class ASwimmingPool {

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

        PrintWriter out = new PrintWriter(System.out, true);

        int n_vert=0;

        try {
           n_vert = Integer.parseInt(br.readLine());
        }
        catch (IOException e) {
            System.err.println(e);

        }

        int[][] verts = new int[n_vert][2];

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

            try {
                StringTokenizer st;
                st = new StringTokenizer(br.readLine());
                verts[i][0] = Integer.parseInt(st.nextToken());
            }
            catch (IOException e) {
                System.err.println(e);
            }

        }

        if (n_verts <= 1) {
            System.out.println("-1");
        }

        for (int[] v: verts) {
            System.out.println(v.tostring());
        }

        System.out.println(n_vert);
    }
}


import java.io.*;
public class A
{
public static void main(String[] args)
{
      int day = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
String s = br.readLine();
int aS = Integer.parseInt(s.substring(0,2));
int bS = Integer.parseInt(s.substring(s.length() - 2));
String s1 = br.readLine();
int a = Integer.parseInt(s1.substring(0,1));
int b = Integer.parseInt(s1.substring(s1.length() - 1));
int tGusSt = 10;
int tGusEnd = 22;
int tBoyEnd = 14;
int tCount = 10;
boolean isUp = true;
do
{
 tCount++;
 if(tCount == tBoyEnd)
 {
       isUp  = false;
       day++;
       aS += a;
       if(tCount != tGusEnd)
       {
             aS += a;
             aS -= b;
       }
 }
 else
 {
       isUp = true;
 }
 if(isUp == true)
 {
       if(tCount != tBoyEnd)
       {
             aS += b;
       }
 }
 else
 {
       day = -1;
       aS == bS;
 }
}while(aS != bS);
}
catch(Exception excp)
{

}
finally
{
      System.out.println(day);
}
}
}


package com.company;

import java.util.Scanner;

public class CF652A {
    public void work() {
        Scanner sc = new Scanner(System.in);
        int h1, h2, a, b;
        h1 = sc.nextInt();
        h2 = sc.nextInt();
        a = sc.nextInt();
        b = sc.nextInt();

        int hour = 14, x = h1, day = 0;

        while (x < h2) {
            if (hour > 22 && a <= b) {
                System.out.println(-1);
                return;
            }
            if (hour >= 10 && hour < 22) x+=a;
            else x-=b;
            hour ++;
            if (hour == 24) {
                hour = 0; day++;
            }
        }
        System.out.println(day);
    }

}

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


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

public class Main {

	BufferedReader in;
	StringTokenizer str = null;
	PrintWriter out;

	private String next() throws Exception{
		while (str == null || !str.hasMoreElements())
			str = new StringTokenizer(in.readLine());
		return str.nextToken();
	}

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

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

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

	public void run() throws Exception{
		in =  new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		int h1 = nextInt(), h2 = nextInt();
		int a = nextInt(), b = nextInt();

		int progress = 12 * (a - b);
		if (progress <= 0) {
			out.println(-1);
			out.close();
			return;
		}
		for(int day = 0;;++day){
			if (1L * progress * day + a * 8 >= h2) {
				out.prinltn(day);
				out.close();
				return;
			}
		}
	}

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


import java.util.Scanner;
public class A
{
	public static void main(String args[])
	{
		int h1 = s.nextInt();
		int h2 = s.nextInt();
		int a = s.nextInt();
		int b = s.nextInt();

		if(b>=a)
		{
			if((h2-h1)<=8*a)
				System.out.println(0);
			else
				System.out.println(-1);
		}
		else
		{
			int days = 0;
			h1 = h1+8*a;
			while(h1<h2)
			{
				h1 += 12*(a-b);
				days++;
			}
			System.out.println(days);
		}

	}

}


import java.util.*;
public class Main
{
	public static void main(String[] args) {
	    Scanner s = new Scanner(System.in);
	    int test = s.nextInt();
	    while(t > 0){
	        System.out.println(PlayOff(s.nextInt()));
	        t--;
	    }

	}

	public static int PlayOff(int n){
	    List<Integer> res = new ArrayList<>();
	    int player = (int)Math.pow(2, n);
	    for(int i = 1; i <= player; i++) res.add(i);

	    while(res.size() > 1){
	        List<Integer> next = new ArrayList<>();
	        for(int i = 0; i < res.size(); i+=2){
	            int a = res.get(i);
	            int b = res.get(i+1);
	            if((a+b) % 2 == 0) next.add(b);
	            else next.add(a);
	        }
	        res = next;
	    }
	    return res.get(0);

	}
}


import java.util.Scanner;
import java.io.*;
public class Playoff{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int t;
       t= sc.nextInt();
       while(t--)
       { int n;
        n=sc.nextInt();
           if(n==1)
           {
               System.out.println(1);
           }
           else
           {
               System.out.println((1<<n)-1);
           }
       }
    }
}


// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
public class Main {
    public static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        int t = sc.nextInt();  // number of test cases

        // for each test case
        for (int i = 0; i < t; i++) {
            int n = sc.nextInt();
            int winner = Math.pow(2, n) - 1;
            System.out.println(winner);
        }
    }
}


import java.util.*;
public class Tournament {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scn = new Scanner(System.in);
        int T = scn.nextInt();
        while (T-- > 0)
        {
        	int N = scn.nextInt();
        	System.out.println( (int)Math.pow(2, N) - 1);

        }
	}