import java.util.Scanner;

public class C {
	Scanner sc = new Scanner(System.in);

	void run() {
		int hpY = sc.nextInt();
		int atY = sc.nextInt();
		int deY = sc.nextInt();
		int hpM = sc.nextInt();
		int atM = sc.nextInt();
		int deM = sc.nextInt();
		int h = sc.nextInt();
		int a = sc.nextInt();
		int d = sc.nextInt();

		int cost = Integer.MAX_VALUE;

		for (int i = 0; i <= 700; i++) {
			for (int j = 0; j <= 700; j++) {
				for (int k = 0; k <= 700; k++) {
					if (i * h + j * a + k * d >= cost) continue;
					int tmpHPY = hpY + i;
					int tmpATY = atY + j;
					int tmpDEY = deY + k;
					int tmpHPM = hpM;
					int pre = -1;
					while (true) {
						tmpHPY -= Math.max(0, atM - tmpDEY);
						tmpHPM -= Math.max(0, tmpATY - deM);
						if (tmpHPY > 0 && tmpHPM <= 0) {
							cost = Math.min(cost, i * h + j * a + k * d);
							break;
						}
						if (tmpHPY <= 0 || tmpHPM <= 0) break;
						if (pre == tmpHPM) break;
						pre = tmpHPM;
					}
				}
			}
		}
		System.out.println(cost);
	}

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


import java.util.*;
public class Main {
  public  static void main (String [] args) {
    Scanner sc = new Scanner (System.in);
    long  a = sc.nextLong();
    if(a %2 == 0) System.out.println((a+1)/2);
    else System.out.println((a+1);
  }
}


import java.util.*;

public class virtualContest3 {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		int x1=scn.nextInt();
		int y1=scn.nextInt();
		int x2=scn.nextInt();
		int y2=scn.nextInt();
		int x=scn.nextInt();
		int y=scn.nextInt();
		if(Math.abs(x2-x1)%x==0 && Math.abs(y2-y1)%y==0) {
			int d1=Math.abs(x2-x1);
			int d2=Math.abs(y2-y1);
			if((d2%2==0 && d1%2==0)|| (d2%2!=0 && d1%2!=0)) {
				System.out.println("YES");
			}else
				System.out.println("NO");
		}else
			System.out.println("NO");
	}

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

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

    static class ATreasureHunt {
        int dx = new int[]{1, 1, -1, -1};
        int dy = new int[]{1, -1, 1, -1};

        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            int x1 = in.scanInt();
            int y1 = in.scanInt();

            int x2 = in.scanInt();
            int y2 = in.scanInt();

            int px = in.scanInt();
            int py = in.scanInt();

            boolean f = false;
            for (int i = 0; i < 4; i++) {
                int a = (px - x1) / dx[i];
                int b = (py - y1) / dy[i];

                for (int j = 0; j < 4; j++) {
                    int tx = px + a * dx[j];
                    int ty = py + b * dy[j];
                    if (tx == x2 && ty == y2) f = true;
                }
            }
            if (f) {
                out.println("YES");
            } else {
                out.println("NO");
            }

        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int INDEX;
        private BufferedInputStream in;
        private int TOTAL;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (INDEX >= TOTAL) {
                INDEX = 0;
                try {
                    TOTAL = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (TOTAL <= 0) return -1;
            }
            return buf[INDEX++];
        }

        public int scanInt() {
            int I = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    I *= 10;
                    I += n - '0';
                    n = scan();
                }
            }
            return neg * I;
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

    }
}


import java.util.Arrays;
import java.util.Scanner;
import java.util.*;

public class cr191d2a
{
    public static int max = 0;
    public static int N;

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

        int[] mem = mas.clone();

        Stack<SuperMas> stack = new Stack<SuperMas>();

        SuperMas x = new SuperMas(0, N - 1, mas);
        stack.push(x);

        while (!stack.isEmpty())
        {
            x = stack.pop();

            //System.out.print("1) ");
            //x.Print();

            if (x.begin > x.end)
                continue;

            SuperMas left = new SuperMas(x.begin + 1, x.end, mem.clone());
            SuperMas right = new SuperMas(x.begin, x.end - 1, mem.clone());
            x.Change();

            //System.out.print("2) ");
            //x.Print();

            if (x.CountOne() > SuperMas.max)
            {
                SuperMas.max = x.CountOne();
                //System.out.println("MAX = " + SuperMas.max);
                if (SuperMas.max == N)
                {
                    System.out.println(N);
                    return;
                }
            }

            stack.push(left);
            stack.push(right);
        }

        System.out.println(SuperMas.max);
    }
}

class SuperMas extends cr191d2a
{
    int[] mas;
    int begin, end;

    SuperMas(int begin, int end, int[] mas)
    {
        this.begin = begin;
        this.end = end;
        this.mas = mas;
    }

    int CountOne()
    {
        int count = 0;

        for (int i = 0; i < N; i++)
            if (this.mas[i] == 1)
                count++;

        return count;
    }

    void Change()
    {
        for (int i = this.begin; i < this.end + 1; i++)
            this.mas[i] = 1 - this.mas[i];
    }

    void Print()
    {
        for (int i = 0; i < N; i++)
            System.out.print(this.mas[i]);

        System.out.print(" = " + this.CountOne() + " " + this.begin + "//" + this.end);
        System.out.println();
    }
}


import java.util.Scanner ;
public class A_391
{
    public static void main(String [] args)
    {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String tmp = "" ;
        int c = 0 ;
        for(int i = 0; i < s.length()-1; i++)
        {
            if(s.charAt(i) == s.charAt(i+1))
                tmp += s.charAt(i);
            else
            {
                if(tmp.length() % 2 == 1)
                    c++ ;
                tmp = "" ;
            }
        }
        System.out.println(c);
    }
}


public class MAIN {

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

        int res = 0;
        if (s!=null && s.length()>1) {
            for (int i=0; i<s.length(); ) {
                int j = i+1;
                while (j<s.length() && s.charAt(j)==s.charAt(j-1)) {
                    j++;
                }
                if ((j-i)%2==0) res++;
                i = j;
            }
        }

        System.out.println(res);
    }

}


import java.util.Scanner;

// 391A
public class GeneticEngineering {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        String str = sc.nextLine();

        int i = 0, j = 1;
        int n = str.length();

        int count = 1, res = 0;

        while(i < j)
        {
            if(j < n && str.charAt(i) == str.charAt(j))
            {
                count++;
                j++;
            }
            else
            {
                if ( count % 2 == 0)
                    res++;
                count = 1;
                i = j;
                j++;
            }
        }

        System.out.print(res);
    }

}


import java.util.*;
public class Solution
{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String s = sc.nextLine();
        int l=s.length();
        int c=0,z=1;;
        for(int i=0;i<(l-1);)
        {
            if(s.charAt(i)==s.charAt(i+1))
            {   for(int j=i;;)
                {

                    if(s.charAt(j)==s.charAt(j+1))
                    z++;
                    else break;
                }
                if(z%2==0)
                c=c+z>>1;
                i=i+z;
            }
            else
            i++;
        }
        System.out.println(c);
        sc.close();
    }
}


//package codeforces;
import java.util.*;
public class GeneticEngineering {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        String s=in.nextLine();
        int count=1;
        int insert=0;
        for(int i=0;i<s.length()-1;i++)
        {
            if(s.charAt(i)==s.charAt(i+1))
            {
                count++;
            }
            else
            {
                if(count%2==0)
                {
                    insert++;
                }
                count=1;
            }
        }
        System.out.println(insert);
    }
}


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

public class AMain {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try{
            BufferedReader br =
                          new BufferedReader(new InputStreamReader(System.in));

            String input;
            input=br.readLine();
            char[] s = input.toCharArray();

            int curSeq = 1;
            char curChar = s[0];
            int result = 0;
            for(int i=1; i<s.length; i++){
                if (s[i] != curChar){
                    if (curSeq%2 == 0){
                        result++;
                    }
                    curChar = s[i];
                    curSeq = 1;
                } else {
                    curSeq++;
                }
            }
            System.out.println(result);

        }catch(IOException io){
            io.printStackTrace();
        }
    }

}


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

public class main {
    public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static PrintWriter pw = new PrintWriter(System.out);
    public static String line;
    public static StringTokenizer st;
    public static ArrayList<ArrayList<Integer>> adjList;
    public static int[] dx = {-1, 0, 1, 0, -1, 1, 1, -1};
    public static int[] dy = {0, 1, 0, -1, 1, 1, -1, -1};
    public static int INF = 0x3f3f3f3f;
    public static int MOD = 1000000007;

    public static void main(String[] args) throws Exception {
        int N = Integer.parseInt(br.readLine());
        st = new StringTokenizer(br.readLine());
        ArrayList<Integer> A = new ArrayList<Integer>();

        for(int i = 0; i < N; i++){
            A.add(Integer.parseInt(st.nextToken()));
        }

        boolean f = true;
        boolean sorted = false;
        while (!sorted && f) {
            for(int i = 0; i < N; i++) {
                if (i != 0 && A.get(i) > A.get(i-1)) {
                    sorted = true;
                }

                if (i != 0 && Math.abs(A.get(i) - A.get(i-1)) >= 2) {
                    f = false;
                    break;
                }
            }
            Integer k = Collections.max(A);
            A.remove(k);
            A.add(k);
        }

        pw.println(f ? "YES" : "NO");

        pw.close();
        br.close();
    }
}

class Pair implements Comparable<Pair>{
    public int x, y;

    Pair(int _x, int _y){
        x = _x;
        y = _y;
    }

    public String toString(){
        return x + "," + y;
    }

    public int compareTo(Pair p){
        if(x != p.x) return x < p.x ? 1 : -1;
        else if(y != p.y) return y < p.y ? -1 : 1;
        return 0;
    }
}


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

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

    static class TaskC {

        void Solve(InputReader in, PrintWriter out) {
            int n = in.NextInt();
            ArrayList<Integer> a = new ArrayList<>();
            for (int i = 0; i < n; i++) a.add(in.NextInt());
            for (int j = 0; j < n; j++) {
                for (int i = 0; i < n - 1; i++) {
                    if (Math.abs(a.get(i) - a.get(i + 1)) == 2) {
                        out.println("NO");
                        return;
                    }
                }
                int max = 0;
                int ind = 0;
                for (int i = 0; i < n; i++)
                    if (a.get(i) > max) {
                        ind = i;
                        max = a.get(i);
                    }
                a.remove(ind);
                n--;
            }
            out.println("YES");
        }
    }

    static int GetMax(int[] ar) {
        int max = Integer.MIN_VALUE;
        for (int a : ar) {
            max = Math.max(max, a);
        }
        return max;
    }

    static int GetMin(int[] ar) {
        int min = Integer.MAX_VALUE;
        for (int a : ar) {
            min = Math.min(min, a);
        }
        return min;
    }

    static long GetSum(int[] ar) {
        long s = 0;
        for (int a : ar) s += a;
        return s;
    }

    static int[] GetCount(int[] ar) {
        return GetCount(ar, GetMax(ar));
    }

    static int[] GetCount(int[] ar, int maxValue) {
        int[] dp = new int[maxValue + 1];
        for (int a : ar) {
            dp[a]++;
        }
        return dp;
    }

    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

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

        String Next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine(), " \t\n\r\f,");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        int NextInt() {
            return Integer.parseInt(Next());
        }

        long NextLong() {
            return Long.parseLong(Next());
        }

        double NextDouble() {
            return Double.parseDouble(Next());
        }

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

        int[] NextIntArray(int n, int offset) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = NextInt() - offset;
            }
            return a;
        }

        int[][] NextIntMatrix(int n, int m) {
            return NextIntMatrix(n, m, 0);
        }

        int[][] NextIntMatrix(int n, int m, int offset) {
            int[][] a = new int[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    a[i][j] = NextInt() - offset;
                }
            }
            return a;
        }

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

        long[] NextLongArray(int n, int offset) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = NextLong() - offset;
            }
            return a;
        }

        long[][] NextLongMatrix(int n, int m) {
            return NextLongMatrix(n, m, 0);
        }

        long[][] NextLongMatrix(int n, int m, int offset) {
            long[][] a = new long[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    a[i][j] = NextLong() - offset;
                }
            }
            return a;
        }
    }
}


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

public class CForces3 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int staticArr[] = new int[101];

        for (int i=0;i<101;i++) {
            staticArr[i]=0;
        }
        int n = in.nextInt();
        int input[] = new int[n];
        int copy[] = new int[n];
        for (int i=0;i<n;i++) {
            input[i] = in.nextInt();
            staticArr[input[i]]=1;
            //copy[i] = input[i];
        }
        boolean flag=true;
        //Sort the array and find the difference between the elements.

        for (int i=0;i<n-1;i++) {
            //Difference between adjacent elements in less than 2
            if (Math.abs(input[i] - input[i+1])<2) {

            } else {
                //Check if +-2 there are any elements present in the array
                //check for i first and then i+1
                if (input[i]>input[i+1]) {

                    if ((checkFor(input[i] - 1, 101) && staticArr[input[i] - 1] == 1) ||
                            (checkFor(input[i + 1] + 1, 101) && staticArr[input[i + 1] + 1] == 1)) {
                        flag = false;
                        break;
                    }
                } else {
                    if ((checkFor(input[i+1] - 1, 101) && staticArr[input[i+1] - 1] == 1) ||
                            (checkFor(input[i] + 1, 101) && staticArr[input[i] + 1] == 1)) {
                        flag = false;
                        break;
                    }

                }

            }
        }

        if (flag) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }

        /*
        Arrays.sort(copy);

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

        }

        */
    }

     static boolean checkFor(int a,int n) {
        if (a>=0 && a<n) {
            return true;
        }

        return false;
     }
}


import java.util.Scanner;

public class quirky {
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int a=sc.nextInt();
		if(a==1)
			System.out.println("YES");
		int[] arr = new int[a];
		arr[0]=sc.nextInt();
		boolean flag=true;
		for(int i=1;i<a;i++)
		{
			arr[i]=sc.nextInt();
			if(arr[i]-arr[i-1]>=2)
				flag=false
		}
		if(flag)
			System.out.println("YES");
		else
			System.out.println("NO");

	}

}


import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.PrintStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.io.Writer;
import java.util.Collection;
import java.util.List;
import java.util.BitSet;
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.TreeSet;
import java.math.BigInteger;
import java.util.Collections;
import java.io.InputStream;

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

class TaskD {
    int MOD = 777777777;
    long[][] C = MathUtils.generateCombination(257, MOD);
    public void solve(int testNumber, MyScanner in, MyPrintWriter out) {
        int n = in.nextInt(), k = in.nextInt();
        long[][] dp = new long[k+1][n-k+1];
        dp[0][0] = 1;
        for(int i=1;i<=n;i++){
            long[][] nxt = new long[k+1][n-k+1];
            for(int lie=0;lie<k+1;lie++)for(int hon=0;hon<n-k+1;hon++)if(dp[lie][hon]>0){
                int rest = n - lie - hon;
                for(int use=0;use<=rest;use++){
                    if(use==i && hon + use < n-k+1){
                        nxt[lie][hon+use] = (nxt[lie][hon+use] + dp[lie][hon] * C[rest][use]) % MOD;
                    }else if(use!=i && lie+use<k+1){
                        nxt[lie+use][hon] = (nxt[lie+use][hon] + dp[lie][hon] * C[rest][use]) % MOD;
                    }
                }
            }
            dp = nxt;
        }
        out.println(dp[k][n-k]);
    }
}

class MathUtils {

    public static long[][] generateCombination(int n, int modulus) {
        long[][] C = new long[n][n];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < i + 1; j++) {
                C[i][j] = j == 0 ? 1 : C[i - 1][j - 1] + C[i - 1][j];
                if (C[i][j] >= modulus) C[i][j] -= modulus;
            }
        return C;
    }

    }

class MyScanner {
    private final InputStream in;

    public MyScanner(InputStream in) {
        this.in = in;
    }

    int bufLen;
    int bufPtr;
    byte[] buf = new byte[1024];

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

    public int nextInt() {
        try {
            int c = read();
            if (c == -1) return c;
            while (c != '-' && (c < '0' || '9' < c)) {
                c = read();
                if (c == -1) return c;
            }
            if (c == '-') return -nextInt();
            int res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while ('0' <= c && c <= '9');
            return res;
        } catch (Exception e) {
            return -1;
        }
    }

    }

class MyPrintWriter {
    PrintWriter out;

    public MyPrintWriter(OutputStream outputStream) {
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public MyPrintWriter(Writer writer) {
        out = new PrintWriter(writer);
    }

    public void println(Object... os) {
        for (int i = 0; i < os.length - 1; i++) {
            out.print(os[i]);
            out.print(' ');
        }
        out.println(os[os.length - 1]);
    }

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

    }


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

public class D {

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	static final int MOD = 777777777;

	int[][] c = new int[257][];
	{
		for (int i = 0; i < c.length; i++) {
			c[i] = new int[i + 1];
			c[i][0] = c[i][i] = 1;
			for (int j = 1; j < i; j++) {
				c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
				if (c[i][j] >= MOD)
					c[i][j] -= MOD;
			}
		}
//		System.err.println(Arrays.toString(c[4]));
	}

	int solve(int n, int kn) {

		int[] dp = new int[n + 1];
		dp[0] = 1;

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

			int[] next = new int[n + 1];

			// who said i?
			if (i == kn) {
				for (int k = 0; k + i <= n; k++) {
					if (dp[k] != 0) {
						next[k + i] += (int) ((long) dp[k] * c[n - k][i] % MOD);
						if (next[k + i] >= MOD)
							next[k + i] -= MOD;
					}
				}
			} else {
				// liars
				for (int j = 0; j <= n; j++)
					// how many people said i
					if (j != i) {
						for (int k = 0; k + j <= n; k++)
							// how many people already said
							if (dp[k] != 0) {
								next[k + j] += (int) ((long) dp[k]
										* c[n - k][j] % MOD);
								if (next[k + j] >= MOD)
									next[k + j] -= MOD;
							}
					}
			}
			dp = next;
		}

		return dp[n];
	}

	D() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		int n = nextInt();
		int k = nextInt();
		out.println(solve(n, n - k));
		out.close();
	}

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

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

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

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

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


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

public class TheWall
{
    public static void main(String[] args) throws IOException
    {
        //BufferedReader input = new BufferedReader(new FileReader("Sample Input.txt"));
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder output = new StringBuilder();

        String line;
        String[] items;
        char[] bricks;
        int x, y, a, b, count;

        while ( (line=input.readLine()) != null)
        {
            items = line.split("[\\s]+");
            x = Integer.parseInt(items[0]);
            y = Integer.parseInt(items[1]);
            a = Integer.parseInt(items[2]);
            b = Integer.parseInt(items[3]);

            bricks = new char[b+1];
            Arrays.fill(bricks, ' ');

            count =0;

            for (int i = a; i <= b; i++)
            {
                if ( i%x == 0 && i%y ==0 )
                    count++;
            }

            output.append(count).append("\n");

        }
        System.out.print(output);

    }

}


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

public class D {
	int MODULAR = 1000000007;
	int M;
	long[] numcount = new long[11];
	long[] cumnumcount = new long[11];
	public void solve(){
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		Scanner sc = new Scanner(System.in);
		M = sc.nextInt();

		for(int i = 1; i <= 9; i++){
			if( i == 4 || i == 7) bfs(i, 1);
			else bfs(i, 0);
		}

		for(int i = 0; i <= 10; i++){
			if( i > 0)
				cumnumcount[i] = cumnumcount[i-1] + numcount[i];
			else
				cumnumcount[i] = numcount[i];
//			System.out.println(i + ": " + numcount[i] + ", " + cumnumcount[i]);
		}

		long ans = 0;
		for(int i = 0; i < 10; i++){
			if( numcount[i+1] == 0) break;
			long n = cumnumcount[i] % MODULAR;
			long tmp = numcount[i+1] % MODULAR;
			for(int j = 0; j < 6; j++){
				tmp *= ( (n-j) % MODULAR);
				tmp = tmp % MODULAR;
			}
			tmp = tmp % MODULAR;
			ans += tmp;
			ans = ans % MODULAR;
		}
		ans = ans % 1000000007;

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

	public void bfs(long num, int count){
		if( num > M) return;
		numcount[count]++;
		for(int i = 0; i <= 9; i++){
			if( num*10 > M) return;
			if( i == 4 || i == 7) bfs( num*10+i, count+1);
			else bfs( num*10+i, count);
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new D().solve();
	}

}


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

public class D {
	int MODULAR = 1000000007;
	int M;
	long[] numcount = new long[10];
	public void solve(){
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		Scanner sc = new Scanner(System.in);
		M = sc.nextInt();

		int[] ct = count(M);

		for(int i = 1; i <= Math.min(M, 9); i++){
			if( i == 4 || i == 7) bfs(i, 1);
			else bfs(i, 0);
		}

//		for(int i = 0; i <= 9; i++){
//			System.out.println(i + ": " + numcount[i]);
//		}
//
//		for(int i = 0; i <= 9; i++){
//			System.out.println(i + ": " + ct[i] );
//		}

		long ans = 0;
		for(int i = 1; i <= 9; i++){
			for(int x1 = 0; x1 < i; x1++){
				for(int x2 = 0; x1+x2 < i; x2++){
					for(int x3 = 0; x1+x2+x3 < i; x3++){
						for(int x4 = 0; x1+x2+x3+x4 < i; x4++){
							for(int x5 = 0; x1+x2+x3+x4+x5 < i; x5++){
								for(int x6 = 0; x1+x2+x3+x4+x5+x6 < i; x6++){
//									System.out.println("-->" + i + ", " + x1 + ", " + x2 + ", " + x3 + ", " + x4 + ", " + x5 + ", " + x6);
									int[] a = new int[10];
									a[x1]++;
									a[x2]++;
									a[x3]++;
									a[x4]++;
									a[x5]++;
									a[x6]++;
									long tmp = numcount[i];
									for(int j = 0; j <= 9; j++){
										for(int k = 0; k < a[j]; k++){
											tmp = tmp * (numcount[j]-k) % MODULAR;
										}
									}
									ans += tmp;
								}
							}
						}
					}
				}
			}
		}
		ans = ans % MODULAR;
		System.out.println( ans );
	}

	public void bfs(long num, int count){
		numcount[count]++;
		for(int i = 0; i <= 9; i++){
			if( num*10+i > M) break;
			if( i == 4 || i == 7) bfs( num*10+i, count+1);
			else bfs( num*10+i, count);
		}
	}

	int[] count(long m)
	{
		int[][] dp = new int[11][11];
		dp[0][0] = 1;

		int e = 0;
		long d = 1;
		for(;d <= m;d *= 10,e++){
			for(int i = 10;i >= 0;i--){
				dp[e+1][i] = dp[e][i]*8+(i-1 >= 0 ? dp[e][i-1]*2 : 0);
			}
		}

		int[] ret = new int[11];
		int edge = 0;
		for(;e >= 0;d /= 10,e--){
			int q = (int)(m/d%10);
			for(int i = 0;i < q;i++){
				if(i == 4 || i == 7){
					for(int k = 0;k <= 10 && edge+1+k <= 10;k++){
						ret[edge+1+k] += dp[e][k];
					}
				}else{
					for(int k = 0;k <= 10 && edge+k <= 10;k++){
						ret[edge+k] += dp[e][k];
					}
				}
			}
			if(q == 4 || q == 7)edge++;
		}
		ret[edge]++;
		ret[0]--;

		return ret;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new D().solve();
	}

}


import java.util.*;
public class d {
    public static int m, length;
    public static long[] digits;
    public static int mod = 1000000007;
    public static long[][][] memo;
    public static long[] counts;
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    m = input.nextInt();
    length = (m+"").length();
    digits = new long[length];
    for(int i =0; i<length; i++)
        digits[i] = (m+"").charAt(i) - '0';
    //System.out.println(go(0, false, 0));
    memo = new long[11][2][11];
    for(int i = 0; i<memo.length; i++)
        for(int j = 0; j<memo[0].length; j++)
            Arrays.fill(memo[i][j], -1);
    counts = new long[10];
    for(int i = 0; i<10; i++)
    {
        counts[i] = go(0, false, i);
        if(i == 0) counts[i]--;
    }
    //for(long i: counts) System.out.println(i);
    long res = 0;
    for(int i =0; i<10; i++)
    {
        if(counts[i] == 0) continue;
        counts[i]--;
        for(int j = 0; j<i; j++)
        {
            //System.out.println(i + " " + j);
            res += (counts[i]+1)*go2(0, j, 0);
            res%= mod;
        }
        counts[i]++;
    }
    res %= mod;
    System.out.println(res);
}
public static long go2(int at, int left, int numOn)
{
    //System.out.println(at + " " + left + " " + numOn);
    if(left<0)
        return 0;
    if(at > 6)
        return 0;
    if(at == 6)
    {
        //if(left == 0) System.out.println("SUCCESS");
        return left == 0 ? 1 : 0;
    }
    if(numOn == 10)
        return 0;
    long res = 0;
    for(int i =0; i<7; i++)
    {
        if(i > counts[numOn])
            break;
        long factor = 1;
        for(int j =0; j<i; j++)
        {
            //System.out.println(factor);
            factor *= counts[numOn] - j;
            factor %= mod;
//          factor *= invmod(j+1, mod);
//          factor %= mod;
        }
        //System.out.println(i + " " + numOn + " " + factor);
        res += factor * go2(at+i, left - i*numOn, numOn+1);
        res %= mod;
    }
    return res%mod;
}
public static long go(int at, boolean less, int left)
{
    if(left < 0)
        return 0;
    if(at == length)
        return left == 0 ? 1:0;
    if(memo[at][less?1:0][left] != -1)
        return memo[at][less?1:0][left];
    if(less)
    {
        int res = 0;
        for(int i = 0; i<10; i++)
        {
            if(i == 4 || i == 7)
                res += go(at+1, less, left-1);
            else
                res += go(at+1, less, left);
            res %= mod;
        }
        return memo[at][less?1:0][left] = res;
    }
    else
    {
        int res = 0;
        for(int i = 0; i<digits[at]; i++)
        {
            if(i == 4 || i == 7)
                res += go(at+1, true, left - 1);
            else
                res += go(at+1, true, left);
            res %= mod;
        }
        if(digits[at] == 4 || digits[at] == 7)
            res += go(at+1, false, left -1);
        else
            res += go(at+1, false, left);
        return memo[at][less?1:0][left] = res %mod;
    }
}
}


import java.util.*;
import java.io.*;
public class D {
	public long permutation(int n, int m, int mod){
		long ret = 1;
		while( m-- > 0 ){
			ret *= n;
			n--;
			ret %= mod;
		}
		return ret;
	}
	public void solve() throws IOException {
		int mod = 1000000007;
		int m = nextInt();
		if( m <= 7 ){
			writer.println(0);
			return;
		}
		int[] num = new int[10];
		num[1] = 1;
		for(int i = 1; ; i++){
			int temp = i;
			int mul = 1;
			int n = 0;
			int c = 0;
			while( temp > 0 ){
				if( temp%2 == 0 ){
					n += 4*mul;
 				} else {
 					n += 7*mul;
 				}
				mul *= 10;
				temp /= 2;
				c++;
			}
			if( n > m ){
				break;
			}
			num[c]++;
		}
		num[0] = m;
		for(int i = 9; i > 0; i--){
			num[0] -= num[i];
		}
		long ans = 0;
		int sum = num[0];
		for(int i = 1; i < 10; i++){
			long perm = permutation(sum, 6, mod);
			perm *= num[i];
			ans += perm;
			ans %= mod;
			sum += num[i];
		}
		writer.println(ans%mod);
	}

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

	BufferedReader reader;
	StringTokenizer tokenizer;
	PrintWriter writer;

	public void run() throws IOException {
		try {
			reader = new BufferedReader(new InputStreamReader(System.in));
			tokenizer = null;
			writer = new PrintWriter(System.out);
			solve();
			reader.close();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

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

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

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

}


import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int arr[]=new arr[n];
        for(int i=0;i<n;i++)
        {
            arr[i]=sc.nextInt();
        }
        Arrays.sort(arr);
        boolean check=false;
        for(int i=0;i<n-2;i++)
        {
            if(arr[i+1]-arr[i]==1 && arr[i+2]-arr[i]==2)
            {
                check=true;
                break;
            }
        }
        if(check)
        {
            System.out.println("YES");
        }
        else{
            System.out.println("NO");
        }
    }
}


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

public class A {
    private static int[] nn;
    private static  int n,x,pos,ans=0;
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        PrintWriter writer = new PrintWriter(System.out);
        n=sc.nextInt();
        BigInteger sum=new BigInteger("1");
        nn=new int[n];
        x=sc.nextInt();pos=sc.nextInt();
        for (int i = 1; i < n; i++) {
            sum=sum.multiply(new BigInteger(String.valueOf(i)));
        }
        sum=sum.mod(new BigInteger(String.valueOf((int)(Math.pow(10,9)+7))));
        System.out.println(sum);

//        for (int i = 0; i < n; i++) {
//            nn[i]=i+1;
//        }
//        quan(0);
//        System.out.println(ans);
//        writer.flush();
//    }
//
//    private static void quan(int i) {
//        int t=0;
//        if(nn[pos]==x&&i==n-1) {
//            ans++;
//            return;
//        }else if (i==n-1)
//            return;
//        for (int j = i; j < n; j++) {
//            t=nn[j];
//            nn[j]=nn[i];
//            nn[i]=t;
//            quan(i+1);
//            t=nn[j];
//            nn[j]=nn[i];
//            nn[i]=t;
//
//        }
//    }
//
//    private static void print(int[] nn) {
//        for (int i = 0; i<n; i++) {
//            System.out.print(nn[i]);
//        }
//        System.out.println();
    }
}


import java.util.*;
import java.io.PrintWriter;
public class a
{
	public static void main(String[] arg)
	{
		new a();
	}
	long MOD = 1_000_000_007;
	long[][] memo;
	long[] fib;
	public a()
	{
		Scanner in = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = in.nextInt();
		long k = in.nextLong();
		int[] arr = new int[n];
		for(int i = 1; i <= n; i++) arr[i-1] = i;
		fib = new long[n+1];
		fib[0] = 1;
		fib[1] = 1;
		for(int i = 2; i < fib.length; i++) fib[i] = fib[i-1] + fib[i-2];
		int index = 0;
		while(k > 1)
		{
			if(k <= fib[n-index-1])
				index++;
			else
			{
				k -= fib[n-index-1]+1;
				swap(arr, index, index+1);
			}
		}
		for(int v : arr) out.print(v + " ");
		in.close();
		out.close();
	}
	void swap(int[] arr, int i, int j)
	{
		int temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}

}


import com.sun.xml.internal.ws.handler.ServerLogicalHandlerTube;
import org.codehaus.groovy.classgen.asm.sc.StaticTypesBinaryExpressionMultiTypeDispatcher;

import java.io.*;
import java.util.InputMismatchException;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class Main extends IOException {
    public static void main(String[] args) throws IOException {
        InputStream inputStream =  System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        FastPrinter out = new FastPrinter(outputStream);
        TaskA solver = new TaskA();
        //int testCount = Integer.parseInt(in.next());
        //for (int i = 1; i <= testCount; i++)
        //solver.solve(i, in, out);
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskA {
    StringBuilder builder = new StringBuilder();
    private static final int LIMIT = 63;
    private static final long[] POW2 = new long[LIMIT];
    private static final long[] ONES = new long[LIMIT];

    public void solve(int testNumber, FastScanner in, FastPrinter out) {
        long a = in.nextLong();
        long b = in.nextLong();
        SortedSet<Long> set = new TreeSet<>();
        init();
        for (int i = 2; i < LIMIT; i++) {
            long full = ONES[i];
            for (int j = 0; j < i - 1; j++) {
                set.add(full - POW2[j]);
            }
        }
        int cnt = 0;
        for(Long num: set){
            if (num >= a && num <= b) cnt++;
        }
        builder.append(cnt);
        out.print(builder.toString());
    }
    private void init(){
        POW2[0] = 1;
        for (int i = 1; i < LIMIT; i++) {
            POW2[i] = 2 * POW2[i - 1];
        }
        for (int i = 0; i < LIMIT; i++) {
            ONES[i] = POW2[i] - 1;
        }
    }
}
class Vector{
    double x, y, z;

    public Vector(double x, double y, double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public double abs(){
        return Math.sqrt(x * x + y * y + z * z);
    }

    public double skalyarMultyply(Vector other){
        return (x * other.x + y * other.y + z * other.z);
    }

    public Vector vectorMultyply(Vector other){
        double i = y * other.z - z * other.y;
        double j = z * other.x - x * other.z;
        double k = x * other.y - y * other.x;
        return new Vector(i, j, k);
    }

    public double mixMultylpy(Vector b, Vector c){
        return this.skalyarMultyply(b.vectorMultyply(c));
    }
}
class FastScanner extends BufferedReader {
    boolean isEOF;

    public FastScanner(InputStream is) {
        super(new InputStreamReader(is));
    }

    public FastScanner(InputStreamReader inputStreamReader) {
        super(inputStreamReader);
    }

    public int read() {
        try {
            int ret = super.read();
            if (isEOF && ret < 0) {
                throw new InputMismatchException();
            }
            isEOF = ret == -1;
            return ret;
        } catch (IOException e) {
            throw new InputMismatchException();
        }
    }

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

    public static boolean isWhiteSpace(int c) {
        return c >= -1 && c <= 32;
    }

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

    public int nextInt() {
        int c = read();
        while (isWhiteSpace(c)) {
            c = read();
        }
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int ret = 0;
        while (!isWhiteSpace(c)) {
            if (c < '0' || c > '9') {
                throw new NumberFormatException("digit expected " + (char) c
                        + " found");
            }
            ret = ret * 10 + c - '0';
            c = read();
        }
        return ret * sgn;
    }

    public double nextDouble() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        double res = 0;
        while (!isSpaceChar(c) && c != '.') {
            if (c == 'e' || c == 'E')
                return res * Math.pow(10, nextInt());
            if (c < '0' || c > '9')
                throw new UnknownError();
            res *= 10;
            res += c - '0';
            c = read();
        }
        if (c == '.') {
            c = read();
            double m = 1;
            while (!isSpaceChar(c)) {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new UnknownError();
                m /= 10;
                res += (c - '0') * m;
                c = read();
            }
        }
        return res * sgn;
    }

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

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

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

final class FastPrinter extends PrintWriter {
    public FastPrinter(OutputStream out) {
        super(out);
    }
}
class Pair<U, V> implements Comparable<Pair<U, V>> {
    public final U first;
    public final V second;

    public static<U, V> Pair<U, V> makePair(U first, V second) {
        return new Pair<U, V>(first, second);
    }

    private Pair(U first, V second) {
        this.first = first;
        this.second = second;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Pair pair = (Pair) o;

        return !(first != null ? !first.equals(pair.first) : pair.first != null) && !(second != null ? !second.equals(pair.second) : pair.second != null);

    }

    public int hashCode() {
        int result = first != null ? first.hashCode() : 0;
        result = 31 * result + (second != null ? second.hashCode() : 0);
        return result;
    }

    public String toString() {
        return "(" + first + "," + second + ")";
    }

    public int compareTo(Pair<U, V> o) {
        int value = ((Comparable<U>)first).compareTo(o.first);
        if (value != 0)
            return value;
        return ((Comparable<V>)second).compareTo(o.second);
    }
}


import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;

public class NewYearAndOldProperty {
	static ArrayList<Long> key = new ArrayList<>();
	static TreeMap<Long, Integer> t = new TreeMap<>();
	static TreeSet<Long> temp = new TreeSet<>();
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong(), m = scan.nextLong(); make(); ans();
		System.out.println(t.get(t.floorKey(m)) - t.get(t.ceilingKey(n)) + 1);
	}
	static void make(){
		long n = 2;
		for(int i = 0 ; Math.pow(n, i) < 2000000000000000000L ; i++) key.add((long) Math.pow(n, i));
	}
	static void ans(){
		for(int i = 2 ; i < key.size() ; i++){
			char[] num = Long.toBinaryString(key.get(i)-1).toCharArray();
			for(int j = 1 ; j < num.length ; j++){
				num[j] = '0';
				temp.add(Long.parseLong(makeString(num), 2));
				num[j] = '1';
			}
		}
		for(int i = 1 ; !temp.isEmpty() ; i++){
			long curr = temp.pollFirst();
			t.put(curr, i);
		}
	}
	static String makeString(char[] num){
		String curr = "";
		for(int i = 0 ; i < num.length ; i++) curr+=num[i];
		return curr;
	}
}
/*

10
101
110

*/


import java.util.*;
public class test
{
   public static void main(String args[])
   {
	   Scanner s=new Scanner(System.in);
	   long n=s.nextLong();
	  // int n= Integer.parseInt(str);
	   long x=n+1;
	   if(x==1)
		   Ssytem.out.print(0);
	   else if(x%2==0)
		   System.out.print(x/2);
	   else
		   System.out.print(x);
   }
}


import java.util.Scanner;

public class codeForce2 {
    public static void main(String[]args){
        Scanner sc= new Scanner(System.in);
        if(n!=0){
        long n=sc.nextLong();
        long temp1= (n+1);
        if ((n+1)%2==0){
            long temp=  ((n+1)/2);
            System.out.println(temp);
        }

        else System.out.println(temp1);
        }
        else System.out.println("0");
    }
}


public class NewClass1 {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int cuts=0;
        if((n+1)%2==0){
            cuts=(n+1)/2;
        }
        else if((n+1)/2!=0){
            cuts=n+1;
        }
        System.out.println(cuts);
    }
}


import java.util.*;
public class susbus{
   public static void main(String[] args){
      Scanner sc =new Scanner(System.in);
      int n= sc.nextInt();
      System.out.println(powerthree(n,1000003);
   }
   public static int powerthree(int n,int mod){
      //3^n-1
      if(n==1) return 1;
      return 3*powerthree(n-1,mod)%mod;

   }
}


//import java.awt.Dimension;
import java.awt.Point;
import java.io.*;
import static java.lang.Math.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import static java.math.BigInteger.*;
import java.util.*;
import static java.util.Arrays.*;
//import java.math.BigInteger;
//import static java.lang.Character.*;
//import static java.lang.Math.*;
//import static java.math.BigInteger.*;

//import static java.util.Arrays.*;

//import java.awt.Point; // interger !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//import java.awt.Polygon;
//import java.awt.Rectangle;
//import java.awt.geom.AffineTransform;
//import java.awt.geom.Line2D;
//import java.awt.geom.Point2D;
//import javafx.scene.shape.Line;
//import javafx.scene.transform.Rotate;

//<editor-fold defaultstate="collapsed" desc="Main">
public class Main implements Runnable{

    // https://netbeans.org/kb/73/java/editor-codereference_ru.html#display

    static boolean oj;

    @Override
    public void run(){
        try {
            Locale.setDefault(Locale.US);
        } catch (Exception e) {
        }
        oj = true;
        try {
            oj = System.getProperty("MYLOCAL") == null;
        } catch (Exception e) {
        }
//
        if (oj) {
            sc = new FastScanner(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
        } else {
            try {
                sc = new FastScanner(new FileReader("input.txt"));
                out = new PrintWriter(new FileWriter("output.txt"));
            } catch (IOException e) {
                MLE();
            }
        }
        Solver s = new Solver();
        s.sc = sc;
        s.out = out;

        s.solve();
        if (!oj) {
            err.println("Time: " + (System.currentTimeMillis() - timeBegin) / 1e3);
            err.printf("Mem: %d\n", (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20);
        }
        out.flush();
    }

    private void show(int[] arr) {
        for (int v : arr) {
            err.print(" " + v);
        }
        err.println();
    }

    public static void exit() {
        err.println("Time: " + (System.currentTimeMillis() - timeBegin) / 1e3);
        err.printf("Mem: %d\n", (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20);
        out.flush();
        out.close();
        System.exit(0);
    }

    public static void MLE() {
        int[][] arr = new int[1024 * 1024][];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new int[1024 * 1024];
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        //new Main().run();
        new Thread(null, new Main(), "myThread", 100 * 1024 * 1024).start();
    }

    static long timeBegin = System.currentTimeMillis();
    static FastScanner sc;
    static PrintWriter out;
    static PrintStream err = System.err;
}
    //</editor-fold>

//<editor-fold defaultstate="collapsed" desc="FastScanner">
class FastScanner {

    BufferedReader br;
    StringTokenizer st;

    FastScanner(InputStreamReader reader) {
        br = new BufferedReader(reader);
        st = new StringTokenizer("");
    }

    String next() {
        while (!st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception ex) {
                return null;
            }
        }
        return st.nextToken();
    }

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

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

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

    String nextLine() {
        try {
            return br.readLine();
        } catch (IOException ex) {
            return null;
        }
    }
}
//</editor-fold>

class Solver {

    void aser(boolean OK) {
        if (!OK) {
            Main.MLE();
        }
    }

    FastScanner sc;
    PrintWriter out;
    static PrintStream err = System.err;

    class Item{
        long p;
        int alfa;

        public Item(long p, int alfa) {
            this.p = p;
            this.alfa = alfa;
        }

        @Override
        public String toString() {
            return p + ", " + alfa;
        }

    }

    ArrayList<Long> getAllDiv(long A){
        TreeSet<Long> arr = new TreeSet<>();
        for( long d = 1; d*d <= A; ++d ){
            if( A % d == 0 ) {
                arr.add(d);
                arr.add(A/d);
            }
        }
        return new ArrayList<>(arr);
    }

    long myPow(long p, int alfa){
        long ans = 1;
        for (int i = 0; i < alfa; i++) {
            ans *= p;
        }
        return ans;
    }

    void solve(){
        final long A = sc.nextLong();
        ArrayList<Long> divA = getAllDiv(A);
        TreeMap<Long,ArrayList<Integer>> P = new TreeMap<>();
        for (long div : divA) {
            --div;
            for (int alfa = 1; alfa <= 60; alfa++) {
                long p = (long)pow( div, 1.0 / alfa );
                if( 2<=p && myPow(p,alfa)==div && valueOf(p).isProbablePrime(50) ){
                    if( !P.containsKey(p) )
                        P.put(p, new ArrayList<>());
                    P.get(p).add(alfa);
                }
            }
        }

        long[] dp0, dp;
        dp0 = new long[divA.size()];
        dp0[0] = 1;
        for( long p : P.keySet() ){
            dp = dp0.clone();
            for( int alfa : P.get(p) ){
                long val = myPow(p, alfa) + 1;
                for( long div : divA ){
                    if( 1e18 <= 1.0 * div * val ) continue;
                    long toDiv = div * val;
                    int   ID = Collections.binarySearch(divA,   div);
                    int toID = Collections.binarySearch(divA, toDiv);
                    if( 0 <= toID ){
                        dp[toID] += dp0[ID];
                    }
                }
            }
            //
            dp0 = dp;
        }
        out.println(dp0[divA.size() - 1]);
    }

}


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

public class A_340 {
    FastScanner in;
    PrintWriter out;

    public void run() {
        try {
            in = new FastScanner(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
            solve();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void solve() throws IOException {
        int x = in.nextInt();
        int y = in.nextInt();
        int a = in.nextInt();
        int b = in.nextInt();
        int [] c = new int[b+1];
        int [] d = new int[b+1];
        int j = x;
        while (j <= b) {
            c[j] = 1;
            j +=x;
        }
        j = y;
        while (j <= b) {
            d[j] = 1;
            j += y;
        }
        int counter = 0;
        for (int i=a; i<=b; i++) {
            if (c[i] == 1 && d[i] == 1) {
                counter++;
            }
        }
        out.println(counter);
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

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

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

            return str;
        }

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

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

    public static void main(String[] arg) {
        A_340 o = new A_340();
        o.run();
    }

}


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

    public  class Sample implements Runnable
    {

        public static void solve()
        {
             int n=i(); int m=i();
             int[] a=new int[n];
             int[] b=new int[m];
             for(int i=0;i<n;i++)a[i]=i();
            for(int j=0;j<m;j++)b[j]=i();
              Arrays.sort(a);
             Arrays.sort (b);
             if(a[0]>b[0])out.println(b[0]+""+a[0]);
             else out.println(a[0]+""+b[0]);
        }

          public void run()
          {
              solve();
              out.close();
          }

          public static void main(String[] args) throws IOException
          {
                new Thread(null, new Sample(), "whatever", 1<<26).start();
          }
         static class Pair implements Comparable<Pair>
         {
            long a;
            int b;
            Pair(){}
            Pair(long a,int b)
            {
                       this.a=a;
                       this.b=b;
            }

            public int compareTo(Pair x)
           {
                return Long.compare(x.a,this.a);
           }
        }

    ////////////////////////////////////////////////////// Merge Sort ////////////////////////////////////////////////////////////////////////

   static class Merge
   {

        public static void sort(long inputArr[])
        {
            int length = inputArr.length;
            doMergeSort(inputArr,0, length - 1);
        }

        private static void doMergeSort(long[] arr,int lowerIndex, int higherIndex)
        {
            if (lowerIndex < higherIndex) {
                int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
                doMergeSort(arr,lowerIndex, middle);
                doMergeSort(arr,middle + 1, higherIndex);
                mergeParts(arr,lowerIndex, middle, higherIndex);
            }
        }

        private static void mergeParts(long[]array,int lowerIndex, int middle, int higherIndex)
        {
            long[] temp=new long[higherIndex-lowerIndex+1];
            for (int i = lowerIndex; i <= higherIndex; i++)
            {
                temp[i-lowerIndex] = array[i];
            }
            int i = lowerIndex;
            int j = middle + 1;
            int k = lowerIndex;
            while (i <= middle && j <= higherIndex)
            {
                if (temp[i-lowerIndex] < temp[j-lowerIndex])
                {
                    array[k] = temp[i-lowerIndex];
                    i++;
                } else {
                    array[k] = temp[j-lowerIndex];
                    j++;
                }
                k++;
            }
            while (i <= middle)
            {
                array[k] = temp[i-lowerIndex];
                k++;
                i++;
            }
            while(j<=higherIndex)
            {
                array[k]=temp[j-lowerIndex];
                k++;
                j++;
            }
        }

    }

    /////////////////////////////////////////////////////////// Methods ////////////////////////////////////////////////////////////////////////

     static boolean isPal(String s)
     {
        for(int i=0, j=s.length()-1;i<=j;i++,j--)
        {
                if(s.charAt(i)!=s.charAt(j)) return false;
        }
        return true;
     }
     static String rev(String s)
     {
                StringBuilder sb=new StringBuilder(s);
                sb.reverse();
                return sb.toString();
     }
     static int gcd(int a,int b){return (a==0)?b:gcd(b%a,a);}
     static long gcdExtended(long a,long b,long[] x)
     {

        if(a==0){
            x[0]=0;
            x[1]=1;
            return b;
        }
        long[] y=new long[2];
        long gcd=gcdExtended(b%a, a, y);

        x[0]=y[1]-(b/a)*y[0];
        x[1]=y[0];

        return gcd;
    }

     boolean findSum(int set[], int n, long sum)
    {
      if (sum == 0)
         return true;
      if (n == 0 && sum != 0)
         return false;
      if (set[n-1] > sum)
         return findSum(set, n-1, sum);
      return findSum(set, n-1, sum) ||findSum(set, n-1, sum-set[n-1]);
    }

      public static long modPow(long base, long exp, long mod)
     {
        base = base % mod;
        long result = 1;
        while (exp > 0)
       {
            if (exp % 2 == 1)
            {
                result = (result * base) % mod;
            }
            base = (base * base) % mod;
            exp = exp >> 1;
       }
        return result;
    }

    static long[] fac;
    static long[] inv;
    static long mod=(long)1e9+7;
    public static void cal()
    {
        fac = new long[1000005];
        inv = new long[1000005];
        fac[0] = 1;
        inv[0] = 1;
        for (int i = 1; i <= 1000000; i++)
        {
            fac[i] = (fac[i - 1] * i) % mod;
            inv[i] = (inv[i - 1] * modPow(i, mod - 2, mod)) % mod;
        }
    }

    public static long ncr(int n, int r)
    {
        return (((fac[n] * inv[r]) % mod) * inv[n - r]) % mod;
    }

////////////////////////////////////////// Input Reader ////////////////////////////////////////////////////////////////////////////////////////////////////

    static InputReader sc = new InputReader(System.in);
    static PrintWriter out= new PrintWriter(System.out);

    static class InputReader {

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

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

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

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

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

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

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

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

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

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

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

    }

    static int i()
    {
        return sc.nextInt();
    }
    static long l(){
        return sc.nextLong();
    }
    static int[] iarr(int n)
    {
        return sc.nextIntArray(n);
    }
    static long[] larr(int n)
    {
        return sc.nextLongArray(n);
    }
    static String s(){
        return sc.nextLine();
    }

 }


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

public class main {

	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();
		int a=1,b=1;
		int ans=0;
		while(true)
		{
			int c=a+b;
			if(c<=n)
				ans++;
			else
				break;
			a=b;
			b=c;
		}
		System.out.println(ans);
	}

}


import com.sun.xml.internal.ws.util.StringUtils;
import java.util.*;

/**
 *
 * @author Lenovo
 */
public class JavaApplication2 {

    public static void main(String[] args) {
     String x;
     Scanner in=new Scanner (System.in);
     x=in.next();
    int z=1;
    String aw=x;
     aw=aw.charAt(aw.length()-1)+charRemoveAt(aw,aw.length()-1);
    while (true){

        if(aw == null ? x == null : aw.equals(x))break;
        else {z++;
     aw=aw.charAt(aw.length()-1)+charRemoveAt(aw,aw.length()-1);

        }
     }

     System.out.println(z);
}
  public static String charRemoveAt(String str, int p) {
              return str.substring(0, p) + str.substring(p + 1);
           }

}


//package cf820;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.StringTokenizer;

public class B {
	static int M, B, maxX, maxY;
	static long a[][];
	public static void main(String[] args) throws FileNotFoundException {
		InputStream is = System.in;
//		is = new FileInputStream(new File("A.txt"));
		FastScanner sc = new FastScanner(new InputStreamReader(is));
		M = sc.nextInt();
		B = sc.nextInt();
		maxX=M*B;
		maxY=B;
		a=new long[maxY+1][maxX+1];
		for (int i=1; i<=maxY; i++) a[i][0]=i+a[i-1][0];
		for (int i=1; i<=maxX; i++) a[0][i]=i+a[0][i-1];
		long max=0;
		for (int x=maxX/2; x<=maxX; x++) {
			int y = Y(x);
			max = Math.max(max, s(x,y));
		}
		System.out.println(max);
	}
	static long s(int x, int y) {
		return (x+1)*a[y][0] +(y+1)*a[0][x];
	}
	static int Y(int x) {
		double m=M*1.0;
		double X = 1.0*x;
		double Y = B-(1.0*X/m);
		int y = (int)Y;
		if (y>Y) y--;
		return y;
	}
	static class FastScanner {
	    BufferedReader br;
	    StringTokenizer st;

	    FastScanner(Reader in) {
	        br = new BufferedReader(in);
	    }

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

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

}


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

public class main1 {
	public static void main (String[] args) {
	    Scanner sc=new Scanner(System.in);
	  Hashtable<Integer,Hashtable<Integer,Integer>> h1=new Hashtable();

	    int m=sc.nextInt();
        int b=sc.nextInt();
        int y=b;
        int x=(b)*m;
        int arr[][]=new int[x+1][y+1];
        int max=0;
        arr[0][0]=0;
        for(int i=1;i<=x;i++){
        	arr[i][0]=i+arr[i-1][0];
        	if(max<arr[i][0]) max=arr[i][0];
        }
        for(int i=1;i<=y;i++){
        	arr[0][i]=i+arr[0][i-1];
        	if(max<arr[0][i]) max=arr[0][i];
        }
        for(int i=1;i<=y;i++){
        	for(int j=1;j<=(b-i)*m;j++){
        		arr[j][i]=arr[j-1][i]+arr[j][i-1]-arr[j-1][i-1]+i+j;
        		if(max<arr[j][i]) max=arr[j][i];
        	}
        }
        System.out.println(max);

	}}


import java.util.Scanner;

public class _821okabeB {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		int b = sc.nextInt();
		sc.close();

		int max_x =b*m+1;
		int max_y = b+1;

		int sums[][] = new int[max_x][max_y];

		for(int i=0;i<max_x;i++) {
			for(int j=0;j<max_y;j++) {
				sums[i][j] = 0;
			}
		}

		int y;

		for(int x=0;x<max_x;x++) {

			y=-x/m+b;

			if(((y-b)*m+x)==0) {
				for(int d=y;d>=0;d--) {

					for(int e=x;e>=0;e--) {

						sums[x][y] += e+d;
					}
				}
			}
		}

		int max=0;

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

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

				if(sums[i][j]>max)

					max=sums[i][j];
			}
		}

		System.out.print(max);
	}

}


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

public class C {

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

	C() {

		Scanner in = new Scanner(System.in);

		int len = in.nextInt();
		int rem = 0,count=0;
		ArrayList<Integer> a = new ArrayList<Integer>(len);

		for(int t=0;t<2*len;t++){
			//System.out.println(a);
			if(in.next().equals("add")){
				int n = in.nextInt()-1;
				a.add(n);
			}else{ //remove
				if(a.get(a.size()-1)!=rem++){
					Collections.sort(a,Collections.reverseOrder());
					count++;
				}
				a.remove(a.size()-1);
			}
		}

		System.out.println(count);

		in.close();

	}

}

/*

3
add 1
remove
add 2
add 3
remove
remove

7
add 3
add 2
add 1
remove
add 4
remove
remove
remove
add 6
add 7
add 5
remove
remove
remove

 */


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

/*
Solution Sketch:
*/

public class banana {

	static final boolean stdin = true;
	static final String filename = "";
	static FastScanner br;
	static PrintWriter pw;

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

		if (stdin) {
			br = new FastScanner();
			pw = new PrintWriter(new OutputStreamWriter(System.out));
		}

		else {
			br = new FastScanner(filename + ".in");
			pw = new PrintWriter(new FileWriter(filename + ".out"));
		}

		X solver = new X();
		solver.solve(br, pw);
	}

	static class X {
		static int m,b;
		public void solve(FastScanner br, PrintWriter pw) throws IOException {
			m = br.nextInt();
			b = br.nextInt();
			int[][] pre = new int[m*b+1][b+1];
			for(int i = 1; i <= m*b; i++) {
				pre[i][0] = pre[i-1][0] + i;
				if(i <= b) {
				pre[0][i] = pre[0][i-1] + i;
				}
			}
			for(int i = 1; i <= m*b; i++) {
				for(int j = 1; j <= b; j++) {
					pre[i][j] = (pre[i-1][j] + pre[i][j-1] + (i+j))-pre[i-1][j-1];
				}
			}
			int mx = 0;
			for(int x = 0; x <= m*b; x+= m) {
				int y = gy(x);
//				pw.println(x + " " + y + " " + pre[x][y]);
				mx = Math.max(mx, pre[x][y]);
			}
			pw.println(mx);
			pw.close();
		}
		public static int gy(int x) {
			return (-1 * (x/m))+b;
		}

	}

	//fastscanner class
	public static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(String s) {
			try {
				br = new BufferedReader(new FileReader(s));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

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

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

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

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

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

}


import java.util.*;
public class FindtheBug{
	   public static void main(String[] args) {
	        // Use the Scanner class
	        Scanner in = new Scanner(System.in);
	        int digits = in.nextInt();
	        int nums[] = new int[digits];
	        String val = in.next();
	        int total = 1;
	        for(int i = 0; i < digits; i++){
	        	nums[i] = Integer.parseInt(""+val.charAt(i));
	        	if(nums[i] == 2)
	        		total *= 2;
	        	else if(nums[i] == 3)
	        		total *= 6;
	        	else if(nums[i] == 4)
	        		total *= 24;
	        	else if(nums[i] == 5)
	        		total *= 120;
	        	else if(nums[i] == 6)
	        		total *= 720;
	        	else if(nums[i] == 7)
	        		total *= 5040;
	        	else if(nums[i] == 8)
	        		total *= 40320;
	        	else if(nums[i] == 9)
	        		total *= 362880;
	        	else
	        		total *= 1;
	        }
	        while(total != 1){
	        	if(total % 5040 == 0){
	        		System.out.print(7);
	        		total /= 5040;
	        	}
	        	else if(total % 120 == 0){
	        		System.out.print(5);
	        		total /= 120;
	        	}
	        	else if(total % 6 == 0){
	        		System.out.print(3);
	        		total /= 6;
	        	}
	        	else if(total % 2 == 0){
	        		System.out.print(2);
	        		total /= 2;
	        	}

	        }

	      /*
	      int n      = sc.nextInt();        // read input as integer
	      long k     = sc.nextLong();       // read input as long
	      double d   = sc.nextDouble();     // read input as double
	      String str = sc.next();           // read input as String
	      String s   = sc.nextLine();       // read whole line as String
	      */
	    }
}


import java.util.*;
public class _515C_Drazil_and_Factorial {
    public static int f[]=new int[100];
    public static void factorial()
    {
        f[1]=1;
        int i;
        for (i=2;i<=9;i++)
            f[i]=f[i-1]*i;
    }
    public static void main(String[] args) {
        factorial();
        Scanner input= new Scanner (System.in);
        int n= input.nextInt();
        String a= input.next();
        int i,s=1,Max=0;
        for (i=0;i<n;i++)
        {
            s*=f[(int)a.charAt(i)-48];
            Max=Math.max(Max,(int)a.charAt(i)-48);
        }
        int t=0;
        for (i=0;i<n;i++)
            if ((int)a.charAt(i)-48==Max)
                t++;
        if (t==n)
        {
            System.out.println(a);
            input.close();
            return;
        }
        for (i=Max-1;i>=2;i--)
            if (s%f[i]==0)
            {
                s/=f[i];
                System.out.print(i);
                i=Max;
            }
        input.close();
    }
}


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

public class pipeline {
    static long N, K;
    static boolean[] used;
    static long currentPipes, currentSplit;

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader in = new BufferedReader(new FileReader("pipeline.in"));
        StringTokenizer st = new StringTokenizer(in.readLine());
        N = Long.parseLong(st.nextToken());
        K = Long.parseLong(st.nextToken());
        used = new boolean[(int) (K+1)];
        Arrays.fill(used, false);
        used[0] = true;
        currentPipes = 1;
        currentSplit = 0;

        if (K*K - K + 2 < 2*N) {
            System.out.println(-1);
            System.exit(0);
        }
        while (N != currentPipes) {
            for (long i = Math.min(N-currentPipes+1, K); i > 1; i--) {
                if (!used[(int) i]) {
                    currentPipes = currentPipes + i - 1;
                    currentSplit++;
                    used[(int) i] = true;
                    break;
                }

                if (i == 2) {
                    System.out.println(-1);
                    System.exit(0);
                }
            }

            if (N == currentPipes) {
                System.out.println(currentSplit);
                break;
            }
        }
        System.exit(0);
    }
}


package ru.spb.bourevestnik.ar35.scada.util.printer;

import java.util.Scanner;

/**
 * Created by plahotnik_dy on 21.12.2016.
 */

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

        for (int rowCount = (int)Math.sqrt(pixelsCount); rowCount > 0; rowCount--) {
            if (pixelsCount % rowCount == 0) {
                System.out.println(rowCount + " " + pixelsCount / rowCount);
                return;
            }
        }
    }
}


import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {

    public static void main(String[] args) throws InterruptedException, AWTException, UnknownHostException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        PrintWriter out = new PrintWriter(outputStream);
        InputReader in = new InputReader(inputStream);

        int n = in.nextInt();
        int min = Integer.MAX_VALUE, a = 0, b;
        for(int i = 2; i <= Math.sqrt(n); i++) {
            if(n % i == 0) {
                a = i;
            }
        }
        out.println(a + " " + n/a);
        out.close();
    }

    static class InputReader {

        BufferedReader reader;
        StringTokenizer tokenizer;

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

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return tokenizer.nextToken();
        }

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

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

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

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

        public double[] nextArrayd(int n) {
            double[] a = new double[n];
            for (int i = 0; i < n; i++) {
                a[i] = nextDouble();
            }
            return a;
        }

        private BigInteger nextBigInteger() {
            return new BigInteger(next());
        }
        public void printAr(long[] a) {
            System.out.print("[");
            for(long x  : a)System.out.print(x + ",");
            System.out.print("]");
        }
    }

}


import javax.swing.plaf.basic.BasicButtonUI;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int min = 1001, max =0, kupil, prod, ost;
        int utr[] = new int[30];
        int vetr[] = new int[30];
        int n = scannernextInt();
        int m = scanner.nextInt();
        int r = scanner.nextInt();

        for(int i =0; i < n; i++){
            utr[i] = scanner.nextInt();
            if(utr[i] < min)
                min = utr[i];
        }

        for(int i = 0; i < m; i++){
            vetr[i] = scanner.nextInt();
            if(vetr[i] > max)
                max = vetr[i];
        }

        if(min > r){
            System.out.println(r);
            return 0;
        }

        kupil = r / min;
        ost = r % min;

        prod = kupil * max + ost;

        if(prod <= r){
            System.out.println(r);
        }
        System.out.println(prod);

    }
}


import java.util.*;
public class Main{
  public static void main(String[]args){
  Scanner sc=new Scanner(System.in);
  int n=sc.nextInt();
  int m=sc.nextInt();
  int r=sc.nextInt();

  int min=0;
  for (int i=1;i<=n;i++){
  int s=sc.nextInt();
  if(s<min){
    min=s;

  }
  }
  for

  }
}


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

 public class wef {
static HashSet<Long>set=new HashSet<Long>();
public static class FastReader {
	BufferedReader br;
	StringTokenizer st;
	//it reads the data about the specified point and divide the data about it ,it is quite fast
	//than using direct

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

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

	int nextInt() {
		return Integer.parseInt(next());//converts string to integer
	}

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

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

	String nextLine() {
		String str = "";
		try {
			str = br.readLine();
		} catch (Exception r) {
			r.printStackTrace();
		}
		return str;
	}
}
static ArrayList<String>list1=new ArrayList<String>();
static void combine(String instr, StringBuffer outstr, int index,int k)
{
	if(outstr.length()==k)
	{
		list1.add(outstr.toString());return;
	}
	if(outstr.toString().length()==0)
	outstr.append(instr.charAt(index));
    for (int i = 0; i < instr.length(); i++)
    {
        outstr.append(instr.charAt(i));

        combine(instr, outstr, i + 1,k);
        outstr.deleteCharAt(outstr.length() - 1);
    }
   index++;
}
static ArrayList<ArrayList<Integer>>l=new ArrayList<>();
static void comb(int n,int k,int ind,ArrayList<Integer>list)
{
	if(k==0)
	{
		l.add(new ArrayList<>(list));

		return;
	}

	for(int i=ind;i<=n;i++)
	{
		list.add(i);
		comb(n,k-1,ind+1,list);

		list.remove(list.size()-1);

	}

}
static long sum(long n)
{
	long sum=0;
	while(n!=0)
	{
		sum+=n%10;
		n/=10;
	}
	return sum;
}

static boolean check(HashMap<Integer,Integer>map)
{
	for(int h:map.values())
		if(h>1)
			return false;
	return true;
}

static class Pair implements Comparable<Pair>{
    int x;int y;
    Pair(int x,int y){
        this.x=x;
        this.y=y;
      //  this.i=i;
    }
	@Override
	public int compareTo(Pair o) {
		// TODO Auto-generated method stub
		return x-o.x;

	}
}

public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	FastReader in=new FastReader();
	ArrayList<Integer>list=new ArrayList<Integer>();
	int n=in.nextInt();
	int m=in.nextInt();
	int r=in.nextInt();
	for(int i=0;i<n;i++)
		list.add(in.nextInt());
	int min=Collections.min(list);
	list.clear();
	for(int i=0;i<n;i++)
		list.add(in.nextInt());

	int max=Collections.max(list);

	int buy=r/min;
	int rem=r%min;

	int sell=buy*max;

	System.out.println(Math.max(r, sell+rem));

	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

/**
 * @author khokharnikunj8
 */

public class Main {
    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Main().solve();
            }
        }, "1", 1 << 26).start();
    }

    void solve() {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        AStockArbitraging solver = new AStockArbitraging();
        solver.solve(1, in, out);
        out.close();
    }

    static class AStockArbitraging {
        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            int n = in.scanInt();
            int m = in.scanInt();
            int r = in.scanInt();
            int ans = r;
            int min = Integer.MAX_VALUE;
            int max = -1;
            for (int i = 0; i < n; i++) min = Math.min(min, in.scanInt());
            for (int i = 0; i < n; i++) max = Math.max(max, in.scanInt());
            if (max - min >= 0) {
                int how = r / min;
                r -= (how * min);
                how *= max;
                r += how;
                ans = Math.max(ans, r);
            }
            out.println(ans);
        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int index;
        private BufferedInputStream in;
        private int total;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (index >= total) {
                index = 0;
                try {
                    total = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (total <= 0) return -1;
            }
            return buf[index++];
        }

        public int scanInt() {
            int integer = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    integer *= 10;
                    integer += n - '0';
                    n = scan();
                }
            }
            return neg * integer;
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

    }
}


/**
 * Created with IntelliJ IDEA.
 * User: Zakhar_Voit
 * Date: 21.04.12
 * Time: 1:48
 */

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

import static java.lang.System.exit;

public class C {

    class Pair implements Comparable<Pair> {
        long u;
        long v;

        public Pair(long u, long v) {
            this.u = u;
            this.v = v;
        }

        public int hashCode() {
            int hu = (int) (u ^ (u >>> 32));
            int hv = (int) (v ^ (v >>> 32));
            return (int) (hu * 31 + hv);
        }

        public boolean equals(Object o) {
            Pair other = (Pair) o;
            return u == other.u && v == other.v;
        }

        public int compareTo(Pair other) {
            if (u != other.u)
                return u < other.u ? -1 : 1;

            return v < other.v ? -1 : v > other.v ? 1 : 0;
        }

        public String toString() {
            return "[u=" + u + ", v=" + v + "]";
        }

    }

    BufferedReader in;
    PrintWriter out;
    StringTokenizer st = new StringTokenizer("");

    public void openIO() throws IOException {
        if (new File("input.txt").exists()) {
            System.setIn(new FileInputStream("input.txt"));
            out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
        } else {
            out = new PrintWriter(System.out);
        }
        in = new BufferedReader(new InputStreamReader(System.in));
    }

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

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

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

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

    String nextLine() throws IOException {
        st = new StringTokenizer("");
        return in.readLine();
    }

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

    int n, m, k, ans = 0;
    int[][] a, b, c, p;
    int number[][];

    static void swap(int[] a, int i, int j) {
        int t = a[j];
        a[j] = a[i];
        a[i] = t;
    }

    void init() {

    }

    void read() throws IOException {
        n = nextInt();
        m = nextInt();
        k = nextInt();
        a = new int[n][m];
        b = new int[n][m];
        c = new int[n][m];
        number = new int[n][m];
        p = new int[n][m];
        for (int i = 0; i < n; i++) {
            nextLine();
            for (int j = 0; j < m; j++) {
                a[i][j] = nextInt();
                b[i][j] = nextInt();
                c[i][j] = nextInt();
            }
        }
    }

    void solve() {
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                int[] diff = new int[m], cnt = c[i];
                for (int k = 0; k < m; k++) {
                    diff[k] = b[j][k] - a[i][k];
                }
                for (int t = 0; t < m; t++) {
                    for (int c = 0; c + 1< m; c++) {
                        if (diff[c] < diff[c + 1]) {
                            swap(diff, c, c + 1);
                            swap(cnt, c, c + 1);
                        }
                    }
                }
                int cur = 0, profit = 0, idx = 0;
                while (cur < k && idx < m) {
                    int lastcur = cur;
                    cur += Math.min(k - lastcur, c[i][idx]);
                    profit += Math.min(k - lastcur, c[i][idx]) * diff[idx];
                    idx++;
                }
                p[j][i] = p[i][j] = Math.max(p[j][i], profit);
            }
        }
    }

    void write() {
        for (int i = 0; i < n; i++) {
            int mx = 0;
            for (int j = 0; j < n; j++) {
                mx = Math.max(mx, p[i][j]);
            }
            ans = Math.max(mx, ans);
        }
        out.println(ans);
    }

    public void run() throws IOException {
        openIO();
        init();
        read();
        solve();
        write();
        out.close();
    }

    static public void main(String[] args) throws IOException {
        new Thread(null, new Runnable() {
            public void run() {
                try {
                    new C().run();
                } catch (Throwable e) {
                    e.printStackTrace();
                    exit(999);
                }
            }
        }, "1", 1 << 23).start();
    }
}


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

public class test3
{

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

	PrintWriter out = null;

	Product[][] products = null;
	int n, m, k;
	int max = 0;

	void Trade(int x, int y)
	{
		for (int i = 0; i < m; i++)
		{
			if (products[x][i].a < products[y][i].b && products[x][i].c != 0)
				products[x][i].d = products[y][i].b - products[x][i].a;
			else
				products[x][i].d = -1;
		}

		Arrays.sort(products[x]);

		int total = 0;
		int i = 0;
		int kk = k;

		while (kk > 0 && i < m)
		{
			if (products[x][i].d < 0)
				break;

			total += products[x][i].d * Math.min(products[x][i].c, kk);
			kk -= Math.min(products[x][i].c, kk);
			i++;
		}

		max = Math.max(max, total);

		Arrays.sort(products[x], new Comparator<Product>()
		{
			public int compare(Product o1, Product o2)
			{
				return o1.id - o2.id;
			}
		});
	}

	void run() throws Exception
	{
		Scanner in = new Scanner(System.in);
		out = new PrintWriter(System.out);

		n = in.nextInt();
		m = in.nextInt();
		k = in.nextInt();

		products = new Product[n][m];

		for (int i = 0; i < n; i++)
		{
			in.next();
			for (int j = 0; j < m; j++)
			{
				products[i][j] = new Product(m, in.nextInt(), in.nextInt(),
						in.nextInt());
			}
		}

		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
			{
				// if (i == j)
				// continue;

				Trade(i, j);
			}

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

class Product implements Comparable<Product>
{
	int id;
	int a;
	int b;
	int c;
	int d;

	public Product(int _id, int _a, int _b, int _c)
	{
		id = _id;
		a = _a;
		b = _b;
		c = _c;
	}

	public int compareTo(Product o)
	{
		return o.d - d;
	}
}


import java.io.*;
import java.util.*;
public class Div2_422_A implements Runnable
{
    public void run()
    {
        InputReader sc = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);

        long a = sc.nextLong();
        long b = sc.nextLong();

        long ans1 = factorial(a);
        long ans2 = factorial(b);

        out.print(gcd(ans1,ans2));
        out.close();
    }

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

    int factorial(int n){
      if (n == 0)
        return 1;
      else
        return(n * factorial(n-1));
    }

    long factorial(long n){
      if (n == 0)
        return 1;
      else
        return(n * factorial(n-1));
    }

    static class InputReader
    {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

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

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

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

        public String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        public int nextInt() {
            int c = read();

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

            int sgn = 1;

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

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

            return res * sgn;
        }

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

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

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

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

            return res.toString();
        }

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

        public String next() {
            return readString();
        }

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

    public static void main(String args[]) throws Exception {
        new Thread(null, new Div2_422_A(),"Main",1<<27).start();
    }
}