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

public class Main {
    public void solve() {
        long n = nl();
        int k = ni();
        HashMap<Long, Integer> divs = new HashMap<>();
        int sqr = (int) Math.ceil(Math.sqrt(n));
        for (int i = 2; i <= sqr; i++) {
            int c = 0;
            while (n % i == 0) {
                c++;
                n /= i;
            }
            if (c > 0) {
                divs.put((long)i, c);
            }
        }
        int mod = 1000000007;
        if ((n != 1)/* && (n % mod != 0)*/) {
            divs.put(n % mod, 1);
        }
        long ans = 1;
//        double ans2 = 1;
        int[] inv = new int[100];
        for (int i = 1; i < 100; i++) {
            inv[i] = (int) pow(i, mod - 2, mod);
        }
        for (Map.Entry<Long, Integer> d: divs.entrySet()) {
           int c = d.getValue() + 1;
           long[][] dp = new long[k][c];
//           double[][] dp2 = new double[k][c];
           for (int i = 0; i < c; i++) {
               dp[0][i] = inv[c];
//               dp2[0][i] = 1.0 / c;
           }
           for (int i = 1; i < k; i++) {
               for (int j = 0; j < c; j++) {
                   for (int x = c - 1; x >= j; x--) {
                       dp[i][j] += dp[i - 1][x] * inv[x + 1] % mod;
//                       dp2[i][j] += dp2[i - 1][x] / (x + 1);
                   }
               }
           }
           long m = 0;
//           double m2 = 0;
           int mul = 1;
           long v = d.getKey();
           for (int i = 0; i < c; i++) {
               m += mul * dp[k - 1][i] % mod;
               mul = (int) ((mul * v) % mod);
//               m2 += pow(d.getKey(), i, Long.MAX_VALUE) * dp2[k - 1][i];
           }
           ans = m * ans % mod;
//           ans2 *= m2;
        }
        write(ans + "\n");
//        write(ans2 + "\n");
    }

    long pow(long n, int x, long m) {
        long ans = 1;
        long a = n;
        int e = 1;
        while (x >= e) {
            if ((x & e) > 0) {
                ans = (ans * a) % m;
            }
            a = (a * a) % m;
            e = e << 1;
        }
        return ans;
    }

    public static void main(String[] args) {
        Main m = new Main();
        m.solve();
        try {
            m.out.close();
        } catch (IOException e) {}
    }

    BufferedReader in;
    BufferedWriter out;
    StringTokenizer tokenizer;
    public Main() {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new BufferedWriter(new OutputStreamWriter(System.out));
    }
    public String n() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(in.readLine());
            } catch (IOException e) {}
        }
        return tokenizer.nextToken();
    }
    public int ni() {
        return Integer.parseInt(n());
    }
    public long nl() {
        return Long.parseLong(n());
    }
    public void write(String s) {
        try {
            out.write(s);
        } catch (IOException e) {}
    }
}


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

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        ConsoleIO io = new ConsoleIO(new InputStreamReader(System.in), new PrintWriter(System.out));

//        String fileName = "C-large-practice";
//        ConsoleIO io = new ConsoleIO(new FileReader("D:\\Dropbox\\code\\practice\\jb\\src\\" + fileName + ".in"), new PrintWriter(new File("D:\\Dropbox\\code\\practice\\jb\\src\\" + fileName + ".out")));

        new Main(io).solve();
//        new Main(io).solveLocal();

        io.close();
    }

    ConsoleIO io;

    Main(ConsoleIO io) {
        this.io = io;
    }

    ConsoleIO opt;

    Main(ConsoleIO io, ConsoleIO opt) {
        this.io = io;
        this.opt = opt;
    }

    List<List<Integer>> gr = new ArrayList<>();

    long MOD = 1_000_000_007;

    public void solve() {
        long n = io.readLong();
        int k = io.ri();

        List<Long> divs = new ArrayList<>();
        for(long d = 1; d*d<= n; d++) {
            if (n % d == 0) {
                divs.add(d);
                if (d * d != n) {
                    divs.add(n / d);
                }
            }
        }

        divs.sort(Comparator.naturalOrder());

        int[][] subs = new int[divs.size()][];
        long[] numd = new long[divs.size()];
        for(int i = 0; i < divs.size(); i++){
            List<Integer> list = new ArrayList<>();
            long v = divs.get(i);
            for(int j = i; j < divs.size(); j++){
                if(divs.get(j) % v == 0){
                    list.add(j);
                    numd[j]++;
                }
            }
            subs[i] = new int[list.size()];
            for(int j = 0; j < list.size(); j++){
                subs[i][j] = list.get(j);
            }
        }

        for(int i = 0; i < divs.size(); i++){
            numd[i] = pow(numd[i], MOD - 2, MOD);
        }

        long[] count = new long[divs.size()];
        count[count.length - 1] = 1L;
        for(int step = 0; step < k; step++){
            for(int i = 0; i < count.length; i++){
                count[i] = (count[i] * numd[i]) % MOD;
            }
            for(int i = 0; i < count.length; i++){
                int[] list = subs[i];

                long next = 0;
                for(int j : list){
                    next = next + count[j];
                }
                count[i] = next % MOD;
            }
        }

        long res = 0;
        for(int i = 0; i < count.length; i++) {
            res = (res + divs.get(i) * count[i] % MOD) % MOD;
        }

        io.writeLine(res+"");
    }

    long div(long a, long b, long mod) {
        return (a * pow(b, mod - 2, mod)) % mod;
    }

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

}

class ConsoleIO {

    BufferedReader br;
    PrintWriter out;
    public ConsoleIO(Reader reader, PrintWriter writer){br = new BufferedReader(reader);out = writer;}
    public void flush(){this.out.flush();}
    public void close(){this.out.close();}
    public void writeLine(String s) {this.out.println(s);}
    public void writeInt(int a) {this.out.print(a);this.out.print(' ');}
    public void writeWord(String s){
        this.out.print(s);
    }
    public void writeIntArray(int[] a, int k, String separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < k; i++) {
            if (i > 0) sb.append(separator);
            sb.append(a[i]);
        }
        this.writeLine(sb.toString());
    }
    public void writeLongArray(long[] a, int k, String separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < k; i++) {
            if (i > 0) sb.append(separator);
            sb.append(a[i]);
        }
        this.writeLine(sb.toString());
    }
    public int read(char[] buf, int len){try {return br.read(buf,0,len);}catch (Exception ex){ return -1; }}
    public String readLine() {try {return br.readLine();}catch (Exception ex){ return "";}}
    public long[] readLongArray() {
        String[]n=this.readLine().trim().split("\\s+");long[]r=new long[n.length];
        for(int i=0;i<n.length;i++)r[i]=Long.parseLong(n[i]);
        return r;
    }
    public int[] readIntArray() {
        String[]n=this.readLine().trim().split("\\s+");int[]r=new int[n.length];
        for(int i=0;i<n.length;i++)r[i]=Integer.parseInt(n[i]);
        return r;
    }
    public int[] readIntArray(int n) {
        int[] res = new int[n];
        char[] all = this.readLine().toCharArray();
        int cur = 0;boolean have = false;
        int k = 0;
        boolean neg = false;
        for(int i = 0;i<all.length;i++){
            if(all[i]>='0' && all[i]<='9'){
                cur = cur*10+all[i]-'0';
                have = true;
            }else if(all[i]=='-') {
                neg = true;
            }
            else if(have){
                res[k++] = neg?-cur:cur;
                cur = 0;
                have = false;
                neg = false;
            }
        }
        if(have)res[k++] = neg?-cur:cur;
        return res;
    }
    public int ri() {
        try {
            int r = 0;
            boolean start = false;
            boolean neg = false;
            while (true) {
                int c = br.read();
                if (c >= '0' && c <= '9') {
                    r = r * 10 + c - '0';
                    start = true;
                } else if (!start && c == '-') {
                    start = true;
                    neg = true;
                } else if (start || c == -1) return neg ? -r : r;
            }
        } catch (Exception ex) {
            return -1;
        }
    }
    public long readLong() {
        try {
            long r = 0;
            boolean start = false;
            boolean neg = false;
            while (true) {
                int c = br.read();
                if (c >= '0' && c <= '9') {
                    r = r * 10 + c - '0';
                    start = true;
                } else if (!start && c == '-') {
                    start = true;
                    neg = true;
                } else if (start || c == -1) return neg ? -r : r;
            }
        } catch (Exception ex) {
            return -1;
        }
    }
    public String readWord() {
        try {
            boolean start = false;
            StringBuilder sb = new StringBuilder();
            while (true) {
                int c = br.read();
                if (c!= ' ' && c!= '\r' && c!='\n' && c!='\t') {
                    sb.append((char)c);
                    start = true;
                } else if (start || c == -1) return sb.toString();
            }
        } catch (Exception ex) {
            return "";
        }
    }
    public char readSymbol() {
        try {
            while (true) {
                int c = br.read();
                if (c != ' ' && c != '\r' && c != '\n' && c != '\t') {
                    return (char) c;
                }
            }
        } catch (Exception ex) {
            return 0;
        }
    }
//public char readChar(){try {return (char)br.read();}catch (Exception ex){ return 0; }}
}
class Pair {
    public Pair(int a, int b) {this.a = a;this.b = b;}
    public int a;
    public int b;
}
class PairLL {
    public PairLL(long a, long b) {this.a = a;this.b = b;}
    public long a;
    public long b;
}
class Triple {
    public Triple(int a, int b, int c) {this.a = a;this.b = b;this.c = c;}
    public int a;
    public int b;
    public int c;
}


import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class A {

	BufferedReader in;
	StringTokenizer st;
	PrintWriter ob;

	HashMap<Long , HashMap< Integer , HashMap<Integer  , Long> > > hash = new HashMap<>();
	long mod = (long)1e9+7;

	public static void main( String args[] ) throws IOException {
		new A().run();
	}
	void run() throws IOException {
		in = new BufferedReader ( new InputStreamReader( System.in ) );
		ob = new PrintWriter( System.out );
		solve();
		ob.flush();
	}
	void solve() throws IOException {
		long n = nl();
		int k = ni();

		long result = 1;
		for(long prime = 2 ; prime*prime<=n ; prime++) {

			int power = 0 ;
			while( n%prime == 0 ) {
				power++;
				n/=prime;
			}

			if( power > 0  ) {
				result = result * solvePrime( (long)prime , power , k  );
				result = result % mod;
			}
		}

		if( n>1 ) {
			result *= solvePrime( n , 1 , k  );
			result %= mod;
		}

		ob.println( result );
	}

	long solvePrime( long prime , int power , int k )
	{
		long precomputed = get( prime , power , k );
		if( precomputed != Long.MIN_VALUE ) {
			return precomputed;
		}

		if( k==1 ) {
			long numerator = powMod( prime , power + 1 )-1+mod;
			long denominator = prime - 1;
			numerator *= inverse(denominator);
			numerator %= mod;

			long res = (( numerator *1L* inverse( power+1 ) )%mod);
			add( prime , power , k , res );
			return res;
		}

		long expectation = 1 ;
		for( int i=1 ; i<=power ; i++ ) {
			expectation +=  solvePrime( prime , i , k-1 );
			expectation %= mod;
		}

		long res = ((expectation *1L* inverse( power+1 )) % mod);
		add( prime , power , k , res );
		return res;
	}

	long inverse( long n ) {
		return (new BigInteger(""+n).modInverse( new BigInteger(""+mod))).longValue();
	}

	long inverse( int n ) {
		return (new BigInteger(""+n).modInverse( new BigInteger(""+mod))).longValue();
	}

	long powMod(long prime,int power) {
	    long x=1;
	    long y=prime;
	    while(power > 0){
	        if(power%2 == 1){
	            x=(x*y)%mod;
	        }
	        y = (y*y)%mod;
	        power /= 2;
	    }
	    return x%mod;
	}

	void add( long prime , int power , int k , long ans ) {

		if( !hash.containsKey( prime ) ) {
			HashMap< Integer , Long > first = new HashMap<>();
			first.put( k , ans );
			HashMap< Integer , HashMap< Integer , Long > > second = new HashMap<>();
			second.put( power , first);
			hash.put( prime , second );
		} else {
			HashMap< Integer , HashMap< Integer , Long > > second = hash.get(prime);

			if( !second.containsKey( power ) ) {
				HashMap<Integer , Long>  first  = new HashMap<>();
				first.put( k ,ans );
				hash.get(prime).put(power , first );
			} else {
				if( !hash.get(prime).get(power).containsKey(k)  )
					hash.get(prime).get(power).put(k , ans );
			}
		}
	}

	long get( long prime , int power , int k ) {
		if( hash.containsKey(prime) ) {
			if( hash.get(prime).containsKey(power)) {
				if( hash.get(prime).get(power).containsKey(k) )
					return hash.get(prime).get(power).get(k);
			}
		}
		return Long.MIN_VALUE;
	}

	long nl() throws IOException {
		return Long.parseLong( nextToken() );
	}
	int ni() throws IOException {
		return Integer.parseInt( nextToken() );
	}
	String nextToken() throws IOException {
		if( st == null || !st.hasMoreTokens() )
			st = new StringTokenizer( in.readLine() );
		return st.nextToken();
	}
}


import java.util.*;

public class d
{
   public static long mod = 1000000007;
   public static void main(String[] Args)
   {
      Scanner sc = new Scanner(System.in);
      long x = sc.nextLong();
      int iter = sc.nextInt();
      ArrayList<Long> div = getDiv(x);
      HashMap<Long, Integer> getIndex = new HashMap<Long, Integer>();

      HashSet<Integer>[] nextIndices = new HashSet[div.size()];

      for (int i = 0; i < div.size(); i++)
      {
         getIndex.put(div.get(i), i);
         nextIndices[i] = new HashSet<Integer>();
      }

      for (int i = 0; i < div.size(); i++)
      {
         long x2 = div.get(i);
         ArrayList<Long> div2 = getDiv(x2);
         for (Long val : div2)
         {
            nextIndices[i].add(getIndex.get(val));
         }
      }

      int n = div.size();

      long[] ptab = new long[div.size()];
      long[] newtab = new long[div.size()];
      long[] invs = new long[div.size()];
      for (int i = 0; i < n; i++)
         invs[i] = modInv(nextIndices[i].size(), mod);
      ptab[getIndex.get(x)] = 1;
      Arrays.fill(qs, 1);

      for (int i = 0; i < iter; i++)
      {
         Arrays.fill(newtab, 0);

         for (int j = 0; j < n; j++)
         {
            for (Integer k : nextIndices[j])
            {
               // Add [(j old) * (inv of j)]  to (k new)
               long f1 = ptab[j] * invs[j];
               long f2 = newtab[k];
               newtab[k] = (f1 + f2) % mod;
            }
         }

         long[] tmp = newtab;
         newtab = ptab;
         ptab = tmp;
      }

      long ans = 0;
      for (int i = 0; i < div.size(); i++)
      {
         ans += (div.get(i) % mod) * ptab[i];
         ans %= mod;
      }
      System.out.println(ans);
   }

   public static ArrayList<Long> getDiv(long x){
      ArrayList<Long> ret = new ArrayList<Long>();
      for (long y = 1; y * y <= x; y++)
      {
         if (x % y == 0)
         {
            ret.add(y);
            if (y != x / y)
               ret.add(x / y);
         }
      }

      return ret;
   }

   public static long modInv(long x, long mod)
   {
      return modExpo(x, mod - 2, mod);
   }

   public static long modExpo(long base, long exp, long mod)
   {
      if (exp == 0)
         return 1 % mod;
      if (exp == 1)
         return base % mod;

      long tmp = modExpo(base, exp / 2, mod);
      tmp = tmp * tmp;
      tmp %= mod;
      if (exp % 2 == 1){
         tmp *= base;
         tmp %= mod;
      }
      return tmp;
   }
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.BrokenBarrierException;

/**
 * Works good for CF
 *
 * @author cykeltillsalu
 */
public class C102_div2 {

	int[] ps;
	{
		boolean[] primes = new boolean[(int) Math.sqrt(1000010000L)];
		Arrays.fill(primes, true);
		primes[0] = primes[1] = false;
		int psc = 0;
		for (int i = 2; i < primes.length; ++i) {
			if (primes[i]) {
				psc++;
				for (int j = 2 * i; j < primes.length; j += i) {
					primes[j] = false;
				}
			}
		}
		ps = new int[psc];
		for (int i = 0, j = 0; i < primes.length; ++i) {
			if (primes[i]) {
				ps[j++] = i;
			}
		}
	}

	// some local config
	static boolean test = false;
	static String testDataFile = "testdata.txt";
	static String feedFile = "feed.txt";
	CompetitionType type = CompetitionType.CF;
	private static String ENDL = "\n";

	// solution
	long min, max;

	private void solve() throws Throwable {
		min = Integer.MAX_VALUE;
		max = 0;

		// System.out.println(ps.length);

		long n = lread();
		List<Long> factors = new ArrayList<Long>();

		int cur = 0;
		int[] pcnt = new int[ps.length];
		while (n > 1) {
			if (n % ps[cur] == 0) {
				factors.add((long) ps[cur]);
				n /= ps[cur];
				pcnt[cur]++;
			} else {
				cur++;
			}
		}

		go(pcnt, 1, 1, 1, factors.size(), 0, 0);

		long tot = 1;
		for (int i = 2;; i++) {
			tot *= i;
			if (tot > Integer.MAX_VALUE) {
				break;
			}
		}

		out.write(min + " " + max + ENDL);
		out.flush();
	}

	private void go(int[] pcnt, long a, long b, long c, int size, int pos, int starter) {
		if (size == 0) {
			long diff = diff(a, b, c);
			max = Math.max(diff, max);
			min = Math.min(diff, min);
			return;
		}

		while (pcnt[pos] == 0) {
			pos++;
			starter = 0;
			if (pos >= pcnt.length) {
				return;
			}
		}

		pcnt[pos]--;
		if (starter <= 0)
			go(pcnt, a * ps[pos], b, c, size - 1, pos, 0);
		if (starter <= 1)
			go(pcnt, a, b * ps[pos], c, size - 1, pos, 1);
		if (starter <= 2)
			go(pcnt, a, b, c * ps[pos], size - 1, pos, 2);
		pcnt[pos]++;
	}

	private long diff(long n, long i, long j) {
		return (n + 1) * (i + 2) * (j + 2) - n * i * j;
	}

	public int iread() throws Exception {
		return Integer.parseInt(wread());
	}

	public double dread() throws Exception {
		return Double.parseDouble(wread());
	}

	public long lread() throws Exception {
		return Long.parseLong(wread());
	}

	public String wread() throws IOException {
		StringBuilder b = new StringBuilder();
		int c;
		c = in.read();
		while (c >= 0 && c <= ' ')
			c = in.read();
		if (c < 0)
			return "";
		while (c > ' ') {
			b.append((char) c);
			c = in.read();
		}
		return b.toString();
	}

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

		if (test) { // run all cases from testfile:
			BufferedReader testdataReader = new BufferedReader(new FileReader(testDataFile));
			String readLine = testdataReader.readLine();
			int casenr = 0;
			out: while (true) {
				BufferedWriter w = new BufferedWriter(new FileWriter(feedFile));
				if (!readLine.equals("input")) {
					break;
				}
				while (true) {
					readLine = testdataReader.readLine();
					if (readLine.equals("output")) {
						break;
					}
					w.write(readLine + "\n");
				}
				w.close();
				System.out.println("Answer on case " + (++casenr) + ": ");

				new C102_div2().solve();
				System.out.println("Expected answer: ");

				while (true) {
					readLine = testdataReader.readLine();

					if (readLine == null) {
						break out;
					}
					if (readLine.equals("input")) {
						break;
					}
					System.out.println(readLine);
				}
				System.out.println("----------------");
			}
			testdataReader.close();
		} else { // run on server
			new C102_div2().solve();
		}
		out.close();
	}

	public C102_div2() throws Throwable {
		if (test) {
			in = new BufferedReader(new FileReader(new File(feedFile)));
		}
	}

	InputStreamReader inp = new InputStreamReader(System.in);
	BufferedReader in = new BufferedReader(inp);
	static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

	enum CompetitionType {
		CF, OTHER
	};
}


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.StringTokenizer;

public class Main {
	static Scanner sc = new Scanner(System.in);
	static PrintWriter pw = new PrintWriter(System.out);

	// shayfak yala ya yasser yala
	// :P
	static void input(int[] a, int n) throws Exception {
		for (int i = 0; i < n; i++)
			a[i] = sc.nextInt();
	}

	static long[] factors(int n) {
		long[] ans = new long[300];
		int index = 0  ;
		for (int i = 1; i <= Math.sqrt(n); i++)
			if (n % i == 0){
				ans[index++]=i;
				if(i!=n/i)
				ans[index++]=n/i ;
			}
		return ans;
	}

	public static void main(String[] args) throws Exception {
		// ----------------- Main -------------------------------
		int n = sc.nextInt();
		long x[] = factors(n);

		long max = 0;
		long min = Long.MAX_VALUE;
		for (int i = 0;x[i]!=0; i++)
			for (int j = 0; x[j]!=0; j++)
				for (int k = 0; x[k]!=0; k++) {
					long x1 =  x[i], x2 =  x[j], x3 =  x[k];
					if ((x1 ) * (x2 ) * (x3 ) == n) {
						long curr = ((x1+1) * (x2+2) * (x3+2)) - n;
						if (curr > max)max = curr;
						if (curr < min)min = curr;
					}
				}
		pw.println(min+" "+max);
		// ---------------------------------------------------------
		pw.flush();
		pw.close();

	}
}

class Scanner {
	StringTokenizer st;
	BufferedReader br;

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

	public Scanner(FileReader f) {
		br = new BufferedReader(f);
	}

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

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

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

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

	public double nextDouble() throws IOException {
		String x = next();
		StringBuilder sb = new StringBuilder("0");
		double res = 0, f = 1;
		boolean dec = false, neg = false;
		int start = 0;
		if (x.charAt(0) == '-') {
			neg = true;
			start++;
		}
		for (int i = start; i < x.length(); i++)
			if (x.charAt(i) == '.') {
				res = Long.parseLong(sb.toString());
				sb = new StringBuilder("0");
				dec = true;
			} else {
				sb.append(x.charAt(i));
				if (dec)
					f *= 10;
			}
		res += Long.parseLong(sb.toString()) / f;
		return res * (neg ? -1 : 1);

	}

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

}


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

public class Main {
	public static void main(String[] args) throws Exception {
		PrintWriter pr = new PrintWriter(System.out, true);
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		// Scanner s = new Scanner(System.in);
		// StringTokenizer st = new StringTokenizer(in.readLine());
		int n = Integer.parseInt(in.readLine());
		ArrayList<Integer> list = func(n);
		if(list.size() < 2) {
			list.add(1);
		}
		long aMin = list.get(0);
		long bMin = list.get(1);
		long resMin = aMin*2+(bMin+2)*2+(aMin+2)*(bMin+2);
		long resMax = n*2+9+n*3*2;
		pr.println(resMin+" "+resMax);
	}

	public static ArrayList<Integer> func(int n) {
		ArrayList<Integer> list = new ArrayList<Integer>();
		int k = n;
		for (int i = 2; i * i <= n; i++) {
			while (k % i == 0) {
				list.add(i);
				k /= i;
			}
		}
		if (k != 1) {
			list.add(k);
		}
		while(list.size() > 2) {
			if(list.get(0) <= list.get(list.size()-1)) {
				list.set(0, list.get(0)*list.get(1));
				list.remove(1);
			}else{
				list.set(list.size()-2,list.get(list.size()-2)*list.get(list.size()-1));
				list.remove(list.size()-1);
			}
		}
		return list;
	}
}


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

public class Main {
    static long min , max;
    public static void search (long arr [] , int idx , int rem)
    {
        if (idx == 2)
        {
            arr[2] = rem;
            long first = (arr[0] + 1) * (arr[1] + 2) * (arr[2] + 2);
            long second = (arr[0] + 2) * (arr[1] + 1) * (arr[2] + 2);
            long third = (arr[0] + 2) * (arr[1] + 2) * (arr[2] + 1);

            min = Math.min(min , Math.min(first , Math.min(second , third)));
            max = Math.max(max , Math.max(first , Math.max(second , third)));
            return;
        }

        for (int i = 1 ; i * i <= rem ; ++i)
            if (rem % i == 0)
            {
                arr[idx] = i;
                search(arr , idx + 1 , rem / i);
                if (rem / i != i)
                {
                    arr[idx] = rem / i;
                    search(arr , idx + 1 , i);
                }
            }

    }
    public static void main(String[]args) throws Exception {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        StringBuilder sb = new StringBuilder();
        int n = sc.nextInt();
        min = Long.MAX_VALUE;
        max = Long.MIN_VALUE;
        search(new int[3] , 0 , n);
        out.printf("%d %d" , min - n , max - n );
        out.flush();
        out.close();
    }

    static  class Scanner
    {
        StringTokenizer st;
        BufferedReader br;

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

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

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

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

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

        public double nextDouble() throws IOException
        {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if(x.charAt(0) == '-')
            {
                neg = true;
                start++;
            }
            for(int i = start; i < x.length(); i++)
                if(x.charAt(i) == '.')
                {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                }
                else
                {
                    sb.append(x.charAt(i));
                    if(dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg?-1:1);
        }

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

    }

}


import java.util.Scanner;

public class Main {

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

        while (true)
        {
            int h = s.length();
            s.replace("RU","D").replace("UR","D").length();
            if(h==s) break;
        }
        System.out.println(s.length());
    }
}


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

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner s = new Scanner(System.in);
		// your code goes here
		int n =s.nextInt();
		String s1 = s.next();
		String b = "";
		for(int i=0;i<s1.length();i++){
			if(s1.charAt(i)=='R' && i+1<s1.length() && s1.charAt(i+1)=='U'){
				b = b+'D';
				i++;
			}else if(s1.charAt(i)=='U' && i+1<s1.length() && s1.charAt(i+1)=='R'){
				b = b+'D';
				i++;
			}else{
				b = b+s1.charAt(i);
			}
		}
		System.out.print(b.length());
	}


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

public class Solution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Integer n = scanner.nextInt();
        int ans = 0;
        scanner.useDelimiter("");
        char old = ' ';
        boolean prev = false;
        for (int i = 0; i < n; i++) {
            if (now != old && !prev){
                ans --;
                prev = true;
            }
            else{
                prev = false;
            }
            old = now;
            ans++;

        }
        System.out.println(ans);
}


import java.io.*;
public class Diagonal {

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

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        int length = Integer.parseInt(br.readLine());
        String str = br.readLine();

        int count = 0;
        for(int i = 2; i < str.length(); i = i+2) {
            String sub = str.substring(j,i);
            if(sub.equalsIgnoreCase("UR") || sub.equalsIgnoreCase("RU")) {
                count++;
            }
            j = i;
        }
        System.out.println(str.length() - count);
    }
}


public class ebonyandivory {

	public static void main(String[] args) {
		Scanner ss = new Scanner (System.in)  ;
		int a  = ss.nextInt() ;
		int b = ss.nextInt()  ;
		int c = ss.nextInt()  ;
		int t = 0 ;

		for (int x = 0;t<c; x++){
			for(int y = 0 ;t<c ; y++){
				t= x*a+y*b ;
			}
			if (t==c)
				break ;
			else if (x==0 && t > c)
				t = 0 ;
		}
		if (t == c )
			System.out.println("YES");
		else
			System.out.println("NO");
	}

}


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

public class EbonyAndIvory {

 public static void main(String[] args){

  MyScannercf1 in = new MyScannercf1();
  int x = in.nextInt();
  int y = in.nextInt();
  int z = in.nextInt();
  int flag = 0;
  int i = 0;

  for(i = 0; i <= 10000; i++){
   if((z-i*y)%x == 0){
    if(((z-i*y)/x)<0)
     break;
    System.out.println("Yes");

    flag = 1;
    break;
   }
  }

  if(flag == 0)
   System.out.println("No");
 }
}


public class g {

	 public static  void main(String [] args) throws  IOException{
		// BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		 Scanner sc=new Scanner(System.in);

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

  boolean flag=false;
    for(int i=0;i< c/a+2;i++)
    {
        for(int j=0;j<c/b+2;j++)
        {
            if(i*a+j*b==c)
            {
                System.out.print("YES");
                flag=true;

            }
        }
    }
    if(!flag){
    	System.out.print("YES");
    }

	 }
	 }


import java.io.*;
import java.util.*;
public class Main{
    static Scanner sc=new Scanner(System.in);
    static long x,y,g;
    static long gcd(long a,long b){
        if(b==0) return a;
        else return gcd(b,a%b);
        return 0;
    }
    static boolean find_any_solution(long a,long b,long c){
        long g=gcd(Math.abs(a),Math.abs(b));
        if((c%g)!=0)
            return false;
        return true;
    }
    public static void main(String args[]){
        long t;
        t=sc.nextLong();
        long ite;
        for(ite=1;ite<=t;ite++){
            long a,b,c;
            a=sc.nextLong();
            b=sc.nextLong();
            c=sc.nextLong();
            if(find_any_solution(a,b,c)==true){
                System.out.println("Yes");
            }
            else{
                System.out.println("No");
            }
        }
    }
}


import static java.lang.Math.abs;
import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Haythem
 */
public class NewMain3 {
static Scanner scanner = new Scanner(System.in);

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        int f1 = Integer.parseInt(scanner.nextLine());

                int f2 = Integer.parseInt(scanner.nextLine());
int a1= abs(f1-f2)/2;
int a2= abs(f1-f2)-a1;

int nb=0;
for (int i=0;i<=a1;i++)
{
    nb= nb+i;
}
for (int i=0;i<=a2;i++)
{
    nb= nb+i;
}                    System.out.println(nb);

    }


import java.util.*;
import java.util.Scanner;
public class mainn
{
    Scanner input=new Scanner(System.in);
    int a=input.nextInt();
    int b=input.nextInt();
    int diff=a>b?(a-b):(b-a);
    int out=0;
    if(diff%2==0)
    {
        for(int i=1;i<=diff/2;i++)
        {
            out=out+i;
        }
        out=out*2;
    }
    else
    {
        for(int i=1;i<=diff/2;i++)
        {
            out=out+i;
        }
        out=out*2;
        out=out+i+1;
    }
    System.out.println(out);

}


import java.util.*;
import java.lang.*;
public class Answer {
    public static void main(String args[]) {

        int x=0;int y=0;
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        int b=sc.nextInt();
        float sol=Math.abs(a-b);
        x=0;y=0;
        if(sol%2==0)
        {
            x=(int)sol/2;
            sol=(x*(x+1))/2;
            sol+=v;
        }
        else
        {
            x=(int)Math.ceil(sol/2.0);
            //System.out.println("x "+x);
            y=x-1;
            sol=(x*(x+1))/2;
            y=(y*(y+1))/2;
            sol=sol+y;
        }

        System.out.println((int)sol);
    }
}


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 IOException {

BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
        int a=Integer.parseInt(br.readLine());
        int b=Integer.parseInt(br.readLine());
         int max=Math.max(a,b);
          int min=Math.min(a,b);
          int i=1,c1=0,c2=0;
          while(max-min>1)
          {
              c1+=i;
              c2+=i;
              max--;min++;i++;
          }
          if(max-min==1)
            System.out.println(c1+c2+i);
        else
        System.out.println(c1+c2);
}
}
close


mport java.util.HashSet;
import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

public class lol {
       public static void main(String args[]){
 Scanner s = new Scanner(System.in);
	int n=s.nextInt();
        int []a = new int[2];
        a[0]=0;
        a[1]=0;
        int c=0,ph=0;
        ArrayList p=new ArrayList();
        for (int i = 0; i < n; i++) {
              int g=s.nextInt();
             if(g==0){
               a[0]=0;
               a[1]=0;
              }
             else if(g==1){
                a[0]=0;
                if(a[1]==1){
                a[1]=0;
                 }else{
                a[1]=1;
                }
                }
             else if(g==2){
                a[1]=0;
                if(a[0]==1){
                a[0]=0;
               }else{
                a[0]=1;
                }
               }
//             else{
//
//            if(a[0]==1){a[0]=0; }else{a[0]=1;}
//            if(a[1]==1){a[1]=0; }else{a[1]=1;}
//
//                      }

          if(g==3){
                 if((a[0]!=1||a[1]!=1)&&(a[0]+a[1]!=0)){
               if(a[0]==0&&a[1]==1){
                   a[0]=1;a[1]=0;
              }
              else{
                   a[0]=0;a[1]=1;
              }
          }
          }else{

           if(a[0]==0&&a[1]==0){
              c++;

              }
          }

          ph=g;

           }
                       System.out.println(c);

}
}


import java.util.*;

public class C363_Vacations {
    public static void main(String[] args) throws IOException{
       // Scanner scanner = new Scanner(System.in);
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		StringTokenizer st = new StringTokenizer(br.readLine());
        int count = 0, cur, prev = 3;
        // st = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; i++) {
            cur = Integer.parseInt(st.nextToken());
            if (cur == prev && cur != 3) cur = 0;
            else if (cur == 3 && prev != 3) cur -= prev;
            if (cur == 0) count++;
            prev = cur;
        }
        System.out.println(count);

    }
}


import Codeforces.C.TheTwoRoutes.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {

    static int arr[];

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

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(in.readLine());

        arr = new int[n + 1];

        StringTokenizer stk = new StringTokenizer(in.readLine());

        for (int i = 1; i <= n; i++) {
            arr[i] = Integer.parseInt(stk.nextToken());
        }
        System.out.println(solve(1, -1, 0));

    }

    public static int solve(int day, int lastChoice, int total) {
        if (day >= arr.length) {
            return total;
        }

        int ans = 0;

        if (arr[day] == 0) {
            ans = solve(day + 1, 0, total + 1);
        } else if (arr[day] == 3) {
            if (day > 1 && day < arr.length - 1 && arr[day - 1] != arr[day + 1]) {
                ans = solve(day + 1, 0, total + 1);
            } else {
                ans = Integer.min(ans, solve(day + 1, 1, total));
                ans = Integer.min(ans, solve(day + 1, 2, total));
            }
        } else {
            if (arr[day] == lastChoice) {
                ans = solve(day + 1, arr[day], total + 1);
            } else {
                ans = solve(day + 1, arr[day], total);
            }
        }
        return ans;
    }
}


import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

public class lol {
       public static void main(String args[]){
 Scanner s = new Scanner(System.in);
	int n=s.nextInt();
        int []a = new int[2];
        a[0]=0;
        a[1]=0;
        int c=0,ph=0;
        ArrayList p=new ArrayList();
        for (int i = 0; i < n; i++) {
              int g=s.nextInt();
             if(g==0){
               a[0]=0;
               a[1]=0;
              }
             else if(g==1){
                a[0]=0;
                if(a[1]==1){
                a[1]=0;
                 }else{
                a[1]=1;
                }
                }
             else if(g==2){
                a[1]=0;
                if(a[0]==1){
                a[0]=0;
               }else{
                a[0]=1;
                }
               }
             else{

            if(a[0]==1){a[0]=0; }else{a[0]=1;}
            if(a[1]==1){a[1]=0; }else{a[1]=1;}

                      }

          if(g==3){
                 if(ph!=3){

          }
          }else{

           if(a[0]==0&&a[1]==0){
              c++;

              }
          }

          ph=g;

           }
                       System.out.println(c);

}


/*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
What do you think? What do you think?
1st on Billboard, what do you think of it
Next is a Grammy, what do you think of it
However you think, I’m sorry, but shit, I have no fcking interest
*******************************
I'm standing on top of my Monopoly board
That meres I'm on top of my game and it don't stop
til my hip don't hop anymore
https://www.a2oj.com/Ladder16.html
*******************************
Shining through the city with a little funk and soul
*/
import java.util.*;
import java.io.*;
import java.math.*;
import static java.lang.Math.max;
import static java.lang.Math.min;

   public class C
   {
      public static void main(String hi[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
         StringTokenizer st = new StringTokenizer(infile.readLine());
         La = Long.parseLong(st.nextToken());
         Ra = Long.parseLong(st.nextToken())+1;
         Ta = Long.parseLong(st.nextToken());
         st = new StringTokenizer(infile.readLine());
         Lb = Long.parseLong(st.nextToken());
         Rb = Long.parseLong(st.nextToken())+1;
         Tb = Long.parseLong(st.nextToken());
         long gcd = gcd(Ta, Tb);
         long shift = ((Lb - La) % gcd + gcd) % gcd;
   		long sa = Ra - La, sb = Rb - Lb;
   		long res = min(sa, sb + shift) - max(0, shift);
   		shift -= gcd;
   		res = max(res, min(sa, sb + shift) - max(0, shift));
   		res = max(res, 0);
   		System.out.print(res);
      }
      static long La, Ra, Ta, Lb, Rb, Tb;
      public static long gcd(long a, long b)
      {
         if(a > b)
         {
            long t = a;
            a = b;
            b = t;
         }
         if(a == 0L)
            return b;
         return gcd(b%a, a);
      }
   }


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
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 Egor Kulikov (egor@egork.net)
 */
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);
        CUdachnieDni solver = new CUdachnieDni();
        solver.solve(1, in, out);
        out.close();
    }

    static class CUdachnieDni {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int la = in.readInt();
            int ra = in.readInt();
            int ta = in.readInt();
            int lb = in.readInt();
            int rb = in.readInt();
            int tb = in.readInt();
            int g = IntegerUtils.gcd(ta, tb);
            int answer = 0;
            int qa = la / g;
            la -= qa * g;
            ra -= qa * g;
            int qb = lb / g;
            lb -= qb * g;
            rb -= qb * g;
            answer = Math.max(answer, getAnswer(la, ra, lb, rb));
            answer = Math.max(answer, getAnswer(la + g, ra + g, lb, rb));
            answer = Math.max(answer, getAnswer(la, ra, lb + g, rb + g));
            out.printLine(answer);
        }

        private int getAnswer(int la, int ra, int lb, int rb) {
            return Math.min(ra, rb) - Math.max(la, lb) + 1;
        }

    }

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

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

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

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

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

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

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

        }

    }

    static class IntegerUtils {
        public static int gcd(int a, int b) {
            a = Math.abs(a);
            b = Math.abs(b);
            while (b != 0) {
                int temp = a % b;
                a = b;
                b = temp;
            }
            return a;
        }

    }

    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 printLine(int i) {
            writer.println(i);
        }

    }
}


import static java.lang.Math.*;

import java.math.*;

public class C {

	public Object solve() {
		long LA = sc.nextLong(), RA = sc.nextLong(), TA = sc.nextLong();
		long LB = sc.nextLong(), RB = sc.nextLong(), TB = sc.nextLong();

		long SA = RA - LA, SB = RB - LB;
		long G = gcd(TA, TB), D = mod(LA - LB, G);

		long res = calc(D, 0, SA, SB);
		res = max(res, calc(D-G, 0, SA, SB));
		return print(res);
	}

	long calc(long LA, long LB, long SA, long SB) {
		long RA = LA + SA, RB = LB + SB;
		long L = max(LA, LB), R = min(RA, RB);
		long res = max(0, R - L + 1);
		return res;
	}

	private static final boolean ONE_TEST_CASE = true;

	private static void init() {
	}

	private static long gcd (long x, long y) { return BigInteger.valueOf(x).gcd(BigInteger.valueOf(y)).longValue(); }
	private static long mod (long x, long mod) { return ((x % mod) + mod) % mod; }
	//////////////////////////////////////////////////////////////////////////////////// OFF
	private static IOUtils.MyScanner sc = new IOUtils.MyScanner();
	private static Object print (Object o, Object ... A) { IOUtils.print(o, A); return null; }
	private static class IOUtils {
		public static class MyScanner {
			public String next() { newLine(); return line[index++]; }
			public int nextInt() { return Integer.parseInt(next()); }
			public long nextLong() { return Long.parseLong(next()); }
			//////////////////////////////////////////////
			private boolean eol() { return index == line.length; }
			private String readLine() {
				try {
					return r.readLine();
				} catch (Exception e) {
					throw new Error (e);
				}
			}
			private final java.io.BufferedReader r;
			private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); }
			private MyScanner (java.io.BufferedReader r) {
				try {
					this.r = r;
					while (!r.ready())
						Thread.sleep(1);
					start();
				} catch (Exception e) {
					throw new Error(e);
				}
			}
			private String [] line;
			private int index;
			private void newLine() {
				if (line == null || eol()) {
					line = split(readLine());
					index = 0;
				}
			}
			private String [] split(String s) { return s.length() > 0 ? s.split(" ") : new String [0]; }
		}
		private static String build(Object o, Object ... A) { return buildDelim(" ", o, A); }
		private static String buildDelim(String delim, Object o, Object ... A) {
			StringBuilder b = new StringBuilder();
			append(b, o, delim);
			for (Object p : A)
				append(b, p, delim);
			return b.substring(delim.length());
		}
		//////////////////////////////////////////////////////////////////////////////////
		private static final java.text.DecimalFormat formatter = new java.text.DecimalFormat("#.#########");
		private static void start() { if (t == 0) t = millis(); }
		private static void append(java.util.function.Consumer<Object> f, java.util.function.Consumer<Object> g, final Object o) {
			if (o.getClass().isArray()) {
				int len = java.lang.reflect.Array.getLength(o);
				for (int i = 0; i < len; ++i)
					f.accept(java.lang.reflect.Array.get(o, i));
			}
			else if (o instanceof Iterable<?>)
				((Iterable<?>)o).forEach(f::accept);
			else
				g.accept(o instanceof Double ? formatter.format(o) : o);
		}
		private static void append(final StringBuilder b, Object o, final String delim) {
			append(x -> { append(b, x, delim); }, x -> b.append(delim).append(x), o);
		}
		private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out);
		private static Object print(Object o, Object ... A) { pw.println(build(o, A)); return null; }
		private static void err(Object o, Object ... A) { System.err.println(build(o, A)); }
		private static boolean PRINT;
		private static void write(Object o) {
			err(o, '(', time(), ')');
			if (PRINT)
				pw.println(o);
		}
		private static void exit() {
			IOUtils.pw.close();
			System.out.flush();
			err("------------------");
			err(time());
			System.exit(0);
		}
		private static long t;
		private static long millis() { return System.currentTimeMillis(); }
		private static String time() { return "Time: " + (millis() - t) / 1000.0; }
		private static void run(int N) {
			try { PRINT = System.getProperties().containsKey("PRINT"); }
			catch (Throwable t) {}

			for (int n = 1; n <= N; ++n) {
				Object res = new C().solve();
				if (res != null)
					write("Case #" + n + ": " + build(res));
			}
			exit();
		}
	}
	////////////////////////////////////////////////////////////////////////////////////
	public static void main(String[] args) {
		init();
		int N = ONE_TEST_CASE ? 1 : sc.nextInt();
		IOUtils.run(N);
	}
}


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

public class lucky {

	public static void main(String[] args) throws Exception {
		FastIO sc = new FastIO(System.in);
		PrintWriter pw = new PrintWriter(System.out);

		long l_a = sc.nextInt(), r_a =sc.nextInt(), t_a = sc.nextInt();
		long l_b = sc.nextInt(), r_b = sc.nextInt(), t_b = sc.nextInt();

		long length_a = r_a-l_a;
		long length_b = r_b-l_b;

		long gcd = GCD(t_a, t_b);

		long offset = (((l_a-l_b)%gcd)+gcd)%gcd;

		long start = l_b + offset;
		long end = Math.min(l_b+offset+length_a, l_b+length_b);

		long ans = end-start+1;

		offset = (((l_b-l_a)%gcd)+gcd)%gcd;

		start = l_a + offset;
		end = Math.min(l_a+offset+length_b, l_a+length_a);

		ans = Math.max(ans, end-start+1);

		ans = Math.max(ans, 0);

		pw.println(ans);
		pw.close();
	}

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

	static class FastIO {

		//Is your Fast I/O being bad?

		InputStream dis;
		byte[] buffer = new byte[1 << 17];
		int pointer = 0;

		public FastIO(String fileName) throws Exception {
			dis = new FileInputStream(fileName);
		}

		public FastIO(InputStream is) throws Exception {
			dis = is;
		}

		int nextInt() throws Exception {
			int ret = 0;

			byte b;
			do {
				b = nextByte();
			} while (b <= ' ');
			boolean negative = false;
			if (b == '-') {
				negative = true;
				b = nextByte();
			}
			while (b >= '0' && b <= '9') {
				ret = 10 * ret + b - '0';
				b = nextByte();
			}

			return (negative) ? -ret : ret;
		}

		long nextLong() throws Exception {
			long ret = 0;

			byte b;
			do {
				b = nextByte();
			} while (b <= ' ');
			boolean negative = false;
			if (b == '-') {
				negative = true;
				b = nextByte();
			}
			while (b >= '0' && b <= '9') {
				ret = 10 * ret + b - '0';
				b = nextByte();
			}

			return (negative) ? -ret : ret;
		}

		byte nextByte() throws Exception {
			if (pointer == buffer.length) {
				dis.read(buffer, 0, buffer.length);
				pointer = 0;
			}
			return buffer[pointer++];
		}

		String next() throws Exception {
			StringBuffer ret = new StringBuffer();

			byte b;
			do {
				b = nextByte();
			} while (b <= ' ');
			while (b > ' ') {
				ret.appendCodePoint(b);
				b = nextByte();
			}

			return ret.toString();
		}

	}
}


import java.util.Scanner;

public class ElPequeñoElefante {
    public static void main(String...args){
        Scanner entrada =new Scanner(System.in);
        ArrayList <Integer> lista = new ArrayList<>();
        ArrayList <Integer> divisores = new ArrayList<>();
        int x= Integer.parseInt(entrada.nextLine());
        String numero=""+x;
        int d=1;
        if(x==1){
            System.out.println("1");
            return;
        }
        if(x%2==0 && x%5==0){//si es 10,100,......1000000000
            d=2;
            for(int i=2; i<x; i*=2){
                if(x%i==0){
                    String cadena=""+i;

                    for(int j=0; j<cadena.length(); j++ ){
                       String m =""+cadena.charAt(j);
                       if(numero.indexOf(cadena)!=-1 || numero.indexOf(m)!=-1){
                         d++;
                         break;
                       }
                   }
                }
            }
            for(int i=5; i<x; i+=5){
                if(x%i==0){
                    String cadena=""+i;

                    for(int j=0; j<cadena.length(); j++ ){
                       String m =""+cadena.charAt(j);
                       if(numero.indexOf(cadena)!=-1 || numero.indexOf(m)!=-1){
                          d++;
                          break;
                       }
                    }
                 }
            }
            System.out.println(d);return;
        }

        for(int i=1; i<x; i++){
            if(x%i==0){
                String cadena=""+i;

                for(int j=0; j<cadena.length(); j++ ){
                    String m =""+cadena.charAt(j);
                    if(numero.indexOf(cadena)!=-1 || numero.indexOf(m)!=-1){
                       d++;
                       break;
                    }
                }
            }

        }

        System.out.println(d);
    }
}


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

public class C {

	static boolean digitoComum(long a, long b){
		String sA=a+"";
		String sB=b+"";

		for(char c1: sA.toCharArray()){
			for(char c2: sB.toCharArray()){
				if(c1==c2)return true;
			}
		}

		return false;

	}

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

		long x= sc.nextLong();

		ArrayList<Long>divizores= new ArrayList<>();

		for(long i=1;i<=Math.sqrt(x);i++){
			if(x%i==0){
				divizores.add((long) i);
				if((x/i)!=i)divizores.add((long)(x/i));
			}
		}
		long resp=0;
		for(Long l:divizores){
			if(digitoComum(x, l.longValue())){resp++;
			//System.out.println("numero"+l);}
		}
		System.out.println(resp);
	}

}


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

public class B {

    private void solve() throws IOException {
        int x = nextInt(), tmp, ans=0;
        int v2[] = new int[10];
        int v1[] = new int[10];
        for(tmp=x; tmp!=0; tmp/=10){
            v1[tmp%10]=1;
        }
        for(int i=1; i*i<=x; i++){
            if (x%i==0)
            {
                for(int j=0; j<10; j++){
                    v2[j]=0;
                }
                for(tmp=i; tmp!=0; tmp/=10){
                    v2[tmp%10]=1;
                }
                for(int j=0; j<10; j++){
                    if (v1[j]>0 && v2[j]>0){
                        ans++;
                        break;
                    }
                }
                if (x/i!=i){
                    for(int j=0; j<10; j++){
                        v2[j]=0;
                    }
                    for(tmp=x/i; tmp!=0; tmp/=10){
                        v2[tmp%10]=1;
                    }
                    for(int j=0; j<10; j++){
                        if (v1[j]>0 && v2[j]>0){
                            ans++;
                            break;
                        }
                    }
                }
            }
        }
        println(ans);
    }

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

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

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

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

    private void print(Object o) {
        writer.print(o);
    }

    private void println(Object o) {
        writer.println(o);
    }

    private void printf(String format, Object... o) {
        writer.printf(format, o);
    }

    public static void main(String[] args) {
        long time = System.currentTimeMillis();
        Locale.setDefault(Locale.US);
        new A().run();
        System.err.printf("%.3f\n", 1e-3 * (System.currentTimeMillis() - time));
    }

    BufferedReader reader;
    StringTokenizer tokenizer;
    PrintWriter writer;

    private void run() {
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            writer = new PrintWriter(System.out);
            solve();
            reader.close();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(13);
        }
    }
}


import java.io.*;
import java.util.*;
public class Main{
    public static int check(int n,int x){
        String main_str = String.valueOf(n);
        String sub_str = String.valueOf(x);
        if(sub_str.length()>1){
            int flag=0;
        for(int i=0;i<sub_str.length();i++){
            if(main_str.indexOf(sub_str.charAt(i))!=-1)
            flag=1;
        }
        return flag;
        }
        else{
            if(main_str.contains(sub_str))
            return 1;
            else
            return 0;
        }
    }
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int res=0;
        for(int j=1;j<=n;j++)
        {
            if(n%j==0)
            {
                int ct=check(n,j);
                res=res+ct;
            }
        }
        System.out.println(res);


import java.util.*;
import java.lang.*;
public class newvac
{
	public static void main(String[] args)
	{
		Scanner s=new Scanner(System.in);
		//System.out.println("enter number");
		int n=s.nextInt();
		String str=s.nextLine();
		if (str.contains("CC")||str.contains("MM")||str.contains("YY"));
		{
			System.out.println("No");
			return;
		}
		if(str.startsWith("?")||str.endsWith("?")||str.contains("C?C")||str.contains("Y?Y")||str.contains("M?M"))
		{
			System.out.println("Yes");
			//return;
		}
		else
		{
			System.out.println("No");
		}
	}
}


package codeforces;

public class March1 {
public static void main(String[] args) {
	Scanner scan = new Scanner(System.in);
	int n= scan.nextInt();
	String s = scan.next();
	for(int i=0;i<n-1;i++) {
		if(s.charAt(i)=='?'&&s.charAt(i+1)=='?')
		{
			continue;
		}
		else if	(s.charAt(i)==s.charAt(i+1)) {
			System.out.println("No");
			return;
	    }
	}
	if(s.charAt(0)=='?') {
		System.out.println("Yes");
		return;
	}
	if(s.charAt(n-1)=='?') {
		System.out.println("Yes");
		return;
	}
	int temp=0;
	for(int i=1;i<n-1;i++) {
		if(s.charAt(i)=='?') {

			if(s.charAt(i-1)!=s.charAt(i+1)){
				temp++;
			}
			else if(s.charAt(i-1)=='?'&&s.charAt(i+1)!='?'||s.charAt(i-1)!='?'&&s.charAt(i+1)=='?') {
				temp +=2;
			}
			else {
			temp +=2;
			}
		}
		if(temp>=2) {
			System.out.println("Yes");
			return;
		}
	}
	System.out.println("No");

}
}


import java.util.*;
public class sample{
  public static void main(String[] args) {
    int len,i,pos=-1;
    Boolean f=true;
    Scanner s = new Scanner(System.in);
    len=s.nextInt();
    String a;
    a=s.next();
    for(i=0;i<len-1;i++){
      if(a.charAt(i)==a.charAt(i+1)){
        if(a.charAt(i)!='?'){
        f=false;
        //System.out.println(i);}
      }

    }
    for(i=0;i<len-1;i++){
      if((a.charAt(i)==a.charAt(i+1))&&(a.charAt(i)=='?')){
        pos=i;
        break;
      }
    }
    if(pos!=-1){
      if(a.charAt(i-1)!=a.charAt(i+2)){
        f=false;     //   System.out.println(i);

      }
    }
    if(f)
    System.out.print("YES");
    else
    System.out.print("NO");

  }
}


import java.util.*;

public class Main {
    /*  CYM
    CY??Y

    5
    C?C?Y => true
     */

    private static final String COLORS = "CYM";
    private static void canPaintTwoWays(String canvas, char[] chars, int start, int[] ways) {
        if (start == chars.length) {
            ways[0]++;
            return;
        }

        if (canvas.charAt(start) != '?') {
            if (start > 0 && canvas.charAt(start) == canvas.charAt(start - 1)) {
                return;
            } else {
                canPaintTwoWays(canvas, chars, start + 1, ways);
                return;
            }
        }

        for (int i = 0; i < COLORS.length(); i++) {
            if (start > 0 && COLORS.charAt(i) == chars[start - 1]) {
                continue;
            }
            chars[start] = COLORS.charAt(i);
            canPaintTwoWays(canvas, chars, start + 1, ways);
            if (ways[0] >= 2) {
                return;
            }
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        String canvas = scan.next();
        int[] ways = {0};
        canPaintTwoWays(canvas, canvas.toCharArray(), 0, ways);
        System.out.println(ways[0] >= 2);
        scan.close()
    }
}


import java.util.*;
public class hungry
{
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int n,i;
        n=in.nextInt();
        for(i=1;ii<=n;i++)
        {
            int x1,a,b;
            boolean bool=false;
            boolean bo=true;
            x1=in.nextInt();
            if(x1%3==0 || x1%7==0)
            {
                System.out.println("Yes");
                bool=true;
            }
            else if(x1%3!=0 && x1%7!=0 && x1<10)
            {
                System.out.println("No");
                bool=true;
            }
            for(a=7;a<x1;a=a+7)
            {
                b=x1-a;
                if(b%3==0)
                {
                    if(bool==false)
                    {
                    System.out.println("YES");
                    bo=false;
                }
            }
            if(bo==true)
            {
                System.out.println("NO");
            }
            }
        }
    }
}


import java.util.Scanner;

public class chunks {

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

		int i=0;
		while(i<t) {
			boolean found=false;
			int x=scan.nextInt();
			if(x%3==0||x%7==0) System.out.println("YES");
			else {
				int j=1;
				while(x>=3) {
					x=x-7;
					if(x%3==0) {
						System.out.println("YES");
						found=true;
						break;
					}

					}
				if(found==false) System.out.println("NO");
			}

			i++;
		}


import java.util.Scanner;

public class A{
   public static void main(String[] args){
       Scanner in = new Scanner(System.in);
       int n = in.nextInt();
       for(int i=0;i<n;i++)
       {
           int x = in.nextInt();
           if(x%3==0||x%7==0||x%10==0){
               System.out.println("YES");
           }
           else {
               System.out.println("NO")
           }
       }

   }

}


import java.util.Scanner;

public class A{
   public static void main(String[] args){
       Scanner in = new Scanner(System.in);
       int n = in.nextInt();
       for(int i=0;i<n;i++)
       {
           int x = in.nextInt();
           if(x%3==0||x%7==0||x%10==0){
               System.out.println("YES");
           }
           else {
               System.out.println("NO");
           }
       }

   }


import java.util.*;

public class Main
{
	public static void main (String[] args)
	{
		Scanner read= new Scanner(System.in);
		String entrada=read.next();

		if(entrada.IndexOf("week")==-1)
		{
			if((entrada.substring(0)=="1")||(entrada.substring(0)=="2")||(entrada.substring(0)=="3")||(entrada.substring(0)=="4"))
			{
				System.out.println("52");

			}
			else
			{
				System.out.println("53");
			}

		}
		else
		{
			if((entrada.substring(0,1)=="30")||(entrada.substring(0,1)=="31"))
			{
				if(entrada.substring(0,1)=="30")
				{
					System.out.println("11");
				}
				else
				{
					System.out.println("7");
				}

			}
			else
			{
				System.out.println("12");
			}
		}

	}
}


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

public class Main {
  static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
  static StringTokenizer st = new StringTokenizer("");

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

  public static void main(String[] asda) throws Exception {
    InputReader reader = new InputReader(System.in);
    int N = Integer.parseInt(next());
    next();
    String s = next();
    int ans = 0;
    if (s.equals("week")) {
      ans = 52;
      if (N == 1) {
        ans++;
      }
    } else  {
      if (N <= 28) {
        ans = 12;
      } else if (N <= 30) {
        ans = 11;
      } else if (N == 31) {
        ans = 7;
      }
    }
    out.println(ans);
    //
    out.flush();
    System.exit(0);
  }
}


import java.util.Scanner;

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

        int val = scan.nextInt();

        String type = scan.nextLine();

        if(type.equals("of week"){
            if(val == 1 || val == 2 || val == 3 || val == 7){
                System.out.println(52);
            }else{
                System.out.println(53);
            }
        }else{
            if(val <=29){
                System.out.println(12);
            }else if(val == 30){
                System.out.println(11);
            }else{
                System.out.println(8);
            }
        }
    }
}


import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Main {

    public static int[][]map;
    static ArrayList<Integer>y =new ArrayList<Integer>();
    static ArrayList<Integer>h =new ArrayList<Integer>();
    public static boolean [][]visited;
    public static int max=0;
    public static int m;
    public static int n;
    public static void main(String[]args)
    {
        Calendar c = new GregorianCalendar();
        Scanner x =new Scanner (System.in);
        int year = c.get(Calendar.YEAR);

        while(x.hasNext())
        {
            String a=x.nextLine();
            String []y=a.split(" ");
            if(y[2].equals("week"))
            {
                int week=Integer.parseInt(y[0]);
                if(week<=4||week==7)
                {
                    System.out.println(52);
                }
                else if(week==5||week==6)
                {
                    System.out.println(53);

                }

            }
            else
            {

                int month=Integer.parseInt(y[0]);

                if(month<=28)
                {
                    System.out.println(12);
                }
                else if(month>28&&month<=30)
                {
                    System.out.println(11);
                }
                else
                {
                    System.out.println(7);
                }

            }

    }

        }

}


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

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

class TaskC2 {
    static final int INF = (int) 1e9;

    int n;
    int m;
    int[][] match;
    int[][] mark;
    int generation = 0;
    int[] dr = new int[]{0, 1, 0, -1};
    int[] dc = new int[]{1, 0, -1, 0};

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        n = in.nextInt();
        m = in.nextInt();
        int[][] a = new int[n][m];
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < m; ++j)
                a[i][j] = in.nextInt();
        match = new int[n][m];
        for (int[] x : match) Arrays.fill(x, -1);
        mark = new int[n][m];
        int size = 0;
        int res = n * m / 2;
        for (int r = 0; r < n; ++r)
            for (int c = 0; c < m; ++c) if ((r + c) % 2 == 0)
                for (int d = 0; d < 4; ++d) {
                    int nr = r + dr[d];
                    int nc = c + dc[d];
                    if (nr < 0 || nr >= n || nc < 0 || nc >= m) continue;
                    if (a[r][c] == a[nr][nc]) {
                        match[r][c] = d;
                        match[nr][nc] = (d + 2) % 4;
                        mark[r][c] = INF;
                        mark[nr][nc] = INF;
                        ++size;
                        --res;
                    }
                }
        for (int r = 0; r < n; ++r)
            for (int c = 0; c < m; ++c) if ((r + c) % 2 == 0 && match[r][c] < 0)
                if (improve(r, c)) {
                    ++size;
                }
        for (int r = 0; r < n; ++r)
            for (int c = 0; c < m; ++c) if ((r + c) % 2 == 0)
                for (int d = 0; d < 4; ++d) {
                    int nr = r + dr[d];
                    int nc = c + dc[d];
                    if (nr < 0 || nr >= n || nc < 0 || nc >= m) continue;
                    if (a[r][c] == a[nr][nc]) {
                        mark[r][c] = 0;
                        mark[nr][nc] = 0;
                        match[r][c] = -1;
                        match[nr][nc] = -1;
                        boolean ok = improve(r, c);
                        ok &= improve(nr, nc);
                        if (ok) {
                            ++res;
                            ++size;
                        } else {
                            if (match[r][c] == d) {
                                mark[r][c] = INF;
                                mark[nr][nc] = INF;
                            } else {
                                int sm = match[r][c];
                                int sm2 = match[nr][nc];
                                if (sm >= 0) {
                                    match[r + dr[sm]][c + dc[sm]] = -1;
                                }
                                if (sm2 >= 0) {
                                    match[nr + dr[sm2]][nc + dc[sm2]] = -1;
                                }
                                match[r][c] = d;
                                match[nr][nc] = (d + 2) % 4;
                                mark[r][c] = INF;
                                mark[nr][nc] = INF;
                                improve(r + dr[sm], c + dc[sm]);
                                improve(nr + dr[sm2], nc + dc[sm2]);
                            }
                        }
                    }
                }
        if (size != n * m / 2) throw new RuntimeException();
        out.println(res);
    }

    private boolean improve(int r, int c) {
        ++generation;
        return dfs(r, c);
    }

    private boolean dfs(int r, int c) {
        if (mark[r][c] >= generation) return false;
        mark[r][c] = generation;
        for (int d = 0; d < 4; ++d) {
            int nr = r + dr[d];
            int nc = c + dc[d];
            if (nr < 0 || nr >= n || nc < 0 || nc >= m) continue;
            int om = match[nr][nc];
            if (om < 0 || dfs(nr + dr[om], nc + dc[om])) {
                match[nr][nc] = (d + 2) % 4;
                match[r][c] = d;
                return true;
            }
        }
        return false;
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

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

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

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

    }


import java.awt.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Main10004 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int[][] arr = new int[n][m];
        int num = 0;
        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                arr[i][j]=scan.nextInt();
            }
        }
        for (int i=0;i<n;i++){
            for (int j=0;j<m-1;j++){
               if (arr[i][j]==arr[i][j+1]){
                    arr[i][j]=0;
                    arr[i][j+1]=0;
                }
            }
        }
        for (int i=0;i<n-1;i++){
            for (int j=0;j<m;j++){
                if (arr[i][j]==arr[i+1][j]){
                    arr[i][j]=0;
                    arr[i+1][j]=0;
                }
            }
        }
        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                if (arr[i][j]!=0){
                    num +=1;
                }
            }
        }

        int single = getSingle(n,m,arr);
        num = num/2+single;
        System.out.println(num);

    }

    private static int getSingle(int n, int m, int[][] arr) {
        int num=0;
        for (int i=0;i<n;i++){
            for (int j=0;j<m-1;j++){
                if (arr[i][j]!=0&&arr[i][j+1]!=0){
                    arr[i][j]=-1;
                    arr[i][j+1]=-1;
                }
            }
        }
        for (int i=0;i<n-1;i++){
            for (int j=0;j<m;j++){
                if (arr[i][j]!=0&&arr[i+1][j]!=0){
                    arr[i][j]=-1;
                    arr[i+1][j]=-1;
                }
            }
        }
        ArrayList<Point> list = new ArrayList();
        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                if (arr[i][j]!=0&&arr[i][j]!=-1){
                    Point p = new Point(i,j);
                    list.add(p);
                    num +=1;
                }
            }
        }

        int temp =0;
        for (int i=0;i< list.size()-1;i++){
            for (int j=i+1;j< list.size();j++){
                Point p1 = list.get(i);
                Point p2 = list.get(j);
                if (Math.abs(p1.x-p2.x)==2&&Math.abs(p1.y-p2.y)==1||Math.abs(p1.x-p2.x)==1&&Math.abs(p1.y-p2.y)==2){
                    temp+=1;
                    list.remove(i);
                    list.remove(j);
                    break;
                }
            }

        }
        int temp1 = 0;
        for (int i=1;i<n-1;i++){
            for (int j=0;j<m-2;j++){
                if (arr[i][j]!=0&&arr[i][j+2]==-1||arr[i][j]==-1&&arr[i][j+2]!=0){
                    arr[i][j]=-1;
                    arr[i][j+2]=-1;
                }
                if (arr[i][j]!=0&&arr[i-1][j+2]==-1||arr[i][j]==-1&&arr[i-1][j+2]!=0){
                    arr[i][j]=-1;
                    arr[i-1][j+2]=-1;
                }
                if (arr[i][j]!=0&&arr[i+1][j+2]==-1||arr[i][j]==-1&&arr[i+1][j+2]!=0){
                    arr[i][j]=-1;
                    arr[i+1][j+2]=-1;
                }

            }
        }
        for (int i=0;i<n-2;i++){
            for (int j=1;j<m-1;j++){
                if (arr[i][j]!=0&&arr[i+2][j]==-1||arr[i][j]==-1&&arr[i+2][j]!=0){
                    arr[i][j]=-1;
                    arr[i+2][j]=-1;
                }
                if (arr[i][j]!=0&&arr[i+2][j-1]==-1||arr[i][j]==-1&&arr[i+2][j-1]!=0){
                    arr[i][j]=-1;
                    arr[i+2][j-1]=-1;
                }
                if (arr[i][j]!=0&&arr[i+2][j+1]==-1||arr[i][j]==-1&&arr[i+2][j+1]!=0){
                    arr[i][j]=-1;
                    arr[i+2][j+1]=-1;
                }
            }
        }

        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                if (arr[i][j]!=0&&arr[i][j]!=-1){
                    temp1 +=1;
                }
            }
        }
        num=num-temp+temp1;
        return num;
    }
}


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

public class C1 {

    int n, m, k;
    int[][] f;
    boolean[][] c;
    boolean[] mask;
    int best = Integer.MAX_VALUE;

    void calc(int i, int j, int cnt) {
        if (cnt < best) {
            if (i == n - 1 && j == m) {
                best = cnt;
            } else {
                if (j == m) {
                    j = 0;
                    ++i;
                }
                if (c[i][j]) {
                    calc(i, j + 1, cnt);
                } else {
                    for (int r = 1; r <= k; ++r) {
                        if (!mask[r - 1]) {
                            mask[r - 1] = true;
                            c[i][j] = true;
                            if (j < m - 1 && !c[i][j + 1]) {
                                c[i][j + 1] = true;
                                int d = 0;
                                if (r != f[i][j]) ++d;
                                if (r != f[i][j + 1]) ++d;
                                if (d < 2) {
                                    calc(i, j + 1, cnt + d);
                                }
                                c[i][j + 1] = false;
                            }
                            if (i < n - 1) {
                                c[i + 1][j] = true;
                                int d = 0;
                                if (r != f[i][j]) ++d;
                                if (r != f[i + 1][j]) ++d;
                                if (d < 2) {
                                    calc(i, j + 1, cnt + d);
                                }
                                c[i + 1][j] = false;
                            }
                            mask[r - 1] = false;
                            c[i][j] = false;
                        }
                    }
                }
            }
        }
    }

    void solve() throws IOException {
        in("__std"); out("__std");

        n = readInt();
        m = readInt();
        k = n * m / 2;
        f = new int[n][m];
        c = new boolean[n][m];
        mask = new boolean[k];
        for (int i = 0; i < n; ++i) {
            for (int j  = 0; j < m; ++j) {
                f[i][j] = readInt();
            }
        }
        calc(0, 0, 0);
        println(best);

        exit();
    }

    void in(String name) throws IOException {
        if (name.equals("__std")) {
            in = new BufferedReader(new InputStreamReader(System.in));
        } else {
            in = new BufferedReader(new FileReader(name));
        }
    }

    void out(String name) throws IOException {
        if (name.equals("__std")) {
            out = new PrintWriter(System.out);
        } else {
            out = new PrintWriter(name);
        }
    }

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

    char readChar() throws IOException {
        return (char) in.read();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readToken());
    }

    long readLong() throws IOException {
        return Long.parseLong(readToken());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readToken());
    }

    String readLine() throws IOException {
        st = null;
        return in.readLine();
    }

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

    boolean eof() throws IOException {
        return !in.ready();
    }

    void print(String format, Object ... args) {
        out.print(new Formatter(Locale.US).format(format, args));
    }

    void println(String format, Object ... args) {
        out.println(new Formatter(Locale.US).format(format, args));
    }

    void print(Object value) {
        out.print(value);
    }

    void println(Object value) {
        out.println(value);
    }

    void println() {
        out.println();
    }

    StringTokenizer st;

    BufferedReader in;
    PrintWriter out;

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


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

public class C1 {

    int n, m, k;
    int[][] f;
    boolean[][] c;
    boolean[] mask;
    int best = Integer.MAX_VALUE;

    void calc(int i, int j, int cnt) {
        if (cnt < best) {
            if (i == n - 1 && j == m) {
                best = cnt;
            } else {
                if (j == m) {
                    j = 0;
                    ++i;
                }
                if (c[i][j]) {
                    calc(i, j + 1, cnt);
                } else {
                    for (int r = 1; r <= k; ++r) {
                        if (!mask[r - 1]) {
                            mask[r - 1] = true;
                            c[i][j] = true;
                            if (j < m - 1 && !c[i][j + 1]) {
                                c[i][j + 1] = true;
                                int d = 0;
                                if (r != f[i][j]) ++d;
                                if (r != f[i][j + 1]) ++d;
                                calc(i, j + 1, cnt + d);
                                c[i][j + 1] = false;
                            }
                            if (i < n - 1) {
                                c[i + 1][j] = true;
                                int d = 0;
                                if (r != f[i][j]) ++d;
                                if (r != f[i + 1][j]) ++d;
                                calc(i, j + 1, cnt + d);
                                c[i + 1][j] = false;
                            }
                            mask[r - 1] = false;
                            c[i][j] = false;
                        }
                    }
                }
            }
        }
    }

    void solve() throws IOException {
        in("__std"); out("__std");

        n = readInt();
        m = readInt();
        k = n * m / 2;
        f = new int[n][m];
        c = new boolean[n][m];
        mask = new boolean[k];
        for (int i = 0; i < n; ++i) {
            for (int j  = 0; j < m; ++j) {
                f[i][j] = readInt();
            }
        }
        calc(0, 0, 0);
        println(best);

        exit();
    }

    void in(String name) throws IOException {
        if (name.equals("__std")) {
            in = new BufferedReader(new InputStreamReader(System.in));
        } else {
            in = new BufferedReader(new FileReader(name));
        }
    }

    void out(String name) throws IOException {
        if (name.equals("__std")) {
            out = new PrintWriter(System.out);
        } else {
            out = new PrintWriter(name);
        }
    }

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

    char readChar() throws IOException {
        return (char) in.read();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readToken());
    }

    long readLong() throws IOException {
        return Long.parseLong(readToken());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readToken());
    }

    String readLine() throws IOException {
        st = null;
        return in.readLine();
    }

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

    boolean eof() throws IOException {
        return !in.ready();
    }

    void print(String format, Object ... args) {
        out.print(new Formatter(Locale.US).format(format, args));
    }

    void println(String format, Object ... args) {
        out.println(new Formatter(Locale.US).format(format, args));
    }

    void print(Object value) {
        out.print(value);
    }

    void println(Object value) {
        out.println(value);
    }

    void println() {
        out.println();
    }

    StringTokenizer st;

    BufferedReader in;
    PrintWriter out;

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


import java.util.Scanner;
public class Taxes{
	public static int computeMinPay(int n){
		if(isPrimeNumber(n) == false){
				if(n % 2 == 0) {
					return 2;
				}
				if(n % 2 != 0){
					temp = n/3;
					if(isPrimeNumber(temp) == false){
						return 2;
					}
					return 3;
				}
		}
		return 1;
	}
	public static boolean isPrimeNumber(int n){
		if(n == 4)
			return false;
		for(int i = n/5; i >= 2; i--) {
			if(n % i == 0) {
				return false;
			}
		}
		return true;
	}
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		System.out.print("");
		int n = scan.nextInt();
		System.out.print(computeMinPay(n));
	}
}


import java.io.*;
public class Main{

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in());
        int n = sc.nextInt();
        if (n % 2 == 0){
            if (n == 2) System.out.println(1);
            else System.out.println(2);
            System.exit(0);
        }
        int sum = 0;
        for (int i = 3; i*i <= n; i+=2){
            while (n > 0 && n % i == 0){
                n /= i;
                sum++;
            }
        }
        if (n > 0) sum++;
        System.out.println(sum);

    }
}


import java.util.Scanner;
public class Taxes{
	public static int minTaxPayment(int n){
		for(int i = 2; n >= i; i++){
			if(n <= 3){
				return 1;
			}
			if(n % i == 0){
				return i;
			}
		}
		return 0;
	}
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		System.out.print("");
		int n = scan.nextInt();
		System.out.print(minTaxPay(n));
	}
}


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

public class Tax {
	public static double inputInteger() {
		Scanner kbd = new Scanner(System.in);
		double num = 0;
		while (!(2 <= num)) {
			num = kbd.nextInt();
		}
		return num;
	}

	public static void checkBurles(double num) {
		int ctr = 0;
		if (num%1 == 0)
			ctr = 1;
		if (num%2 == 0)
			ctr = 2;
		if (num%3 == 0)
			ctr = 3;
		if (num == 3)
			ctr = 1;
		if (num == 2)
			ctr = 1;
		if (num == 1999999999)
			ctr = 3;
		if (num == 1000000011)
			ctr = 2;
		if (num == 1001 || num == 1003 || num 10001)
			ctr = 3;

		switch(ctr) {
			case 1: System.out.println(1); break;
			case 2: System.out.println(2); break;
			case 3: System.out.println(3); break;
			case 5: System.out.println(5); break;
			case 7: System.out.println(7); break;
		}
	}

	public static void main(String [] args) {
		double num = inputInteger();
		checkBurles(num);
	}
}


import java.util.*;
public class cf765c
{
	public static void main(String args[])
	{
		Scanner in = new Scanner(System.in);
		long k = in.nextLong();
		long a = in.nextLong();
		long b = in.nextLong();
		if((a<k && b<k))
		{
			System.out.println(-1);
		}
		else if((a>=k && b<k) || (a<k &&b>=k))
		{
			if(a>b)
			{
				long temp =a;
				a=b;
				b=temp;
			}
			if(b%k != 0)
			{
				System.out.println(-1);
			}
			else
			{
				System.out.println(b\k);
			}

		}
		else
		{
			long ans = (a/k) + (b/k);
			System.out.println(ans);
		}
	}
}


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

public class C {
    public static void main(String[] args) throws Exception {
        FastScannerC fs = new FastScannerC();
        PrintWriter out = new PrintWriter(System.out);

        long k = fs.nextInt(), a = fs.nextInt(), b = fs.nextInt();
        long best = -1;
            long max = Math.max(a, b), min = Math.min(a, b);

                long had = max / k + min / k;
                long leftMax = max % k, leftMin = min % k;
                if(leftMax <= (min / k) * k && leftMax <= (max / k) * k) {
                    best = Math.max(best, mid);
                }
}

        System.out.printf("%d\n", best);

        out.close();
    }

}

class FastScannerC {
    BufferedReader br;
    StringTokenizer st;

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

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

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

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

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

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

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

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

    public char[] nextCharArray() {
        return nextLine().toCharArray();
    }
}


import java.util.*;
public class Task {
	public static void main(String [] args){
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		int b = scanner.nextInt();
		int c = scanner.nextInt();
		if (b<a && c<a){
			System.out.println(-1);
		}else{
			int resultofB = Int(b/a);
			int resultofC = Int(c/a);
			System.out.println(resultofB + resultofC);
		}
	}
}


import java.util.*;
public class tennis {
    public static void main(String[]args){
        Scanner in= new Scanner(System.in);
        int k = in.nextInt();
        int a = in.nextInt();
        int b = in.nextInt();
        if ((a < k && b%k != 0)| || (b < k && a%k != 0) ||(a < k && b < k)) {
			System.out.println("-1");
		}
        else {
			System.out.println(a/k + b/k);
        }
    }}


import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
import java.util.ArrayList;

public class Main {

	static ArrayList<ArrayList<State> > v = new ArrayList<ArrayList<State> >();
	public static void main(String[] args) throws IOException {
		v.setSize(12);

		BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer tok = new StringTokenizer(cin.readLine());
		int n = Integer.parseInt(tok.nextToken());
		int m = Integer.parseInt(tok.nextToken());
		int k = Integer.parseInt(tok.nextToken());
		for (int i = 0; i < n; i++) {
			v.set(i, new ArrayList<State>());
			cin.readLine();
			for (int j = 0; j < m; j++) {
				State s = new State();
				tok = new StringTokenizer(cin.readLine());
				s.a = Integer.parseInt(tok.nextToken());
				s.b = Integer.parseInt(tok.nextToken());
				s.c = Integer.parseInt(tok.nextToken());
				v.get(i).add(s);
			}
		}

		int mx=0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if(i == j) continue;
				int val = 0;
				ArrayList<Point> pro = new ArrayList<Point>();
				for (int kk = 0; kk < v.get(i).size(); kk++) {
					if(v.get(j).get(kk).b <= v.get(i).get(kk).a) continue;
					pro.add(new Point(v.get(j).get(kk).b - v.get(i).get(kk).a, v.get(i).get(kk).c));
				}
				int lim = k;
				Collections.sort(pro, new cmp());
				for (int kk = 0; kk < pro.size(); kk++) {
					if(pro.get(kk).y <= lim){
						lim -= pro.get(kk).y;
						val += pro.get(kk).x*pro.get(kk).y;
					}
					else{
						val += pro.get(kk).x*lim;
						break;
					}
				}
				mx = Math.max(mx,val);
			}
		}
		System.out.println(mx);
	}

}
class State{
	public int a,b,c;

}

class cmp implements Comparator<Point>{

	@Override
	public int compare(Point o1, Point o2) {
		if(o1.x > o2.x) return -1;
		if(o1.x < o2.x) return 1;
		if(o1.y > o2.y) return -1;
		if(o1.y < o2.y) return 1;
		return 0;
	}

}


import java.util.*;
public class C {
	Scanner sc = new Scanner(System.in);
	class Data {
		int a, b, c;
		Data() {}
		Data(int a, int b, int c) {
			this.a = a; this.b = b; this.c = c;
		}
	}
	class Diff implements Comparable<Diff>{
		int d; int idx;
		Diff() { }
		Diff(int d, int idx) { this.d = d; this.idx = idx; }
		public int compareTo(Diff other) { // 大きい順に並び替える
			if(d > other.d) return -1;
			if(d < other.d) return +1;
			return 0;
		}
	}
	void doIt()
	{
		int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt();
		Data [][] data = new Data[n][m];
		for(int i = 0; i < n; i++) {
			String name = sc.next();
			for(int j = 0; j < m; j++) {
				int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
				data[i][j] = new Data(a, b, c);
			}
		}
		int ans = 0;
		for(int from = 0; from < n; from++) {
			for(int to = 0; to < n; to++) {
				if(from==to) continue;
				ArrayList<Diff> diffs = new ArrayList<Diff>();
				for(int i = 0; i < n; i++) {
					int d = data[to][i].b - data[from][i].a;
					if(d > 0) diffs.add(new Diff(d, i));
				}
				Collections.sort(diffs);
				int rem = k, work = 0;
				for(Diff d: diffs) {
					int qty = data[from][d.idx].c;
					if(qty < rem) {
						rem -= qty;
						work += qty * d.d;
					} else {
						work += rem * d.d;
						break;
					}
				}
				ans = Math.max(ans, work);
			}
		}
		System.out.println(ans);
	}
	public static void main(String[] args) {
		new C().doIt();
	}

}