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

public class A {

	static final int[] dx = { 0, 0, -1, 1 };
	static final int[] dy = { -1, 1, 0, 0 };
	static int[][] grid;
	static int N, cnt, inside[], size[];

	static boolean valid(int x, int y) { return x < N && y < N && x > -1 && y > -1 && grid[x][y] == 0; }

	static void dfs(int x, int y, int c)
	{
		grid[x][y] = c;
		size[c]++;
		for(int k = 0; k < 4; ++k)
		{
			int i = x + dx[k], j = y + dy[k];
			if(valid(i, j))
				dfs(i, j, c);
		}
	}

	static void add(int x) { if(inside[x]++ == 0) cnt += size[x]; }

	static void erase(int x) { if(inside[x]-- == 1) cnt -= size[x]; }

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

		N = sc.nextInt();
		int k = sc.nextInt();
		grid = new int[N][N];
		for(int i = 0; i < N; ++i)
		{
			String s = sc.next();
			for(int j = 0; j < N; ++j)
				if(s.charAt(j) == 'X')
					grid[i][j] = -1;
				else
					grid[i][j] = 0;
		}
		int idx = 0;
		inside = new int[N];
		size = new int[N];
		for(int i = 0; i < N; ++i)
			for(int j = 0; j < N; ++j)
				if(grid[i][j] == 0)
					dfs(i, j, ++idx);

		for(int i = 0; i < k; ++i)
			for(int j = 0; j < k; ++j)
				if(grid[i][j] == -1)
					++cnt;
				else
					add(grid[i][j]);
		if(k == N)
			out.println(cnt);
		else
		{
			for(int i = 0; i < k; ++i)
			{
				if(grid[i][k] != -1)
					add(grid[i][k]);
				if(grid[k][i] != -1)
					add(grid[k][i]);
			}
			int ans = cnt, d = 1, sx = 0, sy = 0;
			while(true)
			{
				while(sy + d != -1 && sy + d + k <= N)
				{
					if(d == 1)
						goRight(sx, sy++, k);
					else
						goLeft(sx, sy--, k);
					ans = Math.max(ans, cnt);
				}
				if(sx + k == N)
					break;
				goDown(sx++, sy, k);
				ans = Math.max(ans, cnt);
				d *= -1;
			}
			out.println(ans);
		}
		out.flush();
		out.close();
	}

	static void goDown(int sx, int sy, int k)
	{
		if(sx != 0)
			for(int j = sy; j < sy + k; ++j)
				if(grid[sx - 1][j] != -1)
					erase(grid[sx - 1][j]);
		for(int j = sy; j < sy + k; ++j)
			if(grid[sx][j] == -1)
				--cnt;
		if(sy > 0 && grid[sx][sy - 1] != -1)
			erase(grid[sx][sy - 1]);
		if(sy + k < N && grid[sx][sy + k] != -1)
			erase(grid[sx][sy + k]);
		++sx;
		for(int j = sy; j < sy + k; ++j)
			if(grid[sx + k - 1][j] == -1)
				++cnt;
		if(sx + k < N)
			for(int j = sy; j < sy + k; ++j)
				if(grid[sx + k][j] != -1)
					add(grid[sx + k][j]);
		if(sy > 0 && grid[sx + k - 1][sy - 1] != -1)
			erase(grid[sx + k - 1][sy - 1]);
		if(sy + k < N && grid[sx + k - 1][sy + k] != -1)
			erase(grid[sx + k - 1][sy + k]);
	}

	static void goRight(int sx, int sy, int k)
	{
		if(sy != 0)
			for(int i = sx; i < sx + k; ++i)
				if(grid[i][sy - 1] != -1)
					erase(grid[i][sy - 1]);
		for(int i = sx; i < sx + k; ++i)
			if(grid[i][sy] == -1)
				--cnt;
		if(sx > 0 && grid[sx - 1][sy] != -1)
			erase(grid[sx - 1][sy]);
		if(sx + k < N && grid[sx + k][sy] != -1)
			erase(grid[sx + k][sy]);
		++sy;
		if(sy + k < N)
			for(int i = sx; i < sx + k; ++i)
				if(grid[i][sy + k] != -1)
					add(grid[i][sy + k]);
		for(int i = sx; i < sx + k; ++i)
			if(grid[i][sy + k - 1] == -1)
				++cnt;
		if(sx > 0 && grid[sx - 1][sy + k - 1] != -1)
			add(grid[sx - 1][sy + k - 1]);
		if(sx + k < N && grid[sx + k][sy + k - 1] != -1)
			add(grid[sx + k][sy + k - 1]);
	}

	static void goLeft(int sx, int sy, int k)
	{
		if(sy + k < N)
			for(int i = sx; i < sx + k; ++i)
				if(grid[i][sy + k] != -1)
					erase(grid[i][sy + k]);
		for(int i = sx; i < sx + k; ++i)
			if(grid[i][sy + k - 1] == -1)
				--cnt;
		if(sx > 0 && grid[sx - 1][sy + k - 1] != -1)
			erase(grid[sx - 1][sy + k - 1]);
		if(sx + k < N && grid[sx + k][sy + k - 1] != -1)
			erase(grid[sx + k][sy + k - 1]);
		--sy;
		if(sy != 0)
			for(int i = sx; i < sx + k; ++i)
				if(grid[i][sy - 1] != -1)
					add(grid[i][sy - 1]);
		for(int i = sx; i < sx + k; ++i)
			if(grid[i][sy] == -1)
				++cnt;
		if(sx > 0 && grid[sx - 1][sy] != -1)
			add(grid[sx - 1][sy]);
		if(sx + k < N && grid[sx + k][sy] != -1)
			add(grid[sx + k][sy]);
	}

	static class Scanner
	{
		StringTokenizer st;
		BufferedReader br;

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

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

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

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

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

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


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

public class Main {

	public static int mod = 1000000007,n,max_ans,cc_count;
	public static boolean visited[][],grid[][];
	//public static ArrayList<ArrayList<Node>> cc_list;
	//public static ArrayList<Node> cc;
	public static int size[];
	public static int dx[]={1,0,-1,0};
	public static int dy[]={0,1,0,-1};
	public static HashMap<Integer,Integer> map;
	public static void main(String[] args)throws IOException {
		// TODO Auto-generated method stub
		InputStream input = System.in;
		InputReader in = new InputReader(input);

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		 map = new HashMap<Integer,Integer>();
		 n = in.nextInt();
		int k = in.nextInt();
		max_ans = Integer.MIN_VALUE;
		 grid = new boolean[n][n];
		 size = new int[5001];
	//	cc_list = new ArrayList<ArrayList<Node>>();
		for(int i=0;i<n;i++)
		{
			String s = in.next();

			for(int j=0;j<n;j++)
				if(s.charAt(j)=='.')
					grid[i][j] = true;

		}

		visited= new boolean[n][n];
		 cc_count=0;
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
				if(!visited[i][j] && grid[i][j])
					{
				//	cc = new ArrayList<Node>();
					dfs(i,j);
				//	cc_list.add(cc);
					cc_count++;
					}

	//	for(int i=0;i<4;i++)
		//	System.out.println(size[i]);

		for(int i=0;i<k;i++)
			for(int j=0;j<k;j++)
			{

				if(!grid[i][j])
				continue;

				size[map.get(i*n+j)]--;

			}

		for(int i=0;i<=n-k;i++)
			{for(int j=0;j<=n-k;j++)
			{//System.out.println(i+" "+j);
				int curr_ans = k*k;

				//	for(int ii=0;ii<4;ii++)
				//		System.out.print(size[ii]+" ");
				//	System.out.println();
				Set<Integer> set = new HashSet<Integer>();

				if(i!=0)
				{
					for(int y=j;y<j+k;y++)
						if(grid[i-1][y])
							set.add(map.get(n*(i-1)+y));

				}
				if(i!=n-k)
				{

					for(int y=j;y<j+k;y++)
						if(grid[i+k][y])
							set.add(map.get(n*(i+k)+y));

				}

				if(j!=0)
				{
					for(int x=i;x<i+k;x++)
						if(grid[x][j-1])
							set.add(map.get(n*x+j-1));

				}
				if(j!=n-k)
				{
					for(int x=i;x<i+k;x++)
						if(grid[x][j+k])
							set.add(map.get(n*x+j+k));

				}

				Iterator<Integer> ir = set.iterator();

				while(ir.hasNext())
				{	int aa = ir.next();
			//	System.out.println(aa+" s "+size[aa]);
					curr_ans += size[aa];

				}

				max_ans = Math.max(max_ans, curr_ans);
			//	System.out.println(curr_ans);

			if(j==n-k)
			{
				for(int x=i;x<i+k;x++)
					for(int y=j;y<j+k;y++)
						if(grid[x][y])
							size[map.get(x*n+y)]++;

				//System.out.println("x");

			}
			else
			{
				for(int x=i;x<i+k;x++)
					{if(grid[x][j])
						size[map.get(x*n+j)]++;
					if(grid[x][j+k])
						size[map.get(x*n+j+k)]--;
					}

			}

		//	System.out.println(i+" i");
		//	for(int ii=0;ii<4;ii++)
		//		System.out.print(size[ii]+" ");
		//	System.out.println();

			}

			if(i!=n-k)
			for(int x=i+1;x<i+k+1;x++)
				for(int y=0;y<k;y++)
					if(grid[x][y])
						size[map.get(x*n+y)]--;
			//System.out.println(i+" i");

	}

		System.out.println(max_ans);

	}

	public static void dfs(int x,int y)
	{

		visited[x][y]=true;

		//cc.add(new Node(x,y));
		map.put(x*n + y, cc_count);
		size[cc_count]++;
		//System.out.println(x+" "+y);
		int x1=0,y1=0;

		for(int i=0;i<4;i++)
		{//System.out.println();
			x1=x+dx[i];
			y1 = y+ dy[i];

			if(x1<0 || x1>=n || y1<0 || y1 >= n)
				continue;

			if(grid[x1][y1] && !visited[x1][y1])
				dfs(x1,y1);

		}

	}

	static class Node{

		int x,y;

		Node(int x,int y)
		{
			this.x=x;this.y=y;
		}
	}

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

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

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

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

    }
}


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

public class Main {

	public static int mod = 1000000007,n,max_ans,cc_count;
	public static boolean visited[][],grid[][];
	//public static ArrayList<ArrayList<Node>> cc_list;
	//public static ArrayList<Node> cc;
	public static int size[];
	public static int dx[]={1,0,-1,0};
	public static int dy[]={0,1,0,-1};
	public static HashMap<Integer,Integer> map;
	public static void main(String[] args)throws IOException {
		// TODO Auto-generated method stub
		InputStream input = System.in;
		InputReader in = new InputReader(input);

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		 map = new HashMap<Integer,Integer>();
		 n = in.nextInt();
		int k = in.nextInt();
		max_ans = Integer.MIN_VALUE;
		 grid = new boolean[n][n];
		 size = new int[1501];
	//	cc_list = new ArrayList<ArrayList<Node>>();
		for(int i=0;i<n;i++)
		{
			String s = in.next();

			for(int j=0;j<n;j++)
				if(s.charAt(j)=='.')
					grid[i][j] = true;

		}

		visited= new boolean[n][n];
		 cc_count=0;
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
				if(!visited[i][j] && grid[i][j])
					{
				//	cc = new ArrayList<Node>();
					dfs(i,j);
				//	cc_list.add(cc);
					cc_count++;
					}

	//	for(int i=0;i<4;i++)
		//	System.out.println(size[i]);

		for(int i=0;i<k;i++)
			for(int j=0;j<k;j++)
			{

				if(!grid[i][j])
				continue;

				size[map.get(i*n+j)]--;

			}

		for(int i=0;i<=n-k;i++)
			{for(int j=0;j<=n-k;j++)
			{//System.out.println(i+" "+j);
				int curr_ans = k*k;

				//	for(int ii=0;ii<4;ii++)
				//		System.out.print(size[ii]+" ");
				//	System.out.println();
				Set<Integer> set = new HashSet<Integer>();

				if(i!=0)
				{
					for(int y=j;y<j+k;y++)
						if(grid[i-1][y])
							set.add(map.get(n*(i-1)+y));

				}
				if(i!=n-k)
				{

					for(int y=j;y<j+k;y++)
						if(grid[i+k][y])
							set.add(map.get(n*(i+k)+y));

				}

				if(j!=0)
				{
					for(int x=i;x<i+k;x++)
						if(grid[x][j-1])
							set.add(map.get(n*x+j-1));

				}
				if(j!=n-k)
				{
					for(int x=i;x<i+k;x++)
						if(grid[x][j+k])
							set.add(map.get(n*x+j+k));

				}

				Iterator<Integer> ir = set.iterator();

				while(ir.hasNext())
				{	int aa = ir.next();
			//	System.out.println(aa+" s "+size[aa]);
					curr_ans += size[aa];

				}

				max_ans = Math.max(max_ans, curr_ans);
			//	System.out.println(curr_ans);

			if(j==n-k)
			{
				for(int x=i;x<i+k;x++)
					for(int y=j;y<j+k;y++)
						if(grid[x][y])
							size[map.get(x*n+y)]++;

				//System.out.println("x");

			}
			else
			{
				for(int x=i;x<i+k;x++)
					{if(grid[x][j])
						size[map.get(x*n+j)]++;
					if(grid[x][j+k])
						size[map.get(x*n+j+k)]--;
					}

			}

		//	System.out.println(i+" i");
		//	for(int ii=0;ii<4;ii++)
		//		System.out.print(size[ii]+" ");
		//	System.out.println();

			}

			if(i!=n-k)
			for(int x=i+1;x<i+k+1;x++)
				for(int y=0;y<k;y++)
					if(grid[x][y])
						size[map.get(x*n+y)]--;
			//System.out.println(i+" i");

	}

		System.out.println(max_ans);

	}

	public static void dfs(int x,int y)
	{

		visited[x][y]=true;

		//cc.add(new Node(x,y));
		map.put(x*n + y, cc_count);
		size[cc_count]++;
		//System.out.println(x+" "+y);
		int x1=0,y1=0;

		for(int i=0;i<4;i++)
		{//System.out.println();
			x1=x+dx[i];
			y1 = y+ dy[i];

			if(x1<0 || x1>=n || y1<0 || y1 >= n)
				continue;

			if(grid[x1][y1] && !visited[x1][y1])
				dfs(x1,y1);

		}

	}

	static class Node{

		int x,y;

		Node(int x,int y)
		{
			this.x=x;this.y=y;
		}
	}

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

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

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

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

    }
}


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

public class Main {

	final static int mod = 1_000_000_007;

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

		STDIN scan = new STDIN();
		int n = scan.nextInt(), m = scan.nextInt();
		int[] dp = new int[n+1];
		dp[1] = 2 % m;
		for(int i = 2; i <= n; i++)
			dp[i] = ((((dp[i-1] + dp[i-1]) % m) + dp[i-1]) % m) + 2;
		System.out.println(dp[n]);
	}

	static class STDIN {
		BufferedReader br;
		StringTokenizer st;

		public STDIN() {
			br = new BufferedReader(new InputStreamReader(System.in));
			st = null;
		}

		boolean hasNext() throws Exception {
			if(!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
			return st.hasMoreTokens();
		}

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

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

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

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

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


import java.util.*;

public class A{
 public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
   long N = sc.nextLong(), M = sc.nextLong();
   long r = (pow((long)3, N, M)+2*M-1)%M;
   System.out.println(r);
 }

 static long pow(long a, long p, long M){
    if(p==0) return 1L;
    if(p==1) return a%M;
    long sq = pow(a, p/2);
    return p%2==0? (sq*sq)%M: (a*(sq*sq)%M)%M;
 }

}


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

public class Main {
	BufferedReader in;
	PrintStream out;
	StringTokenizer tok;
	Scanner sc;

	public Main() throws NumberFormatException, IOException {
		//sc = new Scanner(System.in);
		//sc = new Scanner(new FileReader("in.txt"));
		in = new BufferedReader(new InputStreamReader(System.in));
	    //in = new BufferedReader(new FileReader("in.txt"));
		out = System.out;
		run();
	}

	void run() throws NumberFormatException, IOException {

		long n = nextInt();
		long m = nextInt();
		out.println(Pot(n,m) - 1);
	}

	long Pot(long n, long mod)
	{
		if(n == 0)return 1;
		if(n % 2 == 0)
		{
			long p = Pot(n/2, mod)%mod;
			return (p * p)%mod;
		}
		else return (Pot(n-1, mod) * 3)%mod;
	}

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

	String nextToken() throws IOException {
		String line = "";
		if (tok == null || !tok.hasMoreTokens())
		{
			if ((line = in.readLine()) != null)
				tok = new StringTokenizer(line);
			else return null;
		}
		return tok.nextToken();
	}

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

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

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


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

public class A {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String n, m;
        n= in.next();
        m= in.next();
        BigInteger aux = new BigInteger("3");
        aux=aux.modPow(new BigInteger(n), new BigInteger(m));
        aux=aux.subtract(BigInteger.ONE);
        System.out.println(aux);
    }

}


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

public class EC32P4
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt(); //number of fixed spots

        long counter = 1;

        if(k >= 2){
            counter += (n*(n-1)/2);
        }

        if(k >= 3){
            counter += (n*(n-1)*(n-2)/3);
        }

        if(k >= 4){
            counter += (3*n*(n-1)*(n-2)*(n-3)/8);
        }

        System.out.println(counter);
    }
    }

}


public class Permutation
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt(); //number of fixed spots

        int counter = 1;

        if(k >= 2){
            counter += n*(n-1)/2
        }

        if(k >= 3){
            counter += n*(n-1)*(n-2)/3
        }

        if(k >= 4){
            counter += 3*n*(n-1)*(n-2)*(n-3)/8
        }

        SYstem.out.println(counter);
    }
}


import java.io.*;

public class Solution {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt(), k = input.nextInt();

        long res = 1;
        if (k >= 2) res += n*(n-1)/2;
        if (k >= 3) res += n*(n-1)*(n-2)/3;
        if (k >= 4) res += n*(n-1)*(n-2)*(n-3)*3/8;
    }
}


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

public class Main {

    public static void main(String[] args) {
        // write your code here
        Scanner in=new Scanner(System.in);
        long n=in.nextLong();
        int k=in.nextInt();
        int []ap=new int[n];
        long ans=1;
        if(k>=2)
        {
           ans+=n*(n-1)/2;
        }
        if(k>=3)
        {
            ans+=n*(n-1)*(n-2)/3;
        }
        if(k>=4)
        {
            ans+=3*n*(n-1)*(n-2)*(n-3)/8;
        }
        System.out.println(ans);
    }
}


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

public class Test{

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

        InputReader in = new InputReader(System.in);
    	PrintWriter pw = new PrintWriter(System.out);
        int n=in.readInt();
        int q=in.readInt();
     	HashMap<String,String> map =new HashMap<String,String>();
    	for(int i = 0; i < q; i++) {
    		map.put(in.readString(), in.readString());
    	}
    	for (Map.Entry<String, String> entry : map.entrySet()) {
            pw.println(entry.getKey() + "/" + entry.getValue());
        }
    	pw.close();
    }
}


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

public class Test{

    public static void main(String[] args) throws Exception{
        InputReader in = new InputReader(System.in);
    	PrintWriter pw = new PrintWriter(System.out);
        int n=in.readInt();
        int q=in.readInt();
     	HashMap<String,String> map =new HashMap<String,String>();
    	for(int i = 0; i < q; i++;) {
    		map.put(in.readString(), in.readString());
    	}
    	for (Map.Entry<String, String> entry : map.entrySet()) {
            pw.println(entry.getKey() + "/" + entry.getValue());
        }
    	pw.close();
    }
}


import java.util.Scanner;
//~O(nQ)
public class BearAndCompressing {
	public static void main(String[] raggs)
	{
		char[][] replacementArr = new char[6][36];

		int[] count = new int[6];
		int[] temp = new int[6];

		//System.out.println("gfim");
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int q = in.nextInt();

		for(int i =0 ; i < 6; i++)
		{
			for(int j = 0; j < 36; j++)
			{
				replacementArr[i][j] = '0';
			}
		}

		in.nextLine();

		for(int i = 0; i < q; i++)
		{
			String line;
			char twoletter,oneletter;

			line = in.nextLine();

			twoletter = line.charAt(0);
			oneletter = line.charAt(3);

			for(int j = 0; j < 36; j++)
			{
				if(replacementArr[(int)(oneletter-'a')][j] == '0')
				{
				   replacementArr[(int)(oneletter-'a')][j] = twoletter;
				   break;
				}
			}
		}

		//start with one 'a'
		count[0] = 1;

		for(int i = 0; i < n-1; i++)
		{
			//for every cell in the array:
			for(int j = 0; j < 6; j++)
			{
				int occurances = count[j];
				//System.out.println(j + " " + occurances);
				for(int k = 0; k < 36; k++)
				{
					//Replace j with {replacement[j][k] for every k}
					char replaceWith = replacementArr[j][k];
					if(replaceWith == '0') break;

					int rw = (int)(replaceWith-'a');
					temp[rw] += occurances;
				}
			}
			//Now push temp ->count, and delete temp
			for(int x = 0; x < 6; x++)
		    {
				count[x] = temp[x];
				temp[x] = 0;
		    }

		}

		int sum = 0;

		for(int i = 0; i < 6; i++) sum += count[i];
		System.out.println(sum);
		/*
		for(int i =0 ; i < 6; i++)
		{
			for(int j = 0; j < 36; j++)
			{
				System.out.print(replacementArr[i][j] + " ");
			}
			System.out.println();
		}*/
	}
}


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

public class Solution{
     static Map<String , List<String>> map = new HashMap<>();

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

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

        int res = 0;

        while(q > 0){

            String a = sc.next();
            String b = sc.next();

            if(!map.containsKey(b)){
                List<String> temp = new ArrayList<>();
                temp.add(a);

                map.put(b,temp);
            }
            else{
                List<String> temp = map.get(b);
                temp.add(a);
            }
            q -= 1;
        }

        List<String> base = map.get("a");

        for(String s : base) {
            res += helper(s , n);
        }

        System.out.println(res);
      }

      public static int helper(String str , int n){

          if(str.length() == n){
              return 1;
          }
          int res = 0;

          String curr = Character.toString(str.charAt(0));

          if(!map.containsKey(curr)){
              return 0;
          }
          List<String> temp = map.get(curr);

          StringBuilder sb = new StringBuilder();

          for(String s : temp) {

              sb.append(s);
              sb.append(str.substring(1));

              res += helper(sb.toString() , n);

          }

          return res;
      }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import static java.lang.Math.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.StringTokenizer;

/**
 *
 * @author pttrung
 */
public class C {

    public static double Epsilon = 1e-6;
    public static long x, y, d;

    public static void main(String[] args) {
        Scanner in = new Scanner();
        PrintWriter out = new PrintWriter(System.out);
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File("output.txt")));
        HashMap<String, Score> map = new HashMap();
        HashSet<String> op = new HashSet();
        Score[] data = new Score[4];
        Score ber = null;
        for (int i = 0; i < 5; i++) {
            String a = in.next();
            String b = in.next();
            String[] score = in.next().split(":");
            int c = Integer.parseInt(score[0]);
            int d = Integer.parseInt(score[1]);

            if (!map.containsKey(a)) {
                map.put(a, new Score(a, 0, 0, 0));
            }
            if (!map.containsKey(b)) {
                map.put(b, new Score(b, 0, 0, 0));
            }
            if (a.equals("BERLAND")) {
                op.add(b);
            }
            if (b.equals("BERLAND")) {
                op.add(a);
            }

            Score tempA = map.get(a);
            tempA.score += c;
            tempA.miss += d;
            Score tempB = map.get(b);
            tempB.score += d;
            tempB.miss += c;
            if (c > d) {

                tempA.point += 3;

            } else if (c == d) {
                tempA.point += 1;
                tempB.point += 1;
            } else {
                tempB.point += 3;
            }
            map.put(a, tempA);
            map.put(b, tempB);
        }
        int index = 0;
        Score other = null;
        for (String name : map.keySet()) {
            if (!name.equals("BERLAND")) {
                if (!op.contains(name)) {
                    other = map.get(name);
                }

            } else {
                ber = map.get(name);
            }
            data[index++] = map.get(name);
        }
        ber.score += 1;
        ber.point += 3;

        other.miss += 1;
        Arrays.sort(data);
      //  System.out.println(ber.score + " "+ data[2].score);
        if (ber.point < data[2].point) {
            out.println("IMPOSSIBLE");
        } else {
            int left = 1;
            int right = 0;
            while (!data[2].name.equals("BERLAND")) {
                int difBer = ber.score - ber.miss;
                int difSe = data[2].score - data[2].miss;
                int need = difSe - difBer;
                if (need == 0) {
                    if (data[2].score > ber.score && !data[2].name.equals(other.name)) {
                        int more = data[2].score - ber.score + 1;
                        left += more;
                        ber.score += more;
                        other.miss += more;
                    } else {
                        ber.score += 1;
                        left++;
                        right++;
                        other.miss++;
                    }

                } else {
                    if (!data[2].name.equals(other.name)) {
                        int more = need;
                        int miss = 0;
                        while(data[2].score > ber.score + more){
                            more++;
                            miss++;
                        }
                        if(data[2].name.compareTo(ber.name) < 0){
                            more++;
                            miss++;
                        }
                        left += more;
                        right += miss;
                        ber.score += more;
                        ber.miss += miss;
                        other.miss += more;
                        other.score += miss;
                    } else {
                        int more = need + 1;
                        int a = more / 2;
                        if (more % 2 != 0) {
                            a++;
                        }
                        left += a;
                        ber.score += a;
                        other.miss += a;
                    }
                }
                Arrays.sort(data);
            }
            out.println(left + ":" + right);

        }
        // System.out.println(second.point);

        out.close();
    }

    public static class Score implements Comparable<Score> {

        String name;
        int score, miss;
        int point;

        public Score(String name, int score, int miss, int point) {
            this.name = name;
            this.score = score;
            this.miss = miss;
            this.point = point;
        }

        @Override
        public int compareTo(Score o) {
            if (point != o.point) {
                return point - o.point;
            } else {
                int a = score - miss;
                int b = o.score - o.miss;
                if (a != b) {
                    return a - b;
                } else if (score != o.score) {
                    return score - o.score;
                } else {
                    return -name.compareTo(o.name);
                }
            }
        }
    }

    public static Pair cal(ArrayList<Integer>[] map, HashSet<Integer> result, HashSet<Pair> check, int pa, int node) {
        boolean taken = false;
        if (check.contains(new Pair(pa, node)) || check.contains(new Pair(node, pa))) {
            taken = true;
        }
        boolean leaf = true;

        ArrayList<Integer> list = new ArrayList();
        Pair store = null;
        for (int next : map[node]) {
            if (next != pa) {
                leaf = false;

                Pair p = cal(map, result, check, node, next);
                if (p.y == 1) {
                    store = p;
                    taken = false;

                } else {
                    if (store == null) {
                        store = p;
                    }
                }
            }
        }
        if (leaf) {
            if (taken) {
                result.add(node);
                return new Pair(node, 1);
            } else {
                return new Pair(node, 0);
            }
        }
        if (taken) {
            result.add(store.x);
            return new Pair(store.x, 1);
        }
        return store;

    }

    public static class Pair {

        int x, y;

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

        @Override
        public boolean equals(Object o) {
            if (o instanceof Pair) {
                Pair other = (Pair) o;
                return x == other.x && y == other.y;
            }
            return false;
        }

        @Override
        public int hashCode() {
            int hash = 7;
            hash = 29 * hash + this.x;
            hash = 29 * hash + this.y;
            return hash;
        }
    }

    public static void extendEuclid(long a, long b) {
        if (b == 0) {
            x = 1;
            y = 0;
            d = a;
            return;
        }
        extendEuclid(b, a % b);
        long x1 = y;
        long y1 = x - (a / b) * y;
        x = x1;
        y = y1;

    }

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

    }

    static class Line {

        double a, b, c;

        Line(double x1, double y1, double x2, double y2) {
            if (Math.abs(x1 - x2) < Epsilon && Math.abs(y1 - y2) < Epsilon) {
                throw new RuntimeException("Two point are the same");
            }
            a = y1 - y2;
            b = x2 - x1;
            c = -a * x1 - b * y1;
        }

        Line(Point x, Point y) {
            this(x.x, x.y, y.x, y.y);
        }

        public Line perpendicular(Point p) {
            return new Line(p, new Point(p.x + a, p.y + b));
        }

        public Point intersect(Line l) {
            double d = a * l.b - b * l.a;
            if (d == 0) {
                throw new RuntimeException("Two lines are parallel");
            }

            return new Point((l.c * b - c * l.b) / d, (l.a * c - l.c * a) / d);
        }
    }

    static void check(Point a, Point b, ArrayList<Point> p, Point[] rec, int index) {
        for (int i = 0; i < 4; i++) {
            int m = (i + index) % 4;
            int j = (i + 1 + index) % 4;
            Point k = intersect(minus(b, a), minus(rec[m], rec[j]), minus(rec[m], a));
            if (k.x >= 0 && k.x <= 1 && k.y >= 0 && k.y <= 1) {
                Point val = new Point(k.x * minus(b, a).x, k.x * minus(b, a).y);
                p.add(add(val, a));
                // System.out.println(a + " " + b + " " + rec[i] + " " + rec[j]);
                // System.out.println(add(val, a));
            }
        }
    }

    static Point intersect(Point a, Point b, Point c) {
        double D = cross(a, b);
        if (D != 0) {
            return new Point(cross(c, b) / D, cross(a, c) / D);
        }
        return null;
    }

    static Point convert(Point a, double angle) {
        double x = a.x * cos(angle) - a.y * sin(angle);
        double y = a.x * sin(angle) + a.y * cos(angle);
        return new Point(x, y);
    }

    static Point minus(Point a, Point b) {
        return new Point(a.x - b.x, a.y - b.y);
    }

    static Point add(Point a, Point b) {
        return new Point(a.x + b.x, a.y + b.y);
    }

    static double cross(Point a, Point b) {
        return a.x * b.y - a.y * b.x;

    }

    static class Point {

        double x, y;

        Point(double x, double y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public String toString() {
            return "Point: " + x + " " + y;
        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public String next() {

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

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

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

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

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Scanner;

public class Main {

    static class Team{
        String name;
        int id;
        int winP;
        int goal;
        int missgoal;
        int rank=0;

        Team(int id,String name,
        int winP,int minssgoal,
        int goal){
            this.id=id;
            this.name=name;
            this.winP=winP;
            this.goal=goal;
            this.missgoal=minssgoal;
        }
    }

    int id=0;

    HashMap<String,Team> teams;

    boolean[][] game=new boolean[4][4];
    List<Team> teamlist;
    public void solve() {
        Scanner sc = new Scanner(System.in);
        for(int i=0;i<4;i++){
            game[i][i]=true;
        }
        teams=new HashMap<String, Main.Team>();
        for (int i = 0; i < 5; i++) {
            String name1=sc.next();
            String name2=sc.next();

            String param=sc.next();
            int p1=param.charAt(0)-'0';
            int p2=param.charAt(2)-'0';

            Team t1,t2;
            if(!teams.containsKey(name1)){
                t1=new Team(id++,name1,0,0,0);
                teams.put(name1,t1);
            }else{
                t1=teams.get(name1);
            }
            if(!teams.containsKey(name2)){
                t2=new Team(id++,name2,0,0,0);
                teams.put(name2,t2);
            }else{
                t2=teams.get(name2);
            }

            game[t1.id][t2.id]=true;
            game[t2.id][t1.id]=true;
            t1.goal+=p1;
            t2.goal+=p2;
            t1.missgoal+=p2;
            t2.missgoal+=p1;
            if(p1<p2){
                t2.winP+=3;
            }else if(p1> p2){
                t1.winP+=3;
            }else{
                t1.winP++;
                t2.winP++;
            }
        }

        teamlist=new ArrayList<Team>();
        for(Entry<String,Team> team:teams.entrySet()){
            teamlist.add(team.getValue());
        }

        refreashRank();

        Team berand=null;
        for(Team team:teamlist){
            if(team.name.equals("BERLAND")){
                berand=team;
                break;
            }
        }

        for(int i=0;i<4;i++)    for(int j=0;j<4;j++)
            pr(game[i][j]);

        int lastgameid=0;

        Team lastGame=teamlist.get(lastgameid-1);

        berand.winP+=3;
        int mx=Integer.MAX_VALUE;int my=0;
        for(int x=0;x<=9;x++){
            for(int y=0;y<x;y++){
                berand.goal+=x;
                lastGame.goal+=y;
                berand.missgoal+=y;
                lastGame.missgoal+=x;

                refreashRank();
                if(berand.rank<=1){
                    if(mx-my>=x-y && y<=my){
                        mx=x;
                        my=y;
                    }

                }
                if(x==6 && y==0){

                }
                berand.goal-=x;
                lastGame.goal-=y;
                berand.missgoal-=y;
                lastGame.missgoal-=x;
            }
        }

        if(mx==0 && my==0){
            pr("IMPOSSIBLE");
        }else{
            pr(mx+":"+my);
        }
    }

    public void  refreashRank(){
        Collections.sort(teamlist,new Comparator<Team>() {
            @Override
            public int compare(Team o1, Team o2) {
                if(o1.winP!=o2.winP){
                    return o2.winP-o1.winP;
                }
                if(o1.goal-o1.missgoal!=o2.goal-o2.missgoal){
                    return o2.goal-o2.missgoal-(o1.goal-o1.missgoal);
                }
                if(o1.goal!=o2.goal){
                    return o2.goal-o1.goal;
                }
                return o2.name.compareTo(o1.name);
            }
        });
        int rank=0;
        for(Team team:teamlist){
            team.rank=rank;
            rank++;
        }
    }

    public void run() {
        solve();
    }

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

    public static void pr() {
        System.out.println();
    }

    public static void pr(Object o) {
        System.out.println(o);
    }

}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;

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

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

	class State{

	}

	// solution
	private void solve() throws Throwable {

		int n = iread();
		Hashtable[] tables = new Hashtable[n];

		out.flush();
	}

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

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

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

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

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

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

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

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

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

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

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

	enum CompetitionType {CF, OTHER};
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Collections;
import java.util.ArrayList;

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

    static class TaskC {
        public HashMap<String, Team> hm;
        public ArrayList<Team> teams;

        public void add(String name, int goal, int missed) {

            if (hm.containsKey(name)) {
                hm.get(name).update(goal, missed);
            } else {
                hm.put(name, new Team(name, goal, missed));
            }
        }

        public void solve(int testNumber, Scanner in, PrintWriter out) {

            hm = new HashMap<>();

            for (int i = 0; i < 5; ++i) {
                String A, B;
                int a, b;
                A = in.next();
                B = in.next();
                String[] temp = in.next().split(":");
                a = Integer.parseInt(temp[0]);
                b = Integer.parseInt(temp[1]);
                add(A, a, b);
                add(B, b, a);

            }
            teams = new ArrayList<>(hm.values());
            Collections.sort(teams);
            int current = 3;
            Team berland = teams.get(3);
            Team other = teams.get(3);
            String s_other;
            for (int i = 0; i < 3; ++i) {
                if (teams.get(i).name.equals("BERLAND")) {
                    current = i;
                    berland = teams.get(i);
                } else if (teams.get(i).played == 2) {
                    other = teams.get(i);
                }
            }

            s_other = other.name;

            if (berland.point + 3 < teams.get(1).point) {
                out.println("IMPOSSIBLE");
                return;
            }

            for (int diff = 1; diff <= 20; ++diff) {
                for (int goals = diff; goals <= 20; ++goals) {
                    int mis = goals - diff;
                    berland.update(goals, mis);
                    other.update(mis, goals);
                    Collections.sort(teams);
                    for (int i = 0; i < 2; ++i) {
                        if (teams.get(i).name.equals("BERLAND")) {
                            out.println(goals + ":" + mis);
                            return;
                        }

                    }
                    berland.backup();
                    other.backup();
                }
            }

            out.println("IMPOSSIBLE");

        }

        class Team implements Comparable<Team> {
            public String name;
            public int played;
            public int point;
            public int goals;
            public int missed;
            public int t_point;
            public int t_goals;
            public int t_missed;

            public void backup() {
                goals -= t_goals;
                missed -= t_missed;
                point -= t_point;
            }

            public String toString() {
                return (name + "   " + point + "   " + goals + "   " + missed);
            }

            public void update(int goals, int missed) {
                if (goals > missed) {
                    point += 3;
                    t_point = 3;
                } else if (goals == missed) {
                    point += 1;
                    t_point = 1;
                } else {
                    t_point = 0;
                }
                this.goals += goals;
                this.missed += missed;
                t_goals = goals;
                t_missed = missed;
                ++played;

            }

            public Team(String name, int goals, int missed) {
                this.name = name;
                update(goals, missed);
            }

            public Team() {

            }

            public int hashCode() {
                return name.hashCode();
            }

            public boolean equals(Object obj) {
                return name.equals(obj);
            }

            public int compareTo(Team o) {
                int temp = o.point - point;
                if (temp != 0) {
                    return temp;
                }
                temp = (o.goals - o.missed) - (goals - missed);
                if (temp != 0) {
                    return temp;
                }
                temp = (o.goals - goals);
                if (temp != 0) {
                    return temp;
                }

                return name.compareTo(o.name);

            }

        }

    }

    static
    @Documented
    @Retention(RetentionPolicy.CLASS)
    @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
    interface NotNull {
    }
}


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

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

class CF_55D {

	void run() {
		Scanner cin = new Scanner(new BufferedInputStream(System.in));
		PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));
		int t = cin.nextInt() ;
		while(t-- > 0){
			long l = cin.nextLong() , r = cin.nextLong() ;
			cout.println(calculation(r) - calculation(l - 1));
		}
        cout.flush();
	}

	long calculation(long val){
		int bit = 0 ;
		while(val > 0){
			dight[bit++] = (int) (val % 10) ;
			val /= 10 ;
		}
		return dfs(bit-1, 0, 1, true) ;
	}

	long dfs(int pos, int sum, int lcm, Boolean isEnd) {
		if (pos < 0)
			return sum % lcm == 0 ? 1L : 0L;
		if (!isEnd && dp[pos][sum][hash[lcm]] != -1)
			return dp[pos][sum][hash[lcm]];
		int end = isEnd ? dight[pos] : 9;
		long res = 0L;
		for (int i = 0; i <= end; i++) {
			res += dfs(pos - 1, (sum * 10 + i) % N, lcm(lcm, i), isEnd && i == end);
		}
        if(! isEnd) dp[pos][sum][hash[lcm]] = res ;
        return res ;
	}

	final int N = 2520;
	long[][][] dp = new long[18][N][48];
	int[] dight = new int[18];

	int[] hash = new int[N + 8];
	{
		int cnt = 0;
		for (int i = 1; i <= N; i++) {
			if (N % i == 0)
				hash[i] = cnt++;
		}
		for(int i = 0 ; i < 18 ; i++){
			for(int j = 0 ; j < N ; j++)
				Arrays.fill(dp[i][j], -1) ;
		}
	}

	int gcd(int x, int y) {
		return y == 0 ? x : gcd(y, x % y);
	}

	int lcm(int x, int y) {
		if(x == 0) return y ;
		if(y == 0) return x ;
		return x / gcd(x, y) * y;
	}

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Map;
import java.util.Scanner;
import java.util.HashMap;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author heifrank
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        int testCount = Integer.parseInt(in.next());
        for (int i = 1; i <= testCount; i++)
            solver.solve(i, in, out);
        out.close();
    }

    static class TaskD {
        long[][][] dp = new long[20][50][3005];
        int[] bits = new int[20];
        Map<Integer, Integer> ds = new HashMap<>();
        int cnt = 0;
        final int MOD = 2520;

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

        void dfs2(int p, int pren) {
            if (p == 0) {
                if (!ds.containsKey(pren)) {
                    ds.put(pren, ++cnt);
                }
                return;
            }
            dfs2(p - 1, pren * p / gcd(pren, p));
            dfs2(p - 1, pren);
        }

        public TaskD() {
            for (int i = 0; i < dp.length; i++)
                for (int j = 0; j < dp[0].length; j++) {
                    Arrays.fill(dp[i][j], -1);
                }
            dfs2(9, 1);
        }

        public long dfs(int p, int lcm, int mod, boolean limit) {
            if (p == -1) {
                return mod % lcm == 0 ? 1 : 0;
            }
            int id = ds.get(lcm);
            if (!limit && dp[p][id][mod] != -1) {
                return dp[p][id][mod];
            }
            long ans = 0;
            int end = limit ? bits[p] : 9;
            for (int i = 0; i <= end; i++) {
                ans += dfs(p - 1, i == 0 ? lcm : lcm * i / gcd(lcm, i), (mod * 10 + i) % MOD, limit && i == end);
            }
            if (!limit) {
                dp[p][lcm][mod] = ans;
            }
            return ans;
        }

        public long calc(long n) {
            int len = 0;
            while (n > 0) {
                Long t = n % 10;
                bits[len++] = t.intValue();
                n /= 10;
            }
            return dfs(len - 1, 1, 0, true);
        }

        public void solve(int testNumber, Scanner in, PrintWriter out) {
            long x = in.nextLong();
            long y = in.nextLong();
            out.println(calc(y) - calc(x - 1));
        }

    }
}


import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

//  WeekOfCode21

public class Solution {
	public static InputReader in;
	public static PrintWriter out;
	public static Random ra = new Random(0);

	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		in = new InputReader(inputStream);
//		out = new PrintWriter(outputStream, true);
		out = new PrintWriter(outputStream, false); // enable this for ludicrous speed

		beautifulNumbers();
//		testBM();
//		borrowingMoney();
//		lazySorting();
//		testLazySorting();
//		luckBalance();
//		kangaroo();

		out.close();
	}

	static void beautifulNumbers() {
		int cnt = 0;
		for(int i=1; i<=MOD; i++)
			if(MOD%i == 0)
				Hash[i] = cnt++;

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

		int ts = in.nextInt();
		for (int t = 0; t < ts; t++) {
			long a = in.nextInt();
			long b = in.nextInt();
			System.out.println((solve(b) - solve(a-1)));
		}
	}

	static final int MOD = 2520;
	static long[][][] dp = new long[20][50][2550];
	static int[] dis = new int[20];
	static int[] Hash = new int[2550];

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

	static long dfs(int len, int num, int lcm, boolean flag) {
		if(-1==len) return (num%lcm == 0) ? 1: 0;
		if(!flag && -1!=dp[len][Hash[lcm]][num]) {
			return dp[len][Hash[lcm]][num];
		}
		long ans = 0;
		int end = flag?dis[len]:9;
		for(int i=0; i<=end; i++)
			ans += dfs(len-1, (num*10+i)%MOD, (int)(i > 0 ? lcm*i/gcd(lcm,i) : lcm), flag && i==end);
		if(!flag) dp[len][Hash[lcm]][num] = ans;
		return ans;
	}

	static long solve(long n) {
		int pos = 0;
		while(n > 0) {
			dis[pos++] = (int)n%10;
			n /= 10;
		}
		return dfs(pos-1, 0, 1, true);
	}

	public static void testBM() {
		long t1 = System.currentTimeMillis();
		for (int test = 0; test < 1000000; test++) {
			int N = ra.nextInt(10)+1;
			int M = ra.nextInt(N*(N-1)/2 +1);
			boolean[][] g = new boolean[N][N];
			int[] cost = new int[N];
			for (int i = 0; i < cost.length; i++) {
				cost[i] = ra.nextInt(100);
			}
			for (int i = 0; i < M && N > 1; i++) {
				int a = ra.nextInt(N);
				int b = ra.nextInt(N);
				while (a == b) {
					a = ra.nextInt(N);
					b = ra.nextInt(N);
				}
				g[a][b] = true;
				g[b][a] = true;
			}
			String r1 = borrowingMoneySlow(N, g, cost);
			String r2 = borrowingMoney(N, g, cost);
			if (!r1.equals(r2)) {
				System.out.println("fail");
				borrowingMoneySlow(N, g, cost);
				borrowingMoney(N, g, cost);
			}
		}
		System.out.println(System.currentTimeMillis() - t1);
	}

	public static void borrowingMoney() {
		int N = 0;
		boolean[][] g;
		int[] cost;

		N = in.nextInt();
		int m = in.nextInt();
		cost = in.nextIntArray(N);
		g = new boolean[N][N];

		for (int i = 0; i < m; i++) {
			int a = in.nextInt()-1;
			int b = in.nextInt()-1;
			g[a][b] = true;
			g[b][a] = true;
		}

		System.out.println(borrowingMoney(N, g, cost));
//		System.out.println(borrowingMoneySlow(N, g, cost));

	}

	public static String borrowingMoneySlow(int ns, boolean[][] g, int[] cost) {
		int len = ns;
		int N = 1 << len;
		int[] costF = new int[100*35 +1];
		for (int n = 0; n < N; n++) {
			boolean[] active = new boolean[len];
			for (int i = 0; i < len; i++) {
				if (((1 << i) & n) != 0) {
					active[i] = true;
				}
			}
			int sum = 0;
			boolean v = true;
			for (int i = 0; i < ns; i++) {
				if (!active[i]) {
					for (int j = 0; j < ns; j++) {
						if (g[i][j] && !active[j]) {
							v = false;
							break;
						}
					}
					sum += cost[i];
				}
				if (!v) {
					if (i >= 1) {
						System.currentTimeMillis();
					}
					break;
				}
			}
			if (v) {
				costF[sum]++;
			}
		}
		for (int i = costF.length-1; i >= 0; i--) {
			if (costF[i] > 0) {
				return i + " " + costF[i];
			}
		}
		return "";
	}

	public static String borrowingMoney(int N, boolean[][] g, int[] cost) {
		boolean[] visit;
		visit = new boolean[N];
		HashMap<Long, Long> F;
		F = new HashMap<>();

		long mx = 0;
		long tw = 1;
		for (int p = 0; p < visit.length; p++) {
			if (!visit[p]) {
				F = new HashMap<>();

				Queue<Integer> q = new LinkedList<>();
				q.add(p);
				boolean start = true;

				while (!q.isEmpty()) {
					int top = q.poll();

					if (!visit[top]) {
						visit[top] = true;
						HashMap<Long, Long> current = F;
						HashMap<Long, Long> oldF = new HashMap<>();
						for (Entry<Long, Long> e: current.entrySet()) {
							oldF.put(e.getKey(), e.getValue());
						}
						if (start) {
							start = false;

							// do nothing
							current.put(0L, 0L);

							// steal here
							current.put(1L << top, (long)cost[top]);
						} else {
							// steal here

							for (Entry<Long, Long> e: oldF.entrySet()) {
								if (!getBit(e.getKey(), top)) {
									boolean stillHere = true;
									for (int i = 0; i < N; i++) {
										// top will have left if someone robbed next door
										if (g[top][i] && getBit(e.getKey(), i)) {
											stillHere = false;
										}
									}
									if (stillHere) {
										long max = e.getValue();
										long fmask = e.getKey() | 1L << top;

										if (max > -1) {
											max += cost[top];
											if (!current.containsKey(fmask) || current.get(fmask) < max) {
												current.put(fmask, max);
											}
										}
									}
								}
							}
						}

						for (int i = 0; i < N; i++) {
							if (g[top][i]) {
								q.add(i);
							}
						}

						// If all of a edge's neighbours have been visited, it's removed from the searchable set
						// and the maximum of all of them are added back because they cannot influence future
						// events.

						for (Entry<Long, Long> e: oldF.entrySet()) {
							if (!getBit(e.getKey(), top)) {
								boolean stillHere = true;
								for (int i = 0; i < N; i++) {
									// top will have left if someone robbed next door
									if (g[top][i] && getBit(e.getKey(), i)) {
										stillHere = false;
									}
								}
								if (stillHere) {
									long max = e.getValue();
									long fmask = e.getKey() | 1L << top;

									if (max > -1) {
										max += cost[top];
										if (!current.containsKey(fmask) || current.get(fmask) < max) {
											current.put(fmask, max);
										}
									}
								}
							}
						}

					}
				}

				long max = -1;
				for (Entry<Long, Long> e: F.entrySet()) {
					if (e.getValue() > max) {
						max = e.getValue();
					}
				}
				long ways = 0;
				for (Entry<Long, Long> e: F.entrySet()) {
					if (e.getValue() == max) {
						ways++;
					}
				}
				mx += max;
				tw *= ways;
			}
		}

		//System.out.println(max + " " + ways + " " + Long.toBinaryString(fmask));

		return (mx + " " + tw);
	}

	public static String borrowingMoney2(int N, boolean[][] g, int[] cost) {
		boolean[] visit;
		visit = new boolean[N];
		HashMap<Long, Long> F;
		F = new HashMap<>();

		long mx = 0;
		long tw = 1;
		for (int i = 0; i < visit.length; i++) {
			if (!visit[i]) {
				F = new HashMap<>();

				dfs(true, i, N, g, visit, cost, F);

				long max = -1;
				long fmask = 0;
				for (Entry<Long, Long> e: F.entrySet()) {
					if (e.getValue() > max) {
						max = e.getValue();
						fmask = e.getKey();
					}
				}
				long ways = 0;
				for (Entry<Long, Long> e: F.entrySet()) {
					if (e.getValue() == max) {
						ways++;
					}
				}
				mx += max;
				tw *= ways;
			}
		}

		//System.out.println(max + " " + ways + " " + Long.toBinaryString(fmask));

		return (mx + " " + tw);
	}

	public static void dfs(boolean start, int top, int N, boolean[][] g, boolean[] visit, int[] cost, HashMap<Long, Long> F) {
		if (!visit[top]) {
			visit[top] = true;
			HashMap<Long, Long> current = F;
			HashMap<Long, Long> oldF = new HashMap<>();
			for (Entry<Long, Long> e: current.entrySet()) {
				oldF.put(e.getKey(), e.getValue());
			}
			if (start) {

				// do nothing
				current.put(0L, 0L);

				// steal here
				long fmask = 0;
				for (int i = 0; i < N; i++) {
					if (g[top][i]) {
						fmask |= 1L << i;
					}
				}
				current.put(fmask, (long)cost[top]);
			} else {
				// steal here

				for (Entry<Long, Long> e: oldF.entrySet()) {
					if (!getBit(e.getKey(), top)) {
						long max = e.getValue();
						long fmask = e.getKey();

						for (int i = 0; i < N; i++) {
							if (g[top][i]) {
								fmask |= 1L << i;
							}
						}
						if (max > -1) {
							max += cost[top];
							if (!current.containsKey(fmask) || current.get(fmask) < max) {
								current.put(fmask, max);
							}
//							if (current.get(fmask) == max) {
//								current.put(fmask, max);
//							}
						}
					}
				}
			}

			for (int i = 0; i < N; i++) {
				if (g[top][i]) {
					dfs(false, i, N, g, visit, cost, F);
				}
			}
		}
	}

	public static int clearBit(int x, int i) {
		return (x & ~(1 << i));
	}

	public static int setBit(int x, int i) {
		return (x | (1 << i));
	}

	public static boolean getBit(long x, int i) {
		return (x & (1L << i)) != 0;
	}

	public static void testLazySorting() {
		int[] a = new int[4];
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < a.length; j++) {
				a[j] = ra.nextInt(10);
			}
			double r1 = slowLazySorting(a.length, a);
			double r2 = lazySorting(a.length, a);
			int t = 0;
			while (Math.abs(r1 - r2) > 0.1 && t < 10) {
				t++;
				r1 = slowLazySorting(a.length, a);
				r2 = lazySorting(a.length, a);
			}
			if (t == 10) {
				System.out.println("fail");
			}
		}
	}

	public static void lazySorting() {
		int n = in.nextInt();
		int[] a = in.nextIntArray(n);
		System.out.println(String.format("%.6f", lazySorting(n, a)));
//		System.out.println(String.format("%.6f", slowLazySorting(n, a)));
	}

	public static double slowLazySorting(int n, int[] a) {
		ArrayList<Integer> b = new ArrayList<>();
		for (int i = 0; i < a.length; i++) {
			b.add(a[i]);
		}
		Arrays.sort(a);
		int sum = 0;
		int games = 1000000;
		for (int i = 0; i < games; i++) {
			boolean wrong = false;
			int time = 0;

			for (int j = 0; j < a.length; j++) {
				if (a[j] != b.get(j)) {
					wrong = true;
					break;
				}
			}

			while (wrong) {
				wrong = false;
				time++;
				Collections.shuffle(b);
				for (int j = 0; j < a.length; j++) {
					if (a[j] != b.get(j)) {
						wrong = true;
						break;
					}
				}
				if (!wrong) {
					sum += time;
				}
			}
		}
		return sum / (double)games;
	}

	public static double lazySorting(int n, int[] a) {
		int[] f = new int[101];
		int[] b = a.clone();
		Arrays.sort(b);
		if (Arrays.equals(a, b)) {
			return 0;
		}
		for (int i = 0; i < a.length; i++) {
			f[a[i]]++;
		}
		long c = 1;
		for (int i = 1; i <= n; i++) {
			c *= i;
		}
		for (int i = 0; i < f.length; i++) {
			while (f[i] > 0) {
				c /= f[i];
				f[i]--;
			}
		}
		double p = 1.0/c;
		return 1/p;
	}

	public static void luckBalance() {
		int n = in.nextInt();
		int k = in.nextInt();
		int[] luck = new int[n];
		int[] imp = new int[n];
		for (int i = 0; i < n; i++) {
			luck[i] = in.nextInt();
			imp[i] = in.nextInt();
		}
		System.out.println(luckBalance(n, k, luck, imp));
	}

	public static long luckBalance(int n, int k, int[] luck, int[] imp) {
		ArrayList<Integer> important = new ArrayList<>();
		long balance = 0;
		for (int i = 0; i < imp.length; i++) {
			if (imp[i] == 1) {
				important.add(luck[i]);
			} else {
				balance += luck[i];
			}
		}
		Collections.sort(important);
		Collections.reverse(important);
		for (int i = 0; i < important.size(); i++) {
			if (i < k) {
				balance += important.get(i);
			} else {
				balance -= important.get(i);
			}
		}
		return balance;
	}

	public static void kangaroo() {
		int x1 = in.nextInt();
		int v1 = in.nextInt();
		int x2 = in.nextInt();
		int v2 = in.nextInt();
		if (kangaroo(x1, v1, x2, v2)) {
			System.out.println("YES");
		} else {
			System.out.println("NO");
		}
	}

	public static boolean kangaroo(int x1, int v1, int x2, int v2) {
		int dx = x1-x2;
		int dv = v2-v1;
		if (dv == 0) {
			return dx == 0;
		}
		if (((Math.abs(dx) % Math.abs(dv)) == 0) && dx / (double)dv > 0) {
			return true;
		} else {
			return false;
		}
	}

	@SuppressWarnings("serial")
	public static class Counter<T> extends HashMap<T, Integer> {
		public Counter() {
			super();
		}
		public void add(T key) {
			this.add(key, 1);
		}
		public void add(T key, int count) {
			Integer i = this.get(key);
			if (i == null) {
				this.put(key, count);
			} else {
				this.put(key, i+count);
			}
		}
	}

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

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

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

		public int nextInt() {return Integer.parseInt(next());}
		public long nextLong() {return Long.parseLong(next());}
		public double nextDouble() {return Double.parseDouble(next());}
		public long[] nextLongArray(int n) {
			long[] a = new long[n];
			for (int i = 0; i < a.length; i++) a[i] = this.nextLong();
			return a;
		}
		public int[] nextIntArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < a.length; i++) a[i] = this.nextInt();
			return a;
		}
	}
}


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

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

class D {
    final int MAX = 2520;
    long[][][] memo = new long[19][48][MAX];
    int[] idx = new int[MAX + 1];
    int[] digit = new int[19];
    public void solve(int testNumber, PandaScanner in, PrintWriter out) {

        for (int i = 0; i < 19; i++) {
            for (int j = 0; j < 48; j++) {
                Arrays.fill(memo[i][j], -1);
            }
        }
        for (int i = 1; i < MAX + 1; i++) {
            idx[i] = idx[i - 1] + (MAX % i == 0 ? 1 : 0);
        }

        int t = in.nextInt();
        for (int i = 0; i < t; i++) {
            long l = in.nextLong();
            long r = in.nextLong();
            out.println(getBeautiful(r) - getBeautiful(l - 1));
        }
    }

    public long getBeautiful(long x) {
        int length = 0;
        for (; x > 0; length++) {
            digit[length] = (int) (x % 10);
            x /= 10;
        }
        return recurse(length - 1, 1, 0, false);
    }

    public long recurse(int pow, int lcm, int mod, boolean under) {
        if (pow == -1) {
            return mod % lcm == 0 ? 1 : 0;
        }
        if (under && memo[pow][idx[lcm]][mod] > -1) {
            return memo[pow][idx[lcm]][mod];
        }
        int limit = under ? 9 : digit[pow];
        long res = 0;
        for (int i = 0; i <= limit; i++) {
            res += recurse(pow - 1, i < 2 ? lcm : (i * lcm / gcd(i, lcm)), (mod * 10 + i) % MAX, under || i < limit);
        }
        return under ? memo[pow][idx[lcm]][mod] = res : res;
    }

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

class PandaScanner {
    public BufferedReader br;
    public StringTokenizer st;
    public InputStream in;

    public PandaScanner(InputStream in) {
        br = new BufferedReader(new InputStreamReader(this.in = in));
    }

    public String nextLine() {
        try {
            return br.readLine();
        }
        catch (Exception e) {
            return null;
        }
    }

    public String next() {
        if (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(nextLine().trim());
            return next();
        }
        return st.nextToken();
    }

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

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

    }


import static java.lang.Math.*;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
import static java.lang.System.arraycopy;
import static java.util.Arrays.sort;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.fill;
import java.util.*;
import java.awt.geom.QuadCurve2D;
import java.io.*;

public class Main {

	public static void main(String[] args) throws IOException {
		try {
			if (new File("input.txt").exists())
				System.setIn(new FileInputStream("input.txt"));
		} catch (SecurityException e) {
		}
		new Thread(null, new Runnable() {
			public void run() {
				try {
					long time1 = System.currentTimeMillis();
					new Main().run();
					long time2 = System.currentTimeMillis();
					System.err.println("Time " + (time2 - time1) + " ms");
				} catch (Throwable e) {
					e.printStackTrace();
					exit(999);
				}
			}
		}, "1", 1 << 23).start();
	}

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

	private void run() throws IOException {
		in = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		in.close();
		out.close();
	}

	private void solve() throws IOException {
		long n = nextLong();
		if (n % 3 != 0) {
			out.println(0);
			return;
		}

		n /= 3;

		Set<long[]> set = new TreeSet<long[]>(new Comparator<long[]>() {

			@Override
			public int compare(long[] o1, long[] o2) {
				for (int i = 0; i < o1.length; i++)
					if (o1[i] != o2[i])
						return o1[i] < o2[i] ? -1 : 1;
				return 0;
			}
		});

		for (long x = 1L; x * x * x <= n; x++) {
			if (n % x != 0)
				continue;
			long m = n / x;
			for (long a = 1; 2 * a < x; a++) {
				double[] w = new double[3];
				long b = x - a;
				w[0] = a * b - m;
				w[1] = a + b;
				w[2] = 1;
				int sl = QuadCurve2D.solveQuadratic(w);
				for (int i = 0; i < sl; i++) {
					long c = (long)(w[i] + 0.001);
					if (c > 0 && (a + c) * (b + c) == m) {
						long[] q = new long[] { a, b, c };
						sort(q);
						set.add(q);
					}
				}
			}
		}

		long[] f = {1, 1, 3, 6};

		long ans = 0;
		for (long[] t : set) {
			int count = 1;
			if (t[0] != t[1])
				count++;
			if (t[1] != t[2])
				count++;
			ans += f[count];
		}

//		System.err.println(set.size());
//		System.err.println(ans / 39090.);

		out.println(ans);
	}

	long pw3(long a) {
		return a * a * a;
	}

	class T implements Comparable<T> {
		long a, b, c;

		public T(long... x) {
			sort(x);
			this.a = x[0];
			this.b = x[1];
			this.c = x[2];
		}

		@Override
		public int compareTo(T t) {
			int cmp1 = Long.compare(a, t.a);
			if (cmp1 != 0)
				return cmp1;
			int cmp2 = Long.compare(b, t.b);
			if (cmp2 != 0)
				return cmp2;
			return Long.compare(c, t.c);
		}

	}

	void chk(boolean b) {
		if (b)
			return;
		System.out.println(new Error().getStackTrace()[1]);
		exit(999);
	}
	void deb(String fmt, Object... args) {
		System.out.printf(Locale.US, fmt + "%n", args);
	}
	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;
	}
}


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

public class C {

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

	void solve() throws IOException {
		long n = nextLong();
		if (n % 3 != 0) {
			out.println(0);
			return;
		}

		n /= 3;

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

		Collections.sort(divAL);

		long[] div = new long[divAL.size()];
		for (int i = 0; i < div.length; i++)
			div[i] = divAL.get(i);

		int ans = 0;
		for (int bigInd = 0; bigInd < div.length; bigInd++) {
			long big = div[bigInd];
			long rest = n / big;

			for (int smallInd = 0; smallInd <= bigInd; smallInd++) {
				long small = div[smallInd];
				if (small * small > rest)
					break;
				if (rest % small != 0)
					continue;
				long mid = rest / small;
				if (mid <= big && small + mid >= big) {
					if (((big ^ mid ^ small) & 1) == 0) {

						if (big == mid && mid == small)
							ans++;
						else if (big == mid || mid == small)
							ans += 3;
						else
							ans += 6;

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

	C() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

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

	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.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;

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

class TaskC {

	long n;

	public void solve(int testNumber, InputReader in, OutputWriter out) {
		n = in.nextLong();
		if (n % 3 != 0) {
			out.println(0);
			return;
		}
		n /= 3;
		int ans = 0;
		for (long ab = 2; ab * ab * ab <= n; ++ab) {
			if (n % ab != 0) {
				continue;
			}
			long left = n / ab;
			long to = (long) Math.sqrt(left);
			for (long a = 0; a <= ab / 2; ++a) {
				long b = ab - a;
				long A = 1;
				long B = ab;
				long C = a * b - left;
				long discr = B * B - 4 * A * C;
				if (discr < 0) {
					continue;
				}
				long root = (long) Math.sqrt(discr);
				if (root * root < discr) {
					++root;
				}
				if (root * root != discr) {
					continue;
				}
				long c = (-B + root) / 2 / A;
				if (c >= b) {
					long mult = (a + c) * (b + c);
					if (mult == left) {
						int cur = 1;
						if (b > a) {
							cur *= 2;
							if (c > b) {
								cur *= 3;
							}
						} else if (c > b) {
							cur *= 2;
						}
						ans += cur;
					}
				}
			}
		}
		out.println(ans);
	}
}

class InputReader {
	private final BufferedReader reader;
	private StringTokenizer tokenizer;

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

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

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

	}

class OutputWriter extends PrintWriter {

	public OutputWriter(Writer out) {
		super(out);
	}

	public OutputWriter(OutputStream out) {
		super(out);
	}

	}


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

public class Main {
    public static void main(String[] args) {
        long M = new Scanner(System.in).nextLong();
        int cnt = 0;

        if (M % 6 == 0) {
            M /= 3;
            long[] divs = new long[1000000];
            int nd = 0;
            for (long i = 2; i * i <= M; i++)
                if (M % i == 0) {
                    long d2 = M / i;
                    divs[nd++] = i;
                    if (d2 != i)
                        divs[nd++] = d2;
                }
            Arrays.sort(divs, 0, nd);

            for (int pi = 0; pi < nd; ++pi) {
                long i = divs[pi];
                if (i * i * i > M) break;

                long M2 = M / i;
                for (int pj = pi; pj < nd; ++pj)
                    if (M2 % divs[pj] == 0) {
                        long j = divs[pj];
                        long k = M2 / j;

                        if (k < j) break;
                        if (i + j > k && (i + j + k) % 2 == 0) {
                            if (i == j && i == k)
                                ++cnt;
                            else if (i == j || i == k || j == k)
                                cnt += 3;
                            else
                                cnt += 6;
                        }

                    }

            }

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


import java.util.*;
import java.io.*;
public class template1{
	public static void main(String[] main) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		PrintWriter out = new PrintWriter(System.out);
		int n = Integer.parseInt(st.nextToken());
        long[] fact = new long[n];
        long[] pow = new long[n];
        int mod = 1000000007;
        fact[0] = 1;
        pow[0] = 2;
        for(int i = 1; i < n; i++){
            fact[i] = ((i+1)*fact[i])%mod;
            pow[i] = (2*pow[i])%mod;
        }
        out.println((fact[n-1] - pow[n-1] + mod)%mod)
		out.close();
	}
}


import java.util.*;
import java.io.*;
public class vivek{
    static class FastReader
    {

        BufferedReader br;
        StringTokenizer st;

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

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

        char nextChar()
        {
            return next().charAt(0);
        }

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

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

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

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

        int[] nextArray(int rows)
        {
            int arr[] = new int[rows];
            for (int i = 0; i < rows; i++)
            arr[i] = nextInt();
            return arr;
        }

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

        char[] nextCharArray(int rows)
        {
            char arr[] = new char[rows];
            for (int i = 0; i < rows; i++)
            arr[i] = next().charAt(0);
            return arr;
        }

        int[][] nextMatrix(int rows, int columns)
        {
            int mat[][] = new int[rows][columns];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    mat[i][j] = nextInt();
                }
            }
            return mat;
        }
    }
    public static void main(String args[]){
        FastReader sc=new FastReader();
        PrintWriter p=new PrintWriter(System.out);
        int n=sc.nextInt();
        int m=1000000007;
        int f=1;
        int p=1;
        for(int i=2;i<n;i++){
            f=f*i;
            p=p*2;
        }
        p.println(f-p);
        p.flush();
    }
}


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

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

    int MAX = (int) 1e6+2, MOD = (int)1e9+7;
    long LMOD = (long) 1e9 + 7;

    long nCr(long n, long r) {
        int N = (int)n;
        int R = (int)r;
        long ans = fact[(int)n];
        ans = (ans*1L*inv[R]) % LMOD;
        ans = (ans*1L*inv[N-R]) % LMOD;
        return ans;
    }

    long[] fact;
    long[] inv;

    void solve(int TC) {
        int MAX = (int) 1e6+2, MOD = (int)1e9+7;
        long LMOD = (long) 1e9 + 7;
        fact = new long[MAX];
        inv = new long[MAX];
        fact[0] = 1;
        int N = (int)MAX;
        for(int i=1;i<N;i++) {
            fact[i] = (fact[i-1] * i) % LMOD;
        }
        inv[MAX-1] = inv(fact[MAX-1]);
        for(int i=MAX-2;i>=0;--i) {
            inv[i] = (inv[i+1] * 1L * (i + 1L)) % LMOD;
        }
        int n = ni();
        long ans = 0;
        for(int i=1;i<=n;i++) {
            if(i==1 || i==n) {
                ++ans;
            } else {
                long tmp = nCr(n-1, i-1);
                ans += tmp;
                ans %= LMOD;
            }
        }
        long ans2 = fact[n] - ans;
        ans2 %= LMOD;
        if(ans2<0) ans2 += LMOD;
        ans2 %= LMOD;
        pn(ans2);
    }

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

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

    long inv(long x) {
        long o = 1;
        for(long p = MOD-2; p > 0; p>>=1) {
            if((p&1)==1)o = (o*x)%MOD;
            x = (x*x)%MOD;
        } return o;
    }
    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); }

    void run() throws Exception {
        is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        out = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        int T = TestCases ? ni() : 1;
        for(int t=1;t<=T;t++) solve(t);
        out.flush();
        if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
    }

    void p(Object o) { out.print(o); }
    void pn(Object o) { out.println(o); }
    void pni(Object o) { out.println(o);out.flush(); }
    double PI = 3.141592653589793238462643383279502884197169399;

    int ni() {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-') {
            minus = true;
            b = readByte();
        }
        while(true) {
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-') {
            minus = true;
            b = readByte();
        }
        while(true) {
            if(b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

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

    int BUF_SIZE = 1024 * 8;
    byte[] inbuf = new byte[BUF_SIZE];
    int lenbuf = 0, ptrbuf = 0;

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

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

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

    void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); }
}


import java.io.*;
import java.util.*;
public class d2prac implements Runnable
{
    private boolean console=false;

    public void solve()
    {
    	int i;
    	int n=in.ni();
    	long m=1000000007;
    	long ans=0;
    	long fact[]=new long[n+1];
    	if(n==3)
    	{
    		out.println(2);
    		continue;
    	}
    	fact[4]=8;
    	for(i=5;i<=n;i++)
    		fact[i]=((fact[i-1])*2)%m;
    	long sum=1;
    	for(i=1;i<=n;i++)
    		sum=(sum*i)%m;
    	ans = ((sum - fact[n])%m)+m;
    	out.println(ans%m);
    }
    @Override
    public void run() {
        try {  init();  }
        catch (FileNotFoundException e) {  e.printStackTrace();   }

        int t= 1;

        while (t-->0) {
            solve();
            out.flush(); }
    }
    private FastInput in;    private PrintWriter out;
    public static void main(String[] args) throws Exception {	 new d2prac().run();	 }

    private void init() throws FileNotFoundException {
        InputStream inputStream = System.in; 	 OutputStream outputStream = System.out;
        try {  if (!console && System.getProperty("user.name").equals("sachan")) {
                outputStream = new FileOutputStream("/home/sachan/Desktop/output.txt");
                inputStream = new FileInputStream("/home/sachan/Desktop/input.txt");   	}
        }	catch (Exception ignored) {	}
        out = new PrintWriter(outputStream); 	 in = new FastInput(inputStream);
    }
    static class FastInput { InputStream obj;
    	public FastInput(InputStream obj) {	this.obj = obj;	}
        byte inbuffer[] = new byte[1024];  int lenbuffer = 0, ptrbuffer = 0;
        int readByte() { if (lenbuffer == -1) throw new InputMismatchException();
            if (ptrbuffer >= lenbuffer) { ptrbuffer = 0;
                try { lenbuffer = obj.read(inbuffer);  }
                catch (IOException e) { throw new InputMismatchException(); } }
            if (lenbuffer <= 0) return -1;return inbuffer[ptrbuffer++]; }

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

        int ni() { int num = 0, b;boolean minus = false;
            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
            if (b == '-') { minus = true;b = readByte(); }
            while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else {
                    return minus ? -num : num; }b = readByte(); }}

        long nl() { long num = 0;int b;boolean minus = false;
            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
            if (b == '-') { minus = true;b = readByte(); }
            while (true) { if (b >= '0' && b <= '9') { num = num * 10L + (b - '0'); } else {
                    return minus ? -num : num; }b = readByte(); } }

        boolean isSpaceChar(int c) { return (!(c >= 33 && c <= 126)); }
        int skip() { int b;while ((b = readByte()) != -1 && isSpaceChar(b)) ;return b; }
        float nf() {return Float.parseFloat(ns());}
        double nd() {return Double.parseDouble(ns());}
        char nc() {return (char) skip();}
    }
}


import static java.lang.Math.acos;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import static java.lang.Math.sqrt;
import static java.lang.System.in;
import static java.lang.System.out;
import java.io.*;
import java.util.*;

public class A227 {
	static final double EPS = 1e-10;
	static final double INF = 1 << 31;
	static final double PI = Math.PI;

	public static Scanner sc = new Scanner(in);
	StringBuilder sb = new StringBuilder();
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	public void run() throws IOException {
		String input;
		String[] inputArray;
		P [] ver = new P[3];
		for ( int i =0; i<3; i++){
			input = br.readLine();
			inputArray = input.split(" ");
			int x = Integer.valueOf(inputArray[0]);
			int y = Integer.valueOf(inputArray[1]);
			ver[i] = new P(x,y);
		}
		double ans = P.ccw(ver[0], ver[1], ver[2]);
		if (ans==-1) ln("RIGHT");
		else if (ans==1) ln("LEFT");
		else ln("TOWARDS");
	}

	public static void main(String[] args) throws IOException {
		new A227().run();
	}
	public static void ln(Object obj) {
		out.println(obj);
	}
	static class P{
		//geo
			static final double EPS=1e-10;
			static final double INF = 1<<31;
			static final double PI = Math.PI;
			static int signum(double x){
				return x<-EPS?-1:x>EPS?1:0;
			}
		static Comparator<P> comp=new Comparator<P>(){
			public int compare(P p1, P p2) {
				return signum(p1.x-p2.x)!=0?signum(p1.x-p2.x):signum(p1.y-p2.y);
			}
		};
		public static  final P O=new P(0,0);
		final double x,y;
		P(double _x,double _y){
			x=_x;y=_y;
		}
		//四則
		P add(P a){
			return new P(x+a.x,y+a.y);
		}
		P sub(P a){
			return new P(x-a.x,y-a.y);
		}
		P mul(P a){
			return new P(x*a.x-y*a.y,x*a.y+y*a.x);
		}
		P div(P a){
			double d2=a.dist2(O);
			return new P(dot(a,this)/d2,cross(a,this)/d2);
		}
		//共役
		P conj(){
			return new P(x,-y);
		}
		//内積　a・b=|a||b|cosθ=a.x*b.x+a.y*b.y
		static double dot(P a,P b){
			return  a.x*b.x+a.y*b.y;
		}
		//外積　a×b=|a||b|sinθ=a.x*b.y-a.y*b.x
		static double cross(P a,P b){
			return  a.x*b.y-a.y*b.x;
		}
		//二乗距離
		double dist2(P p){
			return (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y);
		}
		//a,b間の距離
		double dist(P p){
			return sqrt(dist2(p));
		}
		public double norm(){
			return dist(O);
		}
		// a→b→cと進むときの向き
		static int ccw(P a,P b,P c){
			b=b.sub(a);c=c.sub(a);
			if(cross(b,c)>EPS)return 1;//counter clockwise
			if(cross(b,c)<-EPS)return -1;//clockwise
			if(dot(b,c)<-EPS)return 2;//c--a--b on line
			if(b.norm()<c.norm()-EPS)return -2;//a--b--c on line
			return 0;//a--c--b on line (or b==c)
		}
		//oからみたaの角度
		//angle of vector→OA
		//dir.y==0 なら return sを追加 June 3
		static double arg(P o,P a){
			P dir=a.sub(o);
			double s=acos(dir.x/dir.norm());
			return dir.y>=0?s:2*PI-s;
		}
		//ABとBCがなす角度：ABからみて時計周り 0<=x<2pi
		static double arg(P a,P b, P c){
			double angleA = P.arg(a,b);
			double angleB = P.arg(b,c);
			double angle =  Math.PI+angleA-angleB;
			if (angle-2*Math.PI>EPS) angle-=2*Math.PI;
			if (angle<EPS) angle+=2*Math.PI;
			return angle;
		}
		//oを中心に回転
		//this.add->o.add June 6
		P rotate(P o,double arg){
			return o.add(this.sub(o).mul(new P(cos(arg),sin(arg))));
		}
		//→OA,→OBの面積の2倍
		static double S2(P a,P b,P o){
			return cross(a.sub(o),b.sub(o));
		}
		static double S(P a,P b,P o){
			return S2(a,b,o)/2;
		}
		//絶対値と偏角から座標を取得
		static P polar(double abs,double arg){
			return new P(abs*cos(arg),abs*sin(arg));
		}
		// ?
		static P proj(P p,P o){
			return o.mul(new P(dot(p,o)/o.norm(),0));
		}
		public boolean equals(Object obj) {
			if(obj instanceof P){
				P p=(P)obj;
				return signum(x-p.x)==0 && signum(y-p.y)==0;
			}
			return false;
		}
		public String toString(){
			return "("+x+","+y+")";
		}
	}
}


import java.util.Scanner;

public class TaskA {

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int ax = reader.nextInt();
		int ay = reader.nextInt();
		int bx = reader.nextInt();
		int by = reader.nextInt();
		int cx = reader.nextInt();
		int cy = reader.nextInt();
		Tod a = new Tod (ax, ay);
		Tod b = new Tod (bx, by);
		Tod c = new Tod (cx, cy);
		solutionA (new Line(a, b), c);
	}

	static void solutionA (Line ab, Tod c) {
		int x = ((c.x * ab.koeffX) + (c.y * ab.koeffY) + ab.c);
		if (x == 0) System.out.println ("TOWARDS");
		else if (x > 0) System.out.println ("RIGHT");
		else System.out.println ("LEFT");
	}

	class Tod {
		int x;
		int y;
		Tod (int x, int y) {
			this.x = x;
			this.y = y;
		}
	}

	class Line {
		int koeffX;
		int koeffY;
		int c;
		Line (Tod a, Tod b) {
			koeffX = (b.y - a.y);
			koeffY = (a.x - b.x);
			c = (b.x*a.y - a.x*b.y);
		}
	}
}


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

public class Solver {

    public static void main(String[] Args) throws NumberFormatException, IOException {
        new Solver().Run();
    }

    PrintWriter pw;
    StringTokenizer Stok;
    BufferedReader br;

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

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

    public void Run() throws NumberFormatException, IOException {
        br=new BufferedReader(new InputStreamReader(System.in));
        pw=new PrintWriter(new OutputStreamWriter(System.out));

        double xA,yA,xB,yB,xC,yC;
        xA = nextDouble();
        yA = nextDouble();
        xB = nextDouble()-xA;
        yB = nextDouble()-yA;
        xC = nextDouble()-xA-xB;
        yC = nextDouble()-yA-yB;

        byte ab = quarter(xB,yB);
        byte bc = quarter(xC,yC);
        if  (ab == bc) {
            pw.println("TOWARDS");
        } else
        if (left(ab,bc)) {
            pw.println("LEFT");
        } else
            pw.println("RIGHT");
        pw.flush();
        pw.close();
    }

    public boolean left(byte ab, byte bc) {
        if (((ab==1) && (bc==2)) || ((ab==2) && (bc==3)) ||
            ((ab==3) && (bc==4)) || ((ab==4) && (bc==1))) {
            return true;
        } else {
            return false;
        }
    }

    public byte quarter(double x, double y) {
        if ((x>=0) && (y>0)) {
            return 1;
        } else
        if ((x<0) && (y>=0)) {
            return 2;
        } else
        if ((x<=0) && (y<0)) {
            return 3;
        } else
            return 4;
    }
}


public class NewApp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a1 = sc.nextInt();
        int a2 = sc.nextInt();
        int b1 = sc.nextInt();
        int b2 = sc.nextInt();
        int c1 = sc.nextInt();
        int c2 = sc.nextInt();
        int result = (b1 - a1) * (c2 - b2) - (b2 - a2) * (c1 - b1);
        if (result == 0){
            System.out.println("TOWARDS");
        } else if (result > 0) {
            System.out.println("LEFT");
        } else{
            System.out.println("RIGHT");
        }
    }
}


import java.util.Scanner;
public class project{
    public static void main(String[]args){
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		int m =sc.nextInt();
		int len=s.length();

		int rt[]=new int[len];
		int bat[]=new int[len];

		Long sum=0;
		Long bs=1;
		for(int i=len-1;i>-1;i--)
		{
			sum=(sum+bs*(s[i]-'0'))%m;
			rt[i]=sum;
			bat[len-i-1]=bs;
			bs=(bs*10LL)%m;
		}
		bat[len]=bs;

		int rs=m;
		sum=0;
		for(int i=0;i<len;i++)
		{
			if(s[i]=='0')
			{
				sum=(sum*10+s[i]-'0')%m;
				continue;
			}
			int q=(sum+rt[i]*bat[i])%m;
			if(q<rs)rs=q;
			sum=(sum*10+s[i]-'0')%m;
		}
		System.out.println(rs);
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		String s = sc.next();
		BigInteger big = new BigInteger(s);
		int divider  = sc.nextInt();
		BigInteger bigdivider = BigInteger.valueOf(divider);

        int mnoj[] = new int[10];
		BigInteger bm1 = BigInteger.TEN.pow(len);
		BigInteger bm = bm1;
		for (int i = 1; i < 10; i++)
		{
			mnoj[i] = bm.mod(bigdivider).intValue();
			bm = bm.add(bm1);
		}

		int len = s.length();

		int q = 1;
		int min = big.mod(bigdivider).intValue();
		int cur = min;
		for (int i = 0; i < len; i++) {
			q = (s.charAt(i) - '0');
			if (q != 0) {
				if (cur < min) {
					min = cur;
				}
			}
			cur = (cur * 10 - mnoj[q] + q + divider) % divider;
		}

		System.out.println(min);
		sc.close();
	}
}


import java.util.Scanner;
import java.math.*;
public class project{
    public static void main(String[]args){
	    Scanner sc = new Scanner(System.in);
	    String s=sc.next();
	    String mod=sc.next();
	    BigInteger b, m, ten, ans, mul, one;
	    ten=new BigInteger("10");
	    mul=new BigInteger("10");
	    b=new BigInteger("0");
	    one=new BigInteger("0");
	    int i=0, len=s.length();
	    b=b.add(new BigInteger(s));
	    m=new BigInteger("0");
	    m=m.add(new BigInteger(mod));
	    ans=new BigInteger("0");
	    ans=b.mod(m);
	    for (i=0; i<len-1; i++) mul=mul.multiply(ten);
	    for (i=0; i<len; i++) {
	        one=b.mod(ten);
	        one=one.multiply(mul);
	        b=b.divide(ten);
	        b=b.add(one);
	        if (ans.compareTo(b.mod(m))==1) {
	            ans=b.mod(m);
	        }
	    }
	    System.out.println(ans);
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		String s = sc.next();
		BigInteger big = new BigInteger(s);
		int divider  = sc.nextInt();
		BigInteger bigdivider = BigInteger.valueOf(divider);

		int len = s.length();

		int q = 1;
		int min = big.mod(bigdivider).intValue();
		int cur = min;
		for (int i = 0; i < len; i++) {
			q = (s.charAt(i) - '0');
			if (q != 0) {
				if (cur < min) {
					min = cur;
				}
			}
			cur = (cur * 10 + q) % divider;
		}

		System.out.println(min);
		sc.close();
	}
}