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

import static com.sun.tools.javac.jvm.ByteCodes.ret;

public class Solution758D {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer tokenizer = null;
    //Solution758A Code starts Here
    int totalCases, testnum;

    long n;
    String s;

    boolean input() throws IOException {
        n = nl();
        s = ns().trim();
        return true;
    }

    void solve() throws IOException {
        int l = s.length();
        int a = l-1, b = l;
        long m = 1;
        long ret = 0;
        while(b > 0) {
            while (a > -1) {
                String str = s.substring(a, b);
                long acc = Long.parseLong(str);
                if (acc >= n) {
                    break;
                } else {
                    a--;
                }
            }
            a++;
            while(s.charAt(a) == '0' && a < b-1) a++;
            long acc = Long.parseLong(s.substring(a, b));
            ret += (acc * m);
            m*=n;
            b=a--;
        }
        System.out.println(ret);
    }

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

    void execute() throws IOException {
        totalCases = 1;
        for (testnum = 1; testnum <= totalCases; testnum++) {
            if (!input())
                break;
            solve();
        }
    }

    void debug(Object... os) {
        System.out.println(Arrays.deepToString(os));
    }

    int ni() throws IOException {
        return Integer.parseInt(ns());
    }

    long nl() throws IOException {
        return Long.parseLong(ns());
    }

    double nd() throws IOException {
        return Double.parseDouble(ns());
    }

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

    String nline() throws IOException {
        tokenizer = null;
        return br.readLine();
    }
}


/**
 * @author Kunal
 *
 */

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

public class D392 {
	public static void main(String[] args) {
		InputReader in = new InputReader();
		PrintWriter out = new PrintWriter(System.out);
		final long start = System.currentTimeMillis();
		new Task1().solve(in, out);
		@SuppressWarnings("unused")
		final long duration = System.currentTimeMillis() - start;
		out.close();
	}

	static class Task1 {
		public void solve(InputReader in, PrintWriter out) {
			long base  = in.nextLong();
			String s = in.next();

			long ans=0, k=0;
			int i=s.length()-1;
			while(i>=0){
				String nums=s.charAt(i)+"";
				Long num=Long.parseLong(s.charAt(i)+""), temp=num;
				//out.print(num+" ");
				int start = i;
				for(int j=--i; j>=0; j--){
					nums=s.charAt(j)+nums;
					temp = Long.parseLong(nums);
					if(temp<base){
						num = temp;
						i--;
					} else {
						break;
					}
				}
				//out.print(num+" ");

				if(i>=0) nums = nums.substring(1);
				//out.println(nums);
				while(nums.length()>0 && nums.charAt(0)=='0'){
					i++;
					nums = nums.substring(1, nums.length());
				}

				ans += expo(base, k++, Long.MAX_VALUE)*num;
			}

			out.println(ans);
		}

		long expo(long a, long b, long MOD){
			long result = 1;
			while (b>0){
				if (b%2==1) result=(result*a)%MOD;
				b>>=1;
				a=(a*a)%MOD;
			}
			return result%MOD;
		}
	}

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

		public InputReader() {
			reader = new BufferedReader(new InputStreamReader(System.in));
			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 String nextLine() {
			String s = null;
			try {
				s = reader.readLine();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
			return s;
		}

		public String nextParagraph() {
			String line = null;
			String ans = "";
			try {
				while ((line = reader.readLine()) != null) {
					ans += line;
				}
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
			return ans;
		}

	}
}


public class A{

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

        System.out.println(2*n*(n-1) + 1);
    }
}


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

public class abc
{
public static void main(String[] args)
{
 Scanner sc = new Scanner(System.in);
 int n =sc.nextInt();
 int dp[] = new int[n];
 dp[0]=1;
 int i =0;
 for(i =1;i<n;i++)
 {
    dp[i]= dp[i-1]+Math.pow(2,i);
 }
 System.out.println(dp[i]);
}}


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

public class codeforce1
{
	public static void main(String[] args)
	{
	Scanner sc=new Scanner(System.in);
	int n=sc.nextInt();
	int ans=0;
	if n==1;
	System.out.println(1);
	else
	{
		int x=1;
		for(int i=1;i<=n;i++)
		{
			ans+=x;
			x=x+2;
		}
		x=1;
		for(int i=1;i<n;i++)
		{
			ans+=x;
			x=x+2;
		}
		System.out.println(ans);

	}

}


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

public class abc
{
public static void main(String[] args)
{
 Scanner c = new Scanner(System.in);
 int n =sc.nextInt();
 int dp[] = new int[n];
 dp[0]=1;
 int i =0;
 for(i =1;i<n;i++)
 {
    dp[i]= dp[i-1]+Math.pow(2,i);
 }
 System.out.println(dp[i]);
}}


public class A {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String s = in.next();
        int Xcount=0;
        int xcount=0;
        String s2 ="";
        for (int i = 0; i < n; i++) {

            char ch = s.charAt(i);
            if (ch == 'X') {
                Xcount++;
            }
            else if (ch== 'x') {
                xcount++;
            }

        }
        int t =  (xcount - Xcount)/2;

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

                char ch = s.charAt(i);
                if (t > 0 && ch == 'x') {
                    ch = 'X';
                    t--;
                }
                s2 += ch;
            }
        }
        else
            if( t < 0){
            for (int i = 0; i < n; i++) {

                char ch = s.charAt(i);
                if (t < 0 && ch == 'X') {
                    ch = 'x';
                    t++;
                }
                s2 += ch;
            }
        }
        System.out.println(Math.abs((xcount - Xcount)/2));
        System.out.println(s2);

    }
}


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

public class Solution
{
    @SuppressWarnings("empty-statement")
    public static void main(String[] args) throws Exception
    {
        Scanner sc=new Scanner(System.in);
       int q,st=0,sd=0,p;
       char v,w,c;
       String a;
       q=sc.nextInt();
       a=sc.next();
       char[] y=new char[q];
       v='X'; // стоит
       w='x'; // сидит
       for(int i=0;i<a.length();i++){
           c=a.charAt(i);
           if(c==v){
               st++;
           }
           if(c==w){
               sd++;
           }
       }
       for(int i=0;i<q;i++){
           if(i<q/2){
              y[i]='x';
           }
           else{
               y[i]='X';
           }
       }
       String t=new String(y, 0, q);

       p=Min(st, sd);
       System.out.println(p);
       System.out.println(t);
    }
    public static int Min(int a,int b){
        int m=0;
        if(a>b){
           m=(a-b)/2;
        }
        else{
            m=(b-a)/2;
        }
        return m;
     }

}


import java.util.*;
public class lll {

	public static void main(String [] args)
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String ss = sc.next();
		int cnt = 0 , cnt2 = 0;
		for(int i = 0 ; i < n ; i++)
		{
			if(ss.charAt(i) == 'X')
				cnt++;
			else
				cnt2++;
		}
		ss.
		char arr[] = ss.toCharArray();
		if(cnt > cnt2)
		{
			System.out.println((cnt-cnt2)/2);
			for(int i = 0 ; i < (cnt-cnt2)/2 ; i++)
			{
				if(arr[i] == 'X')
					arr[i] = 'x';
			}

		}
		else
		{
			System.out.println((cnt2-cnt)/2);
			for(int i = 0 ; i < (cnt2-cnt)/2 ; i++)
			{
				if(arr[i] == 'x')
					arr[i] = 'X';
			}

		}
		for(int i = 0 ; i < n ; i++)
		{
			System.out.print(arr[i]);
		}

	}
}


import java.util.*;
public class A {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            char[] chs = sc.next().toCharArray();
            int a = 0;
            for(char ch : chs) if(ch=='x') a++;
            int b=n-a;
            int c = a-b;
            int diff=Math.abs(c);
            for(int i=0; i<n; ++i) {
                if(a==b) break;
                if(chs[i]=='x' && a>b) {chs[i]=='X'; a--; b++;}
                else if(chs[i]='X'&&b>a) {chs[i]=='x'; b--; a++;}
            }
            System.out.println(diff);
            System.out.println(new String(chs));
        }
}


import java.util.*;

public class ProblemB {

	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int ans = 0;
		int counter = 0;
		int ya = 0;
		while(true) {
			if(n%400 == 0 || (n%4 == 0 && n%100 != 0) {
				counter += 2;
				ya++;
			}
			else {
				counter ++ ;
				ya++;
			}
			if(counter % 7 == 0) {
				ans = n + ya;
			}
		}
		System.out.println(ans);
	}
}


import java.util.*;

public class ProblemB {

	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int ans = 0;
		int counter = 0;
		int ya = 0;
		while(true) {
			if(n%400 == 0 || (n%4 == 0 && n%100 != 0) {
				counter += 2;
				ya++;
			}
			else {
				counter ++ ;
				ya++;
			}
			if(counter % 7 == 0) {
				ans = n + ya;
			}
		}
	}
}


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

@SuppressWarnings("unchecked")
public class P678B {

  boolean isLeap(int y) {
    return ((((y % 4) == 0) && (y % 100) != 0) || ((y % 400) == 0));
  }

  public void run() throws Exception {
    println(y + (isLeap(y) ? 28 : 6));
  }

  public static void main(String... args) throws Exception {
    br = new BufferedReader(new InputStreamReader(System.in));
    pw = new PrintWriter(new BufferedOutputStream(System.out));
    new P678B().run();
    br.close();
    pw.close();
    System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]");
  }

  static long startTime = System.currentTimeMillis();
  static BufferedReader br;
  static PrintWriter pw;
  StringTokenizer stok;

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

  void print(byte b) { print("" + b); }
  void print(int i) { print("" + i); }
  void print(long l) { print("" + l); }
  void print(double d) { print("" + d); }
  void print(char c) { print("" + c); }
  void print(Object o) {
    if (o instanceof int[]) { print(Arrays.toString((int [])o));
    } else if (o instanceof long[]) { print(Arrays.toString((long [])o));
    } else if (o instanceof char[]) { print(Arrays.toString((char [])o));
    } else if (o instanceof byte[]) { print(Arrays.toString((byte [])o));
    } else if (o instanceof short[]) { print(Arrays.toString((short [])o));
    } else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o));
    } else if (o instanceof float[]) { print(Arrays.toString((float [])o));
    } else if (o instanceof double[]) { print(Arrays.toString((double [])o));
    } else if (o instanceof Object[]) { print(Arrays.toString((Object [])o));
    } else { print("" + o); }
  }
  void print(String s) { pw.print(s); }
  void println() { println(""); }
  void println(byte b) { println("" + b); }
  void println(int i) { println("" + i); }
  void println(long l) { println("" + l); }
  void println(double d) { println("" + d); }
  void println(char c) { println("" + c); }
  void println(Object o) { print(o); println(); }
  void println(String s) { pw.println(s); }
  int nextInt() throws IOException { return Integer.parseInt(nextToken()); }
  long nextLong() throws IOException { return Long.parseLong(nextToken()); }
  double nextDouble() throws IOException { return Double.parseDouble(nextToken()); }
  char nextChar() throws IOException { return (char) (br.read()); }
  String next() throws IOException { return nextToken(); }
  String nextLine() throws IOException { return br.readLine(); }
  int [] readInt(int size) throws IOException {
    int [] array = new int [size];
    for (int i = 0; i < size; i++) { array[i] = nextInt(); }
    return array;
  }
  long [] readLong(int size) throws IOException {
    long [] array = new long [size];
    for (int i = 0; i < size; i++) { array[i] = nextLong(); }
    return array;
  }
  double [] readDouble(int size) throws IOException {
    double [] array = new double [size];
    for (int i = 0; i < size; i++) { array[i] = nextDouble(); }
    return array;
  }
  String [] readLines(int size) throws IOException {
    String [] array = new String [size];
    for (int i = 0; i < size; i++) { array[i] = nextLine(); }
    return array;
  }

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


package codeforces.contest678;

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

public class B {
  private static class Task {
    void solve(FastScanner in, PrintWriter out) {
      int y = in.nextInt();
      int y_origin = y;
      long days = 0;
      while (true) {
        days += 365;
        if (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) {
          days++;
        }
        y++;
        if (days % 7 == 0 && is_uruu(y) == is_uruu(y_origin)) {
          out.println(y);
          return;
        }

      }
    }

    boolean is_uruu(int y) {
      return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));
    }
  }

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

  private static class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int bufferLength = 0;

    private boolean hasNextByte() {
      if (ptr < bufferLength) {
        return true;
      } else {
        ptr = 0;
        try {
          bufferLength = in.read(buffer);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (bufferLength <= 0) {
          return false;
        }
      }
      return true;
    }

    private int readByte() {
      if (hasNextByte()) return buffer[ptr++];
      else return -1;
    }

    private static boolean isPrintableChar(int c) {
      return 33 <= c && c <= 126;
    }

    private void skipUnprintable() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
    }

    boolean hasNext() {
      skipUnprintable();
      return hasNextByte();
    }

    public String next() {
      if (!hasNext()) throw new NoSuchElementException();
      StringBuilder sb = new StringBuilder();
      int b = readByte();
      while (isPrintableChar(b)) {
        sb.appendCodePoint(b);
        b = readByte();
      }
      return sb.toString();
    }

    long nextLong() {
      if (!hasNext()) throw new NoSuchElementException();
      long n = 0;
      boolean minus = false;
      int b = readByte();
      if (b == '-') {
        minus = true;
        b = readByte();
      }
      if (b < '0' || '9' < b) {
        throw new NumberFormatException();
      }
      while (true) {
        if ('0' <= b && b <= '9') {
          n *= 10;
          n += b - '0';
        } else if (b == -1 || !isPrintableChar(b)) {
          return minus ? -n : n;
        } else {
          throw new NumberFormatException();
        }
        b = readByte();
      }
    }

    public int nextInt() {
      return (int) nextLong();
    }
  }
}


package Problems;

import FastIO.Reader;
import java.io.PrintWriter;
import java.util.Arrays;

public class TaskA {
    public void solve(int testNumber, Reader in, PrintWriter out) {

        int[] p = new int[4];

        p[0] = in.nextInt();
        p[1] = in.nextInt();
        p[2] = in.nextInt();
        p[3] = in.nextInt();

        int a = in.nextInt();
        int b = in.nextInt();

        Arrays.sort(p);

        if (p[0] < a)
            out.println(0);
        else
            out.println(Math.abs(p[0] - a));
    }
}


import java.util.*;

import java.io.*;
public class Main
{
    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

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

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

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
	static long gcd(long n,long m) {
		if(n==0&&m==0)return Long.MAX_VALUE;
		if(n==0)return m;
		long temp=m%n;
		return gcd(temp,n);
	}
	static long pow(int i,int j) {
		return ((long)Math.pow(i,j));
	}
	static long pow(long x,long j) {
		for(int i=0;i<32;i++) {
			long temp=1;
			for(int w=0;w<i;w++) {
				temp*=x;
			}
			if(neg&&temp>2147483648l)return 1;
			else
			if(!neg&&temp>2147483647)return 1;
			//pw.println(j);
			if(neg&&temp==j&&i%2!=0)return i;
			if(!neg&&temp==j)return i;
		}
		return 1;
	}
	static trouble ex_gcd(long m,long n) {
		if(m==0) {
			if(n!=0)
			return (new trouble(0,1,n));
			return(new trouble(0,1,(long)1e9));
		}
		trouble temp=ex_gcd(n%m,m);
		long r=temp.x;
		long m_=temp.y;
		temp.x=(-1*n/m)*r+m_;
		temp.y=r;
		return temp;
	}
	  static class pair{
	    	long x;
	    	long y;
	    	pair(long i,long j){
	    		x=i;
	    		y=j;
	    	}
	    }
	  static String toString(trouble p) {
  		return (p.x)+" "+(p.y)+" "+p.z;
  	}
	  static class trouble{
	    	long x;
	    	long y;
	    	long z;
	    	trouble(long i,long j,long k){
	    		x=i;
	    		y=j;
	    		z=k;
	    	}
	    }
	static LinkedList<Integer> div(int x){
		LinkedList<Integer>arr =new LinkedList<>();
        for(int i=1;i*i<=x;i++) {
        	if(x%i==0) {arr.add(i);if(x/i!=i)arr.add(x/i);}
        }
        return arr;
	}
	static long bin(long i,long[] arr) {
		long l=0l;
		long r=Math.min(i+2,arr.length-1);
		long res=r;
		while(l<=r) {
			long mid=l+r>>1;
		    if(arr[(int)mid]>=i) {
		    	r=mid-1;res=mid;
		    }
		    else l=mid+1;
		}
		return res;
	}
	static PrintWriter pw=new PrintWriter(System.out);
    public static void main(String[] args) throws IOException,Exception
    {
  //  	Thread.sleep(3000);
        Reader sc=new Reader();
        //BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int b0,b1,b2,b3,b4,a,b;
        b0=sc.nextInt();
        b1=sc.nextInt();
        b2=sc.nextInt();
        b3=sc.nextInt();
        a=sc.nextInt();
        b=sc.nextInt();
        int[][]arr= {{ b0,b1,b2,b3 },{ b0,b1,b3,b2 },{ b0,b2,b1,b3 },{ b0,b2,b3,b1 },{ b0,b3,b1,b2 },{ b0,b3,b2,b1 },{ b1,b0,b2,b3 },{ b1,b0,b3,b2 },{ b1,b2,b0,b3 },{ b1,b2,b3,b0 },{ b1,b3,b0,b2 },{ b1,b3,b2,b0 },{ b2,b0,b1,b3 },{ b2,b0,b3,b1 },{ b2,b1,b0,b3 },{ b2,b1,b3,b0 },{ b2,b3,b0,b1 },{ b2,b3,b1,b0 },{ b3,b0,b1,b2 },{ b3,b0,b2,b1 },{ b3,b1,b0,b2 },{ b3,b1,b2,b0 },{ b3,b2,b0,b1 },{ b3,b2,b1,b0 }};
        int res=0;
        for(int i=a;i<b;i++) {
             int c=0;
             for(int j=0;j<24;j++) {
            	 if(i%arr[j][0]%arr[j][1]%arr[j][2]%arr[j][3]==i)c++;
             }
             if(c>=7) {
            	 res+=1;
             }
        }
        pw.println(res);
        pw.close();
    }
}


import java.util.*;

import java.io.*;
public class Main
{
    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

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

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

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
	static long gcd(long n,long m) {
		if(n==0&&m==0)return Long.MAX_VALUE;
		if(n==0)return m;
		long temp=m%n;
		return gcd(temp,n);
	}
	static long pow(int i,int j) {
		return ((long)Math.pow(i,j));
	}
	static long pow(long x,long j) {
		for(int i=0;i<32;i++) {
			long temp=1;
			for(int w=0;w<i;w++) {
				temp*=x;
			}
			if(neg&&temp>2147483648l)return 1;
			else
			if(!neg&&temp>2147483647)return 1;
			//pw.println(j);
			if(neg&&temp==j&&i%2!=0)return i;
			if(!neg&&temp==j)return i;
		}
		return 1;
	}
	static trouble ex_gcd(long m,long n) {
		if(m==0) {
			if(n!=0)
			return (new trouble(0,1,n));
			return(new trouble(0,1,(long)1e9));
		}
		trouble temp=ex_gcd(n%m,m);
		long r=temp.x;
		long m_=temp.y;
		temp.x=(-1*n/m)*r+m_;
		temp.y=r;
		return temp;
	}
	  static class pair{
	    	long x;
	    	long y;
	    	pair(long i,long j){
	    		x=i;
	    		y=j;
	    	}
	    }
	  static String toString(trouble p) {
  		return (p.x)+" "+(p.y)+" "+p.z;
  	}
	  static class trouble{
	    	long x;
	    	long y;
	    	long z;
	    	trouble(long i,long j,long k){
	    		x=i;
	    		y=j;
	    		z=k;
	    	}
	    }
	static LinkedList<Integer> div(int x){
		LinkedList<Integer>arr =new LinkedList<>();
        for(int i=1;i*i<=x;i++) {
        	if(x%i==0) {arr.add(i);if(x/i!=i)arr.add(x/i);}
        }
        return arr;
	}
	static long bin(long i,long[] arr) {
		long l=0l;
		long r=Math.min(i+2,arr.length-1);
		long res=r;
		while(l<=r) {
			long mid=l+r>>1;
		    if(arr[(int)mid]>=i) {
		    	r=mid-1;res=mid;
		    }
		    else l=mid+1;
		}
		return res;
	}
	static PrintWriter pw=new PrintWriter(System.out);
    public static void main(String[] args) throws IOException,Exception
    {
  //  	Thread.sleep(3000);
        Reader sc=new Reader();
        //BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int b0,b1,b2,b3,b4,a,b;
        b0=sc.nextInt();
        b1=sc.nextInt();
        b2=sc.nextInt();
        b3=sc.nextInt();
        a=sc.nextInt();
        b=sc.nextInt();
        int[][]arr= {{ b0,b1,b2,b3 },{ b0,b1,b3,b2 },{ b0,b2,b1,b3 },{ b0,b2,b3,b1 },{ b0,b3,b1,b2 },{ b0,b3,b2,b1 },{ b1,b0,b2,b3 },{ b1,b0,b3,b2 },{ b1,b2,b0,b3 },{ b1,b2,b3,b0 },{ b1,b3,b0,b2 },{ b1,b3,b2,b0 },{ b2,b0,b1,b3 },{ b2,b0,b3,b1 },{ b2,b1,b0,b3 },{ b2,b1,b3,b0 },{ b2,b3,b0,b1 },{ b2,b3,b1,b0 },{ b3,b0,b1,b2 },{ b3,b0,b2,b1 },{ b3,b1,b0,b2 },{ b3,b1,b2,b0 },{ b3,b2,b0,b1 },{ b3,b2,b1,b0 }};
        int res=0;
        for(int i=a;i<=b;i++) {
             int c=0;
             for(int j=0;j<24;j++) {
            	 if(i%arr[j][0]%arr[j][1]%arr[j][2]%arr[j][3]==i)c++;
             }
             if(c>=7) {
            	 res+=1;
             }
        }
        pw.println(res);
        pw.close();
    }
}


import java.io.IOException;

public class A_62 {

    public static void main(String[] args) {
        try {
            java.io.BufferedReader stdin = new java.io.BufferedReader(
                    new java.io.InputStreamReader(System.in));
            String line;

            line = stdin.readLine();

            String[] numbers = line.split(" ");
            int p1 = Integer.parseInt(numbers[0]);
            int p2 = Integer.parseInt(numbers[1]);
            int p3 = Integer.parseInt(numbers[2]);
            int p4 = Integer.parseInt(numbers[3]);
            int a = Integer.parseInt(numbers[4]);
            int b = Integer.parseInt(numbers[5]);

            int min = Math.min(p1, Math.min(p2, Math.min(p3, p4)));
            if (a < min) {
                System.out.println(min - a);
            } else {
                System.out.println("0");
            }

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

}


import java.util.Scanner;

public class B {
        static int cost[][]=new int[3][3];
        public static void main(String cd[]){
            Scanner in=new Scanner(System.in);
            for(int i=0;i<3;i++){
                for(int j=0;j<3;j++){
                    cost[i][j]=in.nextInt();
                }
            }
            int n=in.nextInt();
            int dp[][][] =new int[3][3][n+1];
            for(int k=1;k<n+1;k++){
            for(int i=0;i<3;i++){
                for(int j=0;j<3;j++){
                    int x=3-(i+j);
                    if(i==j){
                        dp[i][j][k]=0;
                        continue;
                    }
                    dp[i][j][k]=Math.min(dp[i][x][k-1]+dp[x][j][k-1]+cost[i][j],2*dp[i][j][k-1]+dp[j][i][k-1]+cost[i][x]+cost[x][j]);
                }
            }
            }
            System.out.println(dp[0][2][n]);
        }

}


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

public class Main {
    final static boolean debug = false;
    final static String fileName = "";
    final static boolean useFiles = false;

    public static void main(String[] args) throws FileNotFoundException {
        long start;
        if (debug)
            start = System.nanoTime();
        InputStream inputStream;
        OutputStream outputStream;
        if (useFiles) {
            inputStream = new FileInputStream(fileName + ".in");
            outputStream = new FileOutputStream(fileName + ".out");
        } else {
            inputStream = System.in;
            outputStream = System.out;
        }
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task(in, out);
        solver.solve();
        if(debug)
            out.println((System.nanoTime() - start) / 1e+9);
        out.close();
    }
}
class Task {
    public void solve() {
        int[][] a = new int[3][3];
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                a[i][j] = in.nextInt();
        int n = in.nextInt();
        long[][][] dp = new long[3][3][n + 1];
        for (int i = 1; i <= n; i++) {
            for (int from = 0; from < 3; from++) {
                for (int to = 0; to < 3; to++) {
                    if (from == to)
                        continue;
                    int other = 3 - (from + to);
                    dp[from][to][i] = dp[from][other][i - 1] + a[from][to] + dp[other][to][i - 1];
                }
            }
        }
        out.println(dp[0][2][n]);
    }

    private InputReader in;
    private PrintWriter out;

    Task(InputReader in, PrintWriter out) {
        this.in = in;
        this.out = out;
    }
}

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 double nextDouble(){
        return Double.parseDouble(next());
    }

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

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

    public byte nextByte(){
        return Byte.parseByte(next());
    }
}


import java.util.*;
public class Main{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
int[][] t = new int[3][3];
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
  t[i][j]=sc.nextInt();
int k = sc.nextInt();
int[][][] r = new int[k+1][3][3];
for (int i=1; i<=k; i++){
  for (int a = 0; a<3; a++){
    for (int b=0; b<3; b++){
      if (a==b) continue;
      int c = 1^2^a^b;
      int x1 = r[i-1][a][c] + t[a][b] + r[i-1][c][b];
      int x2 = r[i-1][a][b] + t[a][c] + r[i-1][b][a] + t[c][b] + r[i-1][a][b];
      r[i][a][b] = Math.min(x1,x2);
      //System.out.println(i+" "+a+"-"+b+":"+r[i][a][b]+"  ("+x1+","+x2+")");
    }
  }
}
System.out.println(r[k][0][2]);
}
}


import java.util.*;
public class Main{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
int[][] t = new int[3][3];
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
  t[i][j]=sc.nextInt();
int k = sc.nextInt();
long[][][] r = new int[k+1][3][3];
for (int i=1; i<=k; i++){
  for (int a = 0; a<3; a++){
    for (int b=0; b<3; b++){
      if (a==b) continue;
      int c = 1^2^a^b;
      long x1 = r[i-1][a][c] + t[a][b] + r[i-1][c][b];
      long x2 = r[i-1][a][b] + t[a][c] + r[i-1][b][a] + t[c][b] + r[i-1][a][b];
      r[i][a][b] = Math.min(x1,x2);
      //System.out.println(i+" "+a+"-"+b+":"+r[i][a][b]+"  ("+x1+","+x2+")");
    }
  }
}
System.out.println(r[k][0][2]);
}
}


import java.util.Scanner;

import static me.problems.Cellphone.Direction.*;

/**
 * Created by viosng on 09.07.2016.
 */
public class Cellphone {

    /*
      1, 2, 3
      4, 5, 6,
      7, 8, 9
         0
     */

    private static final int[] up = new int[]{8, -1, -1, -1, 1, 2, 3, 4, 5, 6};
    private static final int[] down = new int[]{-1, 4, 5, 6, 7, 8, 9, -1, 0, -1};
    private static final int[] left = new int[]{-1, -1, 1, 2, -1, 4, 5,-1, 7, 8};
    private static final int[] right = new int[]{-1, 2, 3, -1, 5, 6, -1, 8, 9, -1};

    enum Direction {
        UP(up), DOWN(down), LEFT(left), RIGHT(right);
        int[] direction;

        Direction(int[] direction) {
            this.direction = direction;
        }
    }

    private static boolean toDirection(int[] m, Direction direction) {
        for (int i = 0; i < m.length; i++) {
            if (direction.direction[m[i]] < 0) return false;
        }
        for (int i = 0; i < m.length; i++) {
            m[i] = direction.direction[m[i]];
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(in.nextLine());
        int[] m = in.nextLine().chars().map(c -> c - '0').toArray();
        if (toDirection(m, UP) || toDirection(m, DOWN) || toDirection(m, LEFT) || toDirection(m, RIGHT)) {
            System.out.println("NO");
        } else {
            System.out.println("YES");
        }
    }
}


import java.util.*;

public class Main {
    private static Scanner reader = new Scanner(System.in);

    private static String move(String in, int i) {
        String oup = "";
        if (i == 0) {
            for (int j = 0; j < in.length(); j++) {
                char p = in.charAt(j);
                if (p == '0')
                    oup += '8';
                else
                    oup += (char) (p - 3);
            }
        } else if (i == 1) {
            for (int j = 0; j < in.length(); j++) {
                char p = in.charAt(j);
                if (p == '0')
                    oup += 'X';
                else if (p == '8')
                    outp += '8';
                else
                    oup += (char) (p + 3);
            }
        } else if (i == 2) {
            for (int j = 0; j < in.length(); j++) {
                char p = in.charAt(j);
                if (p == '3' || p == '6' || p == '9' || p == '0')
                    oup += 'X';
                else
                    oup += (char) (p + 1);
            }
        } else if (i == 3) {
            for (int j = 0; j < in.length(); j++) {
                char p = in.charAt(j);
                if (p == '1' || p == '4' || p == '7' || p == '0')
                    oup += 'X';
                else
                    oup += (char) (p - 1);
                ;
            }
        }
        return oup;
    }

    public static void main(String[] args) {
        int n = reader.nextInt();
        String s = reader.next();
        for (int i = 0; i < 4; i++) {
            boolean flag = true;
            String m = move(s, i);
            for (int j = 0; j < m.length(); j++) {
                char p = m.charAt(j);
                if (!Character.isDigit(p)) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                System.out.print("NO");
                return;
            }
        }
        System.out.print("YES");

    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException {
		MyScanner sc = new MyScanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int n=sc.nextInt();
		String s=sc.nextLine();
		boolean []c=new boolean[10];
		for(int i=0;i<s.length();i++){
		    int o=Integer.parseInt(s.substring(i,i+1));
		    if(!c[o])c[o]=true;
		}
		if((c[0]&&c[1]) || (c[0]&&c[2]) ||(c[0]&&c[3]) || (c[1]&&c[9]) || (c[3]&&c[7]))
                pw.println("YES");
        else
            if((in[1] || in[2] || in[3])&&(in[9] || in[6] || in[3])&&(in[1] || in[4] || in[7])&&(in[7] || in[8] || in[9]))
                pw.println("YES");
            else
                pw.println("NO");

		pw.close();
	}

	static class MyScanner {
		StringTokenizer st;
		BufferedReader br;

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

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

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

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

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

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

		}
public void waitForInput() throws InterruptedException {Thread.sleep(1000);}
		public boolean ready() throws IOException {
			return br.ready();
		}

}
}


        import java.util.*;
        import java.io.*;
        /**
         *
         * @author umang
         */

        public class A689 {

                public static void main(String[] args) {

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

                        int n = in.nextInt();
						String s = in.readString();

                        char[][] arr = new char[4][3];
                        int i,j;
                        for(i=0;i<n;i++){
							switch(s.charAt(i)){
								case '1':
									arr[0][0]=1;
									break;
								case '2':
									arr[0][1]=1;
									break;
								case '3':
									arr[0][2]=1;
									break;
								case '4':
									arr[1][0]=1;
									break;
								case '5':
									arr[1][1]=1;
									break;
								case '6':
									arr[1][2]=1;
									break;
								case '7':
									arr[2][0]=1;
									break;
								case '8':
									arr[2][1]=1;
									break;
								case '9':
									arr[2][2]=1;
									break;
								case '0':
									arr[3][1]=1;
									break;
							}
						}
                        boolean flag = false;

                        if(arr[0][1]==1 && arr[0][2]==1 && arr[2][0]==1) flag=true;
                        else if(arr[0][1]==1 && arr[1][1]==1 && arr[2][1]==1 && arr[3][1]==1 ) flag=true;
                        else if(arr[0][0]==1 && arr[2][2]==1) flag=true;
                        else if(arr[0][2]==1 && arr[2][0]==1 ) flag=true;

						if(flag) w.p3rintln("YES");
						else w.println("NO");
                        w.close();
                }

                static class InputReader {

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

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

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

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

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

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

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

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

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


import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class D {
    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        InputReader in=new InputReader(System.in);
        long x0=in.nextLong();long y0=in.nextLong();
        long ax=in.nextLong();long ay=in.nextLong();
        long bx=in.nextLong();long by=in.nextLong();
        long x=in.nextLong(),y=in.nextLong();
        long t=in.nextLong();
        List<long[]> position=new ArrayList<>();
        int minindex=0;
        long min=Math.abs(x0-x)+Math.abs(y0-y);
        position.add(new long[]{x0,y0});
        x0=x0*ax+bx;
        y0=y0*ay+by;
        while ((Math.abs(x0-position.get(0)[0])+Math.abs(y0-position.get(0)[1]))<=1e16){
            position.add(new long[]{x0,y0});
            long temp=Math.abs(x0-x)+Math.abs(y0-y);
            if(temp<min) minindex=position.size()-1;
            x0=x0*ax+bx;
            y0=y0*ay+by;
        }
        int index=minindex;
        x0=position.get(index)[0];;
        y0=position.get(index)[1];
        long curtime=0;
        int res=0;
        boolean flag=true;
        while(curtime<=t){
            long temp=Math.abs(x0-x)+Math.abs(y0-y);
            curtime+=temp;
            x=x0;y=y0;
            if(flag) index--;
            else index++;
            if(index<0) index=minindex+1;
            x0=position.get(index)[0];;
            y0=position.get(index)[1];
            res++;
        }
        out.println(res-1);
        out.flush();
    }
    static class InputReader{
        private BufferedReader in;
        private StringTokenizer tokenizer;
        public InputReader(InputStream stream){
            in = new BufferedReader(new InputStreamReader(stream),32768);
            tokenizer = null;
        }
        public String next() throws IOException{
            while(tokenizer==null || !tokenizer.hasMoreTokens()){
                tokenizer = new StringTokenizer(in.readLine());
            }
            return tokenizer.nextToken();
        }
        public Long nextLong() throws IOException {
            String next = next();
            return Long.valueOf(next);
        }
        public int nextInt() throws IOException{
            return Integer.valueOf(next());
        }
    }
}


import java.util.*;
import java.io.*;
public class R614D1B {
    private static List<long[]> pts;
    private static boolean[] taken;
    private static int ans;
    private static long T;
    private static boolean btwn(long x, long a, long b) {
        return Math.min(a,b)<=x && x<=Math.max(a,b);
    }
    private static long dist(long[] a, long[] b) {
        return Math.abs(a[0]-b[0])+Math.abs(a[1]-b[1]);
    }
    private static void find(int id, int cnt, long time) {
        //System.err.println(id+" "+cnt+" "+time);
        if (time>T)
            return;
        ans=Math.max(ans,cnt);
        taken[id]=true;
        for (int i=0; i<pts.size(); i++) {
            if (taken[i])
                continue;
            boolean good=true;
            for (int j=0; j<pts.size(); j++)
                if (!taken[j] &&
                        j!=i &&
                        btwn(pts.get(j)[0],pts.get(id)[0],pts.get(i)[0]) &&
                        btwn(pts.get(j)[1],pts.get(id)[1],pts.get(i)[1])) {
                    good=false;
                    break;
                }
            if (!good)
                continue;
            long d=dist(pts.get(id),pts.get(i));
            if (time>T-d)
                continue;
            find(i,cnt+1,time+d);
        }
        taken[id]=false;
    }
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(in.readLine());
        long x0=Long.parseLong(tok.nextToken()),
                y0=Long.parseLong(tok.nextToken()),
                ax=Long.parseLong(tok.nextToken()),
                ay=Long.parseLong(tok.nextToken()),
                bx=Long.parseLong(tok.nextToken()),
                by=Long.parseLong(tok.nextToken());
        tok=new StringTokenizer(in.readLine());
        long xs=Long.parseLong(tok.nextToken()),
                ys=Long.parseLong(tok.nextToken());
        T=Long.parseLong(tok.nextToken());
        /*
        if for som i x_i>2^63-1
        max xs=10^16
        x_i-xs>10^16>t
         */
        pts=new ArrayList<>();
        for (long x=x0, y=y0; true;) {
            pts.add(new long[] {x,y});
            //System.out.println(Arrays.toString(new long[] {x,y}));
            if (x>(double)(Long.MAX_VALUE-bx)/ax
                    || y>(double)(Long.MAX_VALUE-by)/ay)
                break;
            x=ax*x+bx;
            y=ay*y+by;
        }
        taken=new boolean[pts.size()];
        ans=0;
        for (int i=0; i<pts.size(); i++)
            find(i,1,dist(pts.get(i),new long[] {xs,ys}));
        System.out.println(ans);
        System.out.flush();
        /*long[] cost=new long[pts.size()];
        boolean[] bad=new boolean[pts.size()];
        for (int i=0; i<pts.size(); i++) {
            long[] pt=pts.get(i);
            long dx=Math.abs(pt[0]-xs),
                    dy=Math.abs(pt[1]-ys);
            if (dx>Long.MAX_VALUE-dy)
                bad[i]=true;
            else
                cost[i] = dx+dy;
        }
        List<Integer> front=new ArrayList<>();
        for (int i=0; i<pts.size(); i++)
            if (!bad[i])
                front.add(i);
        while (front.size()>0) {

        }*/
    }
}


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

    public class AromaSearch {
    	public static void main(String[] args) throws IOException {
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		String[] line = br.readLine().split(" ");
    		long x0 = Long.parseLong(line[0]);
    		long y0 = Long.parseLong(line[1]);
    		int ax = Long.parseLong(line[2]);
    		int ay = Long.parseLong(line[3]);
    		long bx = Long.parseLong(line[4]);
    		long by = Long.parseLong(line[5]);
    		line = br.readLine().split(" ");
    		long sx = Long.parseLong(line[0]);
    		long sy = Long.parseLong(line[1]);
    		long t = Long.parseLong(line[2]);
    		ArrayList<Long> xs = new ArrayList<Long>();
    		ArrayList<Long> ys = new ArrayList<Long>();
    		ArrayList<Long> dist = new ArrayList<Long>();
    		xs.add(x0);
    		ys.add(y0);
    		dist.add((long) 0);
    		long ttemp = t;
    		while (true) {
    			long tx = x0;
    			long ty = y0;
    			x0 *= ax;
    			x0 += bx;
    			y0 *= ay;
    			y0 += by;
    			long d = x0-tx+y0-ty;
    			xs.add(x0);
    			ys.add(y0);
    			dist.add(dist.get(dist.size() - 1)+d);
    			ttemp -= d;
    			if (ttemp <= 0) {
    				break;
    			}
    		}
    		long ans = 0;
    		for (int i = 0; i < xs.size(); i++) {
    			long cx = xs.get(i);
    			long cy = ys.get(i);
    			ttemp = t-dist(sx, sy, cx, cy);
    			long tans = 0;
    			if (ttemp <= 0) {
    				tans = 0;
    			} else if (ttemp >= dist.get(i)) {
    				ttemp -= dist.get(i);
    				tans = i+1;
    				for (int j = i+1; j < xs.size(); j++) {
    					if (ttemp <= dist.get(j)) {
    						tans = j;
    						break;
    					}
    				}
    			} else {
    				for (int j = i; j >= 0; j--) {
    					if (ttemp <= dist.get(i) - dist.get(j)) {
    						tans = i-j+1;
    						break;
    					}
    				}
    			}
    			ans = Long.max(tans, ans);
    		}
    		System.out.println(ans);
    	}
    	static long dist(long x1, long y1, long x2, long y2) {
    		return Math.abs(x1-x2) + Math.abs(y1-y2);
    	}
    }


import java.util.*;
public class Main {

	public static void main (String[] args) {
	    Scanner s = new Scanner(System.in);
		long x0 = s.nextLong();
		long y0 = s.nextLong();
		long ax = s.nextLong();
		long ay = s.nextLong();
		long bx = s.nextLong();
		long by = s.nextLong();
		long xs = s.nextLong();
		long ys = s.nextLong();
		long t = s.nextLong();
		ArrayList<Long> x = new ArrayList<>();
		ArrayList<Long> y = new ArrayList<>();
		long temp = Math.abs(xs-x0) + Math.abs(ys-y0);
		x.add(x0);
		y.add(y0);
		long LIMIT = ((long)1 << 62) - 1;
		int n = x.size();
		while ((LIMIT - bx) / ax >= x.get(n - 1) && (LIMIT - by) / ay >= y.get(n - 1)) {
			x.add(ax * x.get(n - 1) + bx); y.add(ay * y.get(n - 1) + by); n++;
		}
		int answer = 0;
		int size = x.size();
		long[][] dp = new long[size][size];
		for(int l=0;l<size;l++)
		{
		    for(int r=l;r<size;r++)
		    {
		        if(l==r)
		        {
		            dp[l][r] = 0;
		            long yy = Math.abs(xs-x.get(l)) + Math.abs(ys-y.get(l)) + dp[l][r];
		            if(yy <=t)
		            {
		                int z = r-l+1;
		                if(z>answer)
		                answer = z;
		            }
		        }
		        else
		        {
		            //dp[l][r] = dp[l][l+1] + dp[l+1][r];
		            //if((r-l)==1)
		            //{
		                dp[l][r] = x.get(r)-x.get(l) + y.get(r) - y.get(l);
		            //}
		            long yy = Math.min( Math.abs(xs-x.get(l)) + Math.abs(ys-y.get(l)) ,
		                               Math.abs(xs-x.get(r)) + Math.abs(ys-y.get(r)) )+

		            if(yy <=(t-dp[l][r]))
		            {
		                int z = r-l+1;
		                if(z>answer)
		                answer = z;
		            }
		        }
		    }
		}
		System.out.println(answer);
	}
}


import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.TreeSet;
public class Main{
	static boolean prime[];
	static boolean checked[];
	public static void main(String[] args){
		FastScanner sc = new FastScanner();
		Mathplus mp = new Mathplus();
		prime = new boolean[100001];
		checked = new boolean[100001];
		for(int i=0;i<=100000;i++){
			checked[i] = false;
		}
		ArrayList<Integer>[] set =new ArrayList[100001];
		for(int i=0;i<=100000;i++){
			set[i] =new ArrayList<Integer>();
		}
		for(int i=100000;i>=1;i--){
			for(int j=1;j<=100000;j++){
				if(i*j>100000)break;
				set[i*j].add(i);
			}
		}

		int M = sc.nextInt();
		long[] dp = new long[M+1];
		dp[1] = 1;
		int mod =1000000007;
		for(int i=2;i<=M;i++){
			long tmp = 0;

			int[] num = new int[i+1];

			for(int e:set[i]){

				int n = M / e;
				n += num[e];
				for(int f:set[e]){
					if(e!=f){
						num[f] -= n;
					}
				}
				if(e!=i){
					tmp += dp[e] * n % mod;
				}
			}

			tmp+= M;
			tmp %= mod;
			tmp *= mp.rev(M-(M/i));
			dp[i] = tmp%mod;
		}
		long ans = 0;
		for(int i=1;i<=M;i++){
			ans += dp[i];
		}
		ans %= mod;
		System.out.println(ans*mp.rev(M)%mod);

	}

	static boolean isprime(int a){
		if(a==1)return false;
		if(checked[a]) return prime[a];
		checked[a] = true;
		for(int i=2;i*i<=a;i++){
			if(a%i==0){
				return prime[a] =false;
			}
		}
		return prime[a] = true;
	}

}

class UnionFindTree {
	int[] root;
	int[] rank;

	UnionFindTree(int N){
		root = new int[N];
		rank = new int[N];
		for(int i=0;i<N;i++){
			root[i] = i;
		}
	}

	public int find(int x){
		if(root[x]==x){
			return x;
		}else{
			return find(root[x]);
		}
	}

	public void unite(int x,int y){
		x = find(x);
		y = find(y);
		if(x==y){
			return;
		}else{
			if(rank[x]<rank[y]){
				root[x] = y;
			}else{
				root[y] = x;
				if(rank[x]==rank[y]){
					rank[x]++;
				}
			}
		}
	}

	public boolean same(int x,int y){
		return find(x)==find(y);
	}
}

class WeightedUnionFindTree extends UnionFindTree{
	int[] weight ;

	WeightedUnionFindTree(int N){
		super(N);
		weight = new int[N];
		for(int i=0;i<N;i++){
			weight[i] = 1;
		}
	}

	public void unite(int x,int y){
		x = find(x);
		y = find(y);
		if(x==y){
			return;
		}else{
			if(rank[x]<rank[y]){
				root[x] = y;
				weight[y] += weight[x];
			}else{
				root[y] = x;
				weight[x] += weight[y];
				if(rank[x]==rank[y]){
					rank[x]++;
				}
			}
		}
	}

}

class Graph {
	ArrayList<Edge>[] list;
	int size;
	TreeSet<LinkEdge> Edges = new TreeSet<LinkEdge>(new LinkEdgeComparator());
	@SuppressWarnings("unchecked")
	Graph(int N){
		size = N;
		list = new ArrayList[N];
		for(int i=0;i<N;i++){
			list[i] = new ArrayList<Edge>();
		}
	}

	void addEdge(int a,int b){
		list[a].add(new Edge(b,1));
	}

	void addWeightedEdge(int a,int b,int c){
		list[a].add(new Edge(b,c));
	}

	void addEgdes(int[] a,int[] b){
		int size = a.length;
		for(int i=0;i<size;i++){
			list[a[i]].add(new Edge(b[i],1));
		}
	}

	void addWeighterEdges(int[] a ,int[] b ,int[] c){
		int size = a.length;
		for(int i=0;i<size;i++){
			list[a[i]].add(new Edge(b[i],c[1]));
		}
	}

	long[] bfs(int s){
		long[] L = new long[size];
		for(int i=0;i<size;i++){
			L[i] = -1;
		}
		L[s] = 0;
		ArrayDeque<Integer> Q = new ArrayDeque<Integer>();
		Q.add(s);

		while(!Q.isEmpty()){
			int v = Q.poll();
			for(Edge e:list[v]){
				int w = e.to;
				long c = e.cost;
				if(L[w]==-1){
					L[w] = L[v] + c;
					Q.add(w);
				}
			}
		}

		return L;
	}

	long[] dijkstra(int s){
		long[] L = new long[size];
		for(int i=0;i<size;i++){
			L[i] = -1;
		}
		int[] visited = new int[size];
		L[s] = 0;
		PriorityQueue<Pair> Q = new PriorityQueue<Pair>(new SampleComparator());
		Q.add(new Pair(0,s));

		while(!Q.isEmpty()){

			Pair C = Q.poll();
			if(visited[(int)C.b]==0){
				L[(int)C.b] = C.a;
				visited[(int) C.b] = 1;
				for(Edge D:list[(int) C.b]){
					Q.add(new Pair(L[(int)C.b]+D.cost,D.to));
				}
			}
		}

		return L;
	}
	long Kruskal(){
		long ans = 0;

		UnionFindTree UF = new UnionFindTree(size);
		for(LinkEdge e:Edges){
			if(!UF.same(e.a,e.b)){
				ans += e.L;
				UF.unite(e.a,e.b);
			}
		}

		return ans;
	}

}

class LinkEdge{
	long L;
	int a ;
	int b;
	LinkEdge(long l,int A,int B){
		L = l;
		a = A;
		b = B;
	}
	public boolean equals(Object o){
		LinkEdge O = (LinkEdge) o;
		if(O.a==this.a&&O.b==this.b&&O.L==this.L){
			return true;
		}else{
			return false;
		}

	}

	public int hashCode(){
		return Objects.hash(L,a,b);
	}

}

class Edge{
	int to;
	long cost;

	Edge(int a,long b){
		to = a;
		cost = b;
	}

}
class LinkEdgeComparator implements Comparator<LinkEdge>{
	public int compare(LinkEdge P, LinkEdge Q) {
		long temp = P.L-Q.L;
		if(temp==0){
			if(P.a>Q.a){
				return 1;
			}else{
				if(P.b>Q.b){
					return 1;
				}else{
					return -1;
				}
			}

		}
		if(temp>=0){
			return 1;
		}else{
			return -1;
		}
	}

}
class Pair{
	long a;
	long b;

	Pair(long p,long q){
		this.a = p;
		this.b = q;
	}

	public boolean equals(Object o){
		Pair O = (Pair) o;
		if(O.a==this.a&&O.b==this.b){
			return true;
		}else{
			return false;
		}

	}

	public int hashCode(){
		return Objects.hash(a,b);
	}

}

class SampleComparator implements Comparator<Pair>{
	public int compare(Pair P, Pair Q) {
		long temp = P.a-Q.a;
		if(temp==0){
			if(P.b>Q.b){
				return 1;
			}else{
				return -1;
			}

		}
		if(temp>=0){
			return 1;
		}else{
			return -1;
		}
	}

}

class FastScanner {
    private final java.io.InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
    public boolean hasNext() { skipUnprintable(); return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return (minus ? -n : n);
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return (int) (minus ? -n : n);
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
}

class Mathplus{
	int mod = 1000000007;
	long[] fac = new long[1000001];
	boolean isBuild = false;
	int mindex = -1;
	int maxdex = -1;

	void buildFac(){
		fac[0] = 1;
		for(int i=1;i<=1000000;i++){
			fac[i] = (fac[i-1] * i)%mod;
		}
		isBuild = true;
	}
	long max(long[] a){
		long max = 0;
		for(int i=0;i<a.length;i++){
			if(max<a[i]){
				max =a[i];
				maxdex = i;
			}
		}
		return max;
	}
	int max(int[] a){
		int max = 0;
		for(int i=0;i<a.length;i++){
			if(max<a[i]){
				max =a[i];
				maxdex = i;
			}
		}
		return max;
	}
	long min(long[] a){
		long min = Long.MAX_VALUE;
		for(int i=0;i<a.length;i++){
			if(min>a[i]){
				min =a[i];
				mindex = i;
			}
		}
		return min;
	}
	int min(int[] a){
		int min = Integer.MAX_VALUE;
		for(int i=0;i<a.length;i++){
			if(min>a[i]){
				min =a[i];
				mindex = i;
			}
		}
		return min;
	}
	long sum(long[] a){
		long sum = 0;
		for(int i=0;i<a.length;i++){
			sum += a[i];
		}
		return sum;
	}
	long sum(int[] a){
		long sum = 0;
		for(int i=0;i<a.length;i++){
			sum += a[i];
		}
		return sum;
	}

	long gcd(long a, long b){
		if(a<b){
			a^=b;
			b^=a;
			a^=b;
		}
		if(a%b==0){
			return b;
		}else{
			return gcd(b,a%b);
		}
	}

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

	public long comb(int a,int num){
		if(!isBuild){
			buildFac();
		}
		return fac[a] * (rev(fac[num])*rev(fac[a-num])%mod)%mod;
	}

	long rev(long l) {
		return pow(l,mod-2);
	}

	long pow(long l, int i) {
		if(i==0){
			return 1;
		}else{
			if(i%2==0){
				long val = pow(l,i/2);
				return val * val % mod;
			}else{
				return pow(l,i-1) * l % mod;
			}
		}
	}

}


//package com.company;
// Always comment out package when submitting.

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

public class Main {
    public static class Task {

        public class Frac {
            long a, b;
            Frac(long a, long b) {
                this.a = a % mod;
                this.b = b % mod;
            }
            Frac add(Frac other) {
                return new Frac(a * other.b + b * other.a, b * other.b);
            }
            Frac mult(Frac other) {
                return new Frac(a * other.a, b * other.b);
            }

            @Override
            public String toString() {
                return Double.toString((a + 0.0) / b);
            }

            long pow(long a, int b) {
                if (b == 0) return 1;
                if (b % 2 == 0) return pow(a * a % mod, b >> 1);
                return a * pow(a * a % mod, b >> 1) % mod;
            }

            long inv(long a) {
                return pow(a , mod - 2);
            }

            long val() {
                return a * inv(b) % mod;
            }
        }

        int mod = 1_000_000_007;
        int[] factors;
        int[] phi;
        List<Integer> primes;
        Map<Integer, Integer>[] factor;
        int m;
        public void sieve() {
            int M = 100010;
            factors = new int[M];
            phi = new int[M];
            phi[1] = 1;
            primes = new ArrayList<>();
            for (int i = 2; i < M; i++) {
                if (factors[i] == 0) {
                    factors[i] = i;
                    primes.add(i);
                    phi[i] = i - 1;
                }
                for (int j : primes) {
                    int v = j * i;
                    if (v >= M) break;
                    factors[v] = j;
                    if (i % j == 0) {
                        phi[v] = phi[i] * j;
                        break;
                    } else {
                        phi[v] = phi[i] * phi[j];
                    }
                }
            }
            factor = new Map[M];
            for (int i = 1; i < M; i++) {
                factor[i] = factorize(i);
            }
        }

        public Map<Integer, Integer> factorize(int t) {
            Map<Integer, Integer> factor = new HashMap<>();
            while (t != 1) {
                int f = factors[t];
                int cnt = 0;
                while (t % f == 0) {
                    t /= f;
                    cnt ++;
                }
                factor.put(f, cnt);
            }
            return factor;
        }

        void fr(Map<Integer, Integer> visit, Map<Integer, Integer> fa, int f) {
            Map<Integer, Integer> fb = factor[f];
            List<Integer> forbidPrimes = new ArrayList<>();
            for (Map.Entry<Integer, Integer> en: fa.entrySet()) {
                if (!fb.getOrDefault(en.getKey(), -1).equals(en.getValue())) {
                    forbidPrimes.add(en.getKey());
                }
            }
            int n = forbidPrimes.size();
            int chooseFrom = m / f;
            int tot = chooseFrom;
            for (int i = 1; i < 1 << n; i++) {
                int on = 0;
                int val = 1;
                for (int j = 0; j < n; j++) {
                    if (((1 << j) & i) != 0) {
                        val *= forbidPrimes.get(j);
                        on += 1;
                    }
                }
                tot = tot + (on == 1 ? -1 : 1) * chooseFrom / val;
            }
            visit.put(f, tot);
        }

        Frac[] dp;
        int gcd(int a, int b) {
            return b == 0 ? a : gcd(b, a % b);
        }
        public Frac get(int y) {
            if (y == 1) return new Frac(0, 1);
            if (dp[y] != null) return dp[y];
            Map<Integer, Integer> visit = new HashMap<>();
            Map<Integer, Integer> fa = factor[y];
            for (int f = 1; f <= y; f++) {
                if (y % f == 0) {
                    fr(visit, fa, f);
                    if (f * f != y) {
                        fr(visit, fa, y / f);
                    }
                }
            }
            Frac expect = new Frac(0, 1);
            int x = visit.get(y);
            for (Map.Entry<Integer, Integer> en: visit.entrySet()) {
                if (en.getKey() == y) {
                    continue;
                }
                Frac prob = new Frac(en.getValue(), m - x);
                expect = expect.add(prob.mult(get(en.getKey()).add(new Frac(1, 1))));
            };
            return dp[y] = expect.add(new Frac(x, m - x));
        }

        public void solve(Scanner sc, PrintWriter pw) throws IOException {
            sieve();
            m = sc.nextInt();
            dp = new Frac[m + 1];
            Frac tot = new Frac(0, 1);
            for (int i = 1; i <= m; i++) {
                tot= tot.add(get(i).add(new Frac(1, 1)));
            }
            Frac ret = tot.mult(new Frac(1 , m));
            pw.println(ret.val());
//            pw.println(ret.toString());
        }
    }

    // template, actual code is in class Task.
    static long TIME_START, TIME_END;

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
//        Scanner sc = new Scanner(new FileInputStream("Test.in"));
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
//        PrintWriter pw = new PrintWriter(new FileOutputStream("Test.out"));
        Runtime runtime = Runtime.getRuntime();
        long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory();
        TIME_START = System.currentTimeMillis();
        Task t = new Task();
        t.solve(sc, pw);
        TIME_END = System.currentTimeMillis();
        long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory();
        pw.close();
//        System.err.println("Memory increased: " + (usedMemoryAfter - usedMemoryBefore) / 1000000);
//        System.err.println("Time used: " + (TIME_END - TIME_START) + ".");
    }

    // Faster IO with BufferedReader wrapped with Scanner
    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

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

        public Scanner(FileReader s) throws FileNotFoundException {
            br = new BufferedReader(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());
        }

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


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

public class SolutionD {
    static long fast_pow(long a, long b) {
        if(b == 0)
            return 1L;
        long val = fast_pow(a, b / 2);
        if(b % 2 == 0)
            return val * val % mod;
        else
            return val * val % mod * a % mod;
    }
    static void solve(int lvl, int maxlvl, int[] primes, long baseVal, long curVal, long m, int parity) {
        if(lvl == maxlvl) {
            long poss = m / (baseVal * curVal);
            if(parity == 0)
                cnt += poss;
            else
                cnt -= poss;

            return;
        }

        solve(lvl + 1, maxlvl, primes, baseVal, curVal, m, parity);
        solve(lvl + 1, maxlvl, primes, baseVal, curVal * primes[lvl], m, parity ^ 1);
    }
    static long cnt = 0;
    static long mod = (long)1e9 + 7;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int m = sc.nextInt();

        int primes[][] = new int[m + 1][6];
        int ptr[] = new int[m + 1];

        for(int i = 2; i <= m; ++i) {
            if(ptr[i] == 0) {
                for(int j = i; j <= m; j += i) {
                    primes[j][ptr[j]++] = i;
                }
            }
        }

        long prob[] = new long[m + 1];
        long ans[] = new long[m + 1];
        ans[0] = 0;
        prob[0] = 1;
        long invm = fast_pow(m, mod - 2);
        for(int i = m; i >= 2; --i) {
            long multiples = m / i;
            prob[i] = fast_pow(m - multiples, mod - 2);
            long num = m;
            long den = (m - multiples) * (m - multiples) % mod;
            ans[i] = num * fast_pow(den, mod - 2) % mod;

            for(int j = 2 * i; j <= m; j += i) {
                int val = j / i;
                cnt = 0;
                solve(0, ptr[val], primes[val], i, 1, m, 0);
                long curProb = cnt * fast_pow(m - multiples, mod - 2) % mod;
                num = m * cnt % mod;
                den = (m - multiples) * (m - multiples) % mod;
                long curEv = (num * fast_pow(den, mod - 2) % mod * fast_pow(curProb, mod - 2) % mod + ans[j] % mod) % mod;
                curProb = curProb * prob[j] % mod;
                ans[i] += curEv * curProb % mod;
                prob[i] += curProb;
            }

            prob[i] %= mod;
            ans[i] %= mod;
            ans[i] = ans[i] * fast_pow(prob[i], mod - 2) % mod;
        }

        long fans = 0;
        long fprob = invm;
        for(int i = 2; i <= m; ++i) {
            int val = i;
            cnt = 0;
            solve(0, ptr[val], primes[val], 1, 1, m, 0);
            long curProb = prob[i] * cnt % mod * invm % mod;
            fans += curProb * ans[i] % mod;
            fprob += curProb;
        }
        fans %= mod;
        fprob %= mod;

        fans = (fans * fast_pow(fprob, mod - 2) % mod + 1L) % mod;

        System.out.print(fans);
    }
}


public class Main {

  private static void solve() {
    int m = ni();

    int mod = 1000000000 + 7;

    int[] primes = sieveEratosthenes(m + 1);

    long[] dp = new long[m + 1];

    long ret = 0;
    outer: for (int g = 2; g <= m; g ++) {
      dp[g] = f(m, g, mod);

      int[][] f = factor(g, primes);

        for (int[] v : f) {
          if (v[1] > 1) {
            continue outer;
          }
        }
        ret += dp[g] * (f.length % 2 == 1 ? 1 : (mod-1)) %mod;
        ret %=mod;
    }

    ret += inv(m, mod);
    ret %= mod;

    System.out.println(ret);

  }

  private static void dump(int mod) {
    for (int i = 1; i < 20; i ++) {
      for (int j = 1; j < 20; j ++) {
        System.out.println(i + " " + j + " " + inv(j, mod) * i % mod);
      }
    }
  }
  private static long f(long m, long g, int mod) {
    long a = (m/g) * inv(m, mod) % mod;
    long c = (m - (m/g) + mod) * inv(m/g, mod) % mod;

    long b = inv((mod-1+a) * (mod-1+a) % mod, mod) * a % mod;
    long ret = c * (mod + b - a) % mod;
    return ret;
  }

  public static long[][] enumDivisors(int[][] f)
  {
      int n = 1;
      for(int j = 0;j < f.length;j++){
          n *= f[j][1]+1;
      }
      long[][] divs = new long[n][2];
      int p = 1;
      divs[0][0] = 1;
      divs[0][1] = 1;
      for(int j = 0;j < f.length;j++){
          for(int q = p-1;q >= 0;q--){
              long b = divs[q][0];
              long s = -divs[q][1];
              for(int k = 0;k < f[j][1];k++){
                  b *= f[j][0];
//                if((long)b*b <= n)divs[p++] = b;
                  divs[p][1] = s;
                  divs[p++][0] = b;
                  s = -s;
              }
          }
      }
      long s = divs[n-1][1];
      for (int i = 0; i < n; i ++) {
        divs[i][1] *= s;
      }
      return divs;
  }

  public static int[][] factor(int n, int[] primes) {
    int[][] ret = new int[9][2];
    int rp = 0;
    for (int p : primes) {
      if (p * p > n)
        break;
      int i;
      for (i = 0; n % p == 0; n /= p, i++);
      if (i > 0) {
        ret[rp][0] = p;
        ret[rp][1] = i;
        rp++;
      }
    }
    if (n != 1) {
      ret[rp][0] = n;
      ret[rp][1] = 1;
      rp++;
    }
    return Arrays.copyOf(ret, rp);
  }

  public static int[] sieveEratosthenes(int n) {
    if (n <= 32) {
      int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
      for (int i = 0; i < primes.length; i++) {
        if (n < primes[i]) {
          return Arrays.copyOf(primes, i);
        }
      }
      return primes;
    }

    int u = n + 32;
    double lu = Math.log(u);
    int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)];
    ret[0] = 2;
    int pos = 1;

    int[] isnp = new int[(n + 1) / 32 / 2 + 1];
    int sup = (n + 1) / 32 / 2 + 1;

    int[] tprimes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
    for (int tp : tprimes) {
      ret[pos++] = tp;
      int[] ptn = new int[tp];
      for (int i = (tp - 3) / 2; i < tp << 5; i += tp)
        ptn[i >> 5] |= 1 << i;
      for (int j = 0; j < sup; j += tp) {
        for (int i = 0; i < tp && i + j < sup; i++) {
          isnp[j + i] |= ptn[i];
        }
      }
    }

    // 3,5,7
    // 2x+3=n
    int[] magic = {0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10,
        7, 12, 21, 17, 9, 6, 16, 5, 15, 14};
    int h = n / 2;
    for (int i = 0; i < sup; i++) {
      for (int j = ~isnp[i]; j != 0; j &= j - 1) {
        int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27];
        int p = 2 * pp + 3;
        if (p > n)
          break;
        ret[pos++] = p;
        if ((long) p * p > n)
          continue;
        for (int q = (p * p - 3) / 2; q <= h; q += p)
          isnp[q >> 5] |= 1 << q;
      }
    }

    return Arrays.copyOf(ret, pos);
  }

  public static long inv(long a, int p)
  {
      long ret = 1;
      long mul = a;
      for(long n = p-2;n > 0;n >>>= 1){
          if((n&1)==1)ret = ret * mul % p;
          mul = mul * mul % p;
      }
      return ret;
  }

  public static long pow(long a, long n, long mod)
  {
      long ret = 1; // 1%mod if mod=1,n=0
      int x = 63-Long.numberOfLeadingZeros(n);
      for(;x >= 0;x--){
          ret = ret * ret % mod;
          if(n<<~x<0)ret = ret * a % mod;
      }
      return ret;
  }

  public static void main(String[] args) {
    new Thread(null, new Runnable() {
      @Override
      public void run() {
        long start = System.currentTimeMillis();
        String debug = args.length > 0 ? args[0] : null;
        if (debug != null) {
          try {
            is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug));
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
        }
        reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768);
        solve();
        out.flush();
        tr((System.currentTimeMillis() - start) + "ms");
      }
    }, "", 64000000).start();
  }

  private static java.io.InputStream is = System.in;
  private static java.io.PrintWriter out = new java.io.PrintWriter(System.out);
  private static java.util.StringTokenizer tokenizer = null;
  private static java.io.BufferedReader reader;

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

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

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

  private static int[] na(int n) {
    int[] a = new int[n];
    for (int i = 0; i < n; i++)
      a[i] = ni();
    return a;
  }

  private static char[] ns() {
    return next().toCharArray();
  }

  private static long[] nal(int n) {
    long[] a = new long[n];
    for (int i = 0; i < n; i++)
      a[i] = nl();
    return a;
  }

  private static int[][] ntable(int n, int m) {
    int[][] table = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[i][j] = ni();
      }
    }
    return table;
  }

  private static int[][] nlist(int n, int m) {
    int[][] table = new int[m][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[j][i] = ni();
      }
    }
    return table;
  }

  private static int ni() {
    return Integer.parseInt(next());
  }

  private static void tr(Object... o) {
    if (is != System.in)
      System.out.println(java.util.Arrays.deepToString(o));
  }
}


import java.util.Scanner;

public final class EcRound35BApplication {

	public static void main(String[] args) {

//		System.out.println("test start");
//		app.tester(1,5,2,3,1);
//		app.tester(2,4,7,10,3);
//		System.out.println("test finished");

		Input input = new Input();
		input = SystemInput();
		Integer result = run(input);
		System.out.print(result);

	}

	private void tester(Integer no,Integer participant , Integer pieceA , Integer pieceB,Integer answer) {
		Input input = new Input();
		input.setInput(participant , pieceA , pieceB);
		Integer result = run(input);

		if(result == answer) {
			System.out.println("No." + no + ":OK");
		}
		else {
			System.out.println("No." + no + ":failed");
			System.out.println("result:" + result);
			System.out.println("answer:" + answer);
		}
	}

	private static Input SystemInput() {
		Input input = new Input();
		Scanner sc = new Scanner(System.in);
		Integer participant = sc.nextInt();
		Integer pieceA = sc.nextInt();
		Integer pieceB = sc.nextInt();
		input.setInput(participant,pieceA,pieceB);
		sc.close();
		return input;
	}

	private static Integer run(Input input) {
		Integer result;

		result = simpleDivision(input);

		//�������؃��[�v
		while(!checkResult(input,result)) {
			//�S����result�s�[�X���z��Ȃ������ꍇ�Aresult�������čČ���
			result --;
		}

		return result;
	}

	private Integer simpleDivision(Input input) {
		Integer result;
		Integer allPieces;
		//���v
		allPieces = input.getPieceA() + input.getPieceB();

		result = allPieces / input.getParticipant();

		return result;
	}

	private boolean checkResult(Input input , Integer result) {
		boolean isDistribute = true;
		Integer pieceA = input.getPieceA();
		Integer pieceB = input.getPieceB();
		Integer participants = input.getParticipant();

		//A���猟��
		while(pieceA > 0) {
			//������������Ȃ��ꍇ�͒E�o����
			if(pieceA < result) {
				break;
			}
			//����Ă���Ί��蓖�Ă�
			participants --;
			pieceA = pieceA - result;
		}

		//B������
		//B�̐����������~�c�l���ɑ���Ă��Ȃ���΃A�E�g
		if(pieceB < participants * result) {
			isDistribute = false;
		}

		return isDistribute;
	}

	private static class Input{
		private Integer participant;
		private Integer pieceA;
		private Integer pieceB;

		public void setInput( Integer participant , Integer pieceA , Integer pieceB) {
			this.participant = participant;
			this.pieceA = pieceA;
			this.pieceB = pieceB;

		}

		public Integer getParticipant() {
			return participant;
		}

		public void setParticipant(Integer participant) {
			this.participant = participant;
		}

		public Integer getPieceA() {
			return pieceA;
		}

		public void setPieceA(Integer pieceA) {
			this.pieceA = pieceA;
		}

		public Integer getPieceB() {
			return pieceB;
		}

		public void setPieceB(Integer pieceB) {
			this.pieceB = pieceB;
		}
	}
}


import ava.util.*;

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

        int max = 0;
        for(int i = 1 ; i <= n - 1 ; i++)
        {
            int minCakes = Math.min(a/i , n - i/b);
            if(minCakes > max)
            max = minCakes;

        }

        System.out.println(max);
    }
}


 import java.util.Scanner;

public class test
{
	private static Scanner sc;
	public static void main(String[] args)
	{
		sc = new Scanner(System.in);
		int n=sc.nextInt(),a=sc.nextInt(),b=sc.nextInt();
		for(int a=Math.min(i, j);a>0;i--)
		{
			if(a/i+b/i>=n)
			{
				System.out.println(i);
				break;
			}
		}
	}
}


import java.util.*;

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

        int max = 0;
        for(int i = 1 ; i <= n - 1 ; i++)
        {
            if(a/i != 0 && b/(n - i) != 0)
            {
                int minCakes = Math.min(a/i , b/(n - i));
                if(minCakes > max)
                maxx = minCakes;
            }

        }

        System.out.println(max);
    }
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.io.InputStream;

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

class TaskC {
    public void solve(int testNumber, FastScanner in, FastPrinter out) {
        int n = in.nextInt();
        int[] g = new int[n + 1];
        int[] g2 = new int[n + 1];
        g[1] = g[2] = 0;
        int[] v = new int[n + 1];
        int[] move = new int[n + 1];
        Arrays.fill(move, -1);
        for (int i = 3; i <= n; i++) {
            for (int z = 2; z * (z + 1) / 2 <= i; z++) {
                if ((z & 1) == 1) {
                    if (i % z == 0) {
                        int q = g2[i / z + z / 2] ^ g2[i / z - z / 2 - 1];
                        v[q] = i;
                        if (q == 0 && move[i] < 0) {
                            move[i] = z;
                        }
                    }
                } else {
                    if (i % z == z / 2) {
                        int q = g2[i / z + z / 2] ^ g2[i / z - z / 2];
                        v[q] = i;
                        if (q == 0 && move[i] < 0) {
                            move[i] = z;
                        }
                    }
                }
            }
            for (int k = 0; ; k++) {
                if (v[k] != i) {
                    g[i] = k;
                    break;
                }
            }
            g2[i] = g2[i - 1] ^ g[i];
        }
        if (g[n] == 0) {
            out.println(-1);
            return;
        }
        out.println(move[n]);
    }
}

class FastScanner extends BufferedReader {

    boolean isEOF;

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

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

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

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

    }

class FastPrinter extends PrintWriter {

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

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

}


import java.util.*;

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

    public InterestingGame() {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();

        int colors[] = new int[10010];
        colors[1] = 0;
        colors[2] = 0;
        for (int i = 3; i < N; i++) {
            Set<Integer> set = new HashSet<Integer>();
            for (int k = 2; k < 500; k++) {
                int minSum = k * (k + 1) / 2;
                if (minSum > i)
                    break;
                if ((i - minSum) % k != 0)
                    continue;
                int first = (i - minSum) / k;
                int state = 0;
                for (int j = 1; j <= k; j++) {
                    state ^= colors[j + first];
                }
                set.add(state);
            }
            int x = 0;
            while (set.contains(x)) x++;
            colors[i] = x;
        }
        for (int k = 2; k < 500; k++) {
            int minSum = k * (k + 1) / 2;
            if (minSum > N)
                break;
            if ((N - minSum) % k != 0)
                continue;
            int first = (N - minSum) / k;
            int state = 0;
            for (int j = 1; j <= k; j++) {
                state ^= colors[j + first];
            }
            if (state == 0) {
                System.out.println(k);
                return;
            }
        }
        System.out.println(-1);
    }
}


import java.io.BufferedReader;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

public class C73_div1 {

	static boolean test = false; // System.getProperty("JENS_TURBO") != null;

	public String str() throws IOException {
		if (test) {
			return reader.readLine();
		} else {
			return in.readLine();
		}
	}

	private int in() throws NumberFormatException, IOException {
		return Integer.valueOf(str());
	}

	private long lon() throws NumberFormatException, IOException {
		return Long.valueOf(str());
	}

	private int[] ints() throws Throwable {
		StringTokenizer st = new StringTokenizer(str());

		int[] res = new int[st.countTokens()];
		for (int i = 0; i < res.length; i++) {
			res[i] = Integer.parseInt(st.nextToken());
		}
		return res;
	}

	private long[] longs() throws Throwable {
		StringTokenizer st = new StringTokenizer(str());

		long[] res = new long[st.countTokens()];
		for (int i = 0; i < res.length; i++) {
			res[i] = Long.parseLong(st.nextToken());
		}
		return res;
	}

	private BigInteger[] bigs() throws Throwable {
		StringTokenizer st = new StringTokenizer(str());

		BigInteger[] res = new BigInteger[st.countTokens()];
		for (int i = 0; i < res.length; i++) {
			res[i] = new BigInteger(st.nextToken());
		}
		return res;
	}

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

	public C73_div1() throws Throwable {
		if (test) {
			reader = new BufferedReader(new FileReader(new File(testDataFile)));
		}
	}

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

	static PrintWriter writer = new PrintWriter(System.out);
	static String testDataFile = "testdata.txt";
	BufferedReader reader = null;

	int getseg(int l, int r) {
		return nimsum[r] ^ nimsum[l - 1];
	}

	int maxn = 10020;

	int[] nim = new int[maxn];
	int[] nimsum = new int[maxn];
	int[] minpile = new int[maxn];
	int[] arr = new int[maxn];

	private void solve() throws Throwable, IOException {

		int visid = 0;
		minpile[1] = -1;
		int a = 0, x = 0;
		for (int n = 2; n < maxn; n++) {
			visid++;
			minpile[n] = -1;
			for (int k = 2; k * k < 2 * n; k++) {
				if ((2 * n) % k > 0)
					continue;
				int c = 2 * n / k + 1 - k;
				if (c <= 0 || (c & 1) > 0)
					continue;

				a = c >> 1;
				x = getseg(a, a + k - 1);
				arr[x] = visid;
				if (x == 0 && minpile[n] == -1)
					minpile[n] = k;
			}
			int i = 0;
			for (i = 0; arr[i] == visid; i++);
			nim[n] = i;
			nimsum[n] = nimsum[n - 1] ^ nim[n];
		}
		System.out.println(minpile[in()]);
	}

	private double d(int row, int col, int y, int x) {
		double val = Math.pow(row - y, 2) + Math.pow(col - x, 2);
		return Math.sqrt(val);
	}
}


import java.util.Arrays;
import java.util.Scanner;
//http://codeforces.com/contest/914/problem/0
public class PerfectSquares {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		s.nextLine();
		int[] a = Arrays.stream(s.nextLine().split(" ")).mapToInt(x -> Integer.parseInt(x)).toArray();
		int max = Integer.MIN_VALUE;
		for (int i = 0; i < a.length; i++) {
			if(are(int a) {
		for (int i = 0; i*i <= a ; i++) {
			if(i*i == a) {
				return true;
			}
		}
		return false;
	}
}


import java.util.*;
public class Main
{
    public static final void main(String[] args)
    {
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        int m=s.nextInt();
        int k=s.nextInt();
        if(m>==n&&k>=n)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}


import jaav.util.*;
public class cossack
{
  public static void main(String args[])
  {
    Scanner in = new scanner(system.in);
    int n,m,k;
    n=in.nextInt();
    m=in.nextInt();
    k=in.nextInt();
    if(n>m||n>k)
     System.out.println("No");
    else
      System.out.println("Yes");
  }
}


import java.util.*:

public class Main{
    public static void main(String[] ags){
        Scanner scan = new Sanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        if(b >=a && c >= a){
            System.out.println("Yes");
        }
        else
        {
            System.out.println("No");
        }
    }
}


import java.util.Scanner;

public class Codeforce
{
    public static void main(String[] args)
    {
        Solution solution = new Solution();

        Scanner s = new Scanner(System.in);

        int n = s.nextInt();
        int k = s.nextInt();
        int l = s.nextInt();

        if(n < k && n < l)
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}


import java.util.*;
public class MaxTable{
	public static void main(String[]args){
		Scanner kbd = new Scanner(System.in);
		String word = kbd.nextLine();
		int numOfError = 0;
		String[] letters = word.split("");
		String[] rev = new String[letters.length];

		for(int i = 1; i <= rev.length; i++){
		    rev[rev.length - i] = letters[i - 1];
		}

		for(int i = 0; i < rev.length; i++){
		    if(rev[i].equals(letters[i])){
		        //do nothing
		    } else {
		        numOfError ++;
		    }
		}

		if(numOfError == 2){
		    System.out.println("YES");
		} if else (numOfErrpr != 2) {
		    System.out.println("NO");
		}

	}
}


import java.util.Scanner;
public class test{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        String s=sc.nextInt();
        int i=0,j=s.length()-1,cnt=0;
        while(i<j)
            if(s.charAt(i++)!=s.charAt(j--))
                cnt++;
        if(cnt>1)System.out.println("NO");
        else System.out.println("YES");
    }
}


//package pilot;

import java.util.*;

public class CF4102A {
	public static String reverse (String input){
		 char[] in = input.toCharArray();
		    int begin=0;
		    int end=in.length-1;
		    char temp;
		    while(end>begin){
		        temp = in[begin];
		        in[begin]=in[end];
		        in[end] = temp;
		        end--;
		        begin++;
		    }
		    return new String(in);
		}
	public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//String s = in.nextLine();
//String t = reverse(s);
System.out.println(s);
System.out.println(t);
int count =0;
for (int i = 0; i<s.length(); i++) {
	if (s.charAt(i)!=t.charAt(i)) {
		count++;
	}
}
if (s.length()%(2)!=0 && (count==2||count==0)) {System.out.println("YES");

}
else if (s.length()%2==0 && count==2) {System.out.println("YES");

}
else System.out.println("NO");
//System.out.println(count);
}
}