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

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

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

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

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

public class main {

	static int maxn = 400005;
	static long H[], P[], M, N[];

	static long intervalo ( int a, int b ) {
		long res = (H[a-1] * P[b-a+1]) % M;
		long ret = ( H[b] - res);
		if ( ret < 0 )
			ret += M;
		return ret;
	}

	static long min ( long a, long b ) {
		if ( a < b ) return a;
		return b;
	}

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

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		//Scanner cin = new Scanner(System.in);
		String num = in.readLine();
		String x = in.readLine();
		M = Integer.parseInt(x);

		int l = num.length();
		H = new long[l*2+1];
		P = new long[l*2+1];
		N = new long[l*2+1];

		for ( int i = 0; i < l; i ++ )
			N[i+1] = num.charAt(i) - '0';

		for ( int i = 1; i <= l; i ++ )
			N[i+l] = N[i];

		P[0] = 1;
		for ( int i = 1; i <= l*2; i ++ )
			P[i] = ( P[i-1] * 10 ) % M;

		for ( int i = 1; i <= l*2; i ++ )
			H[i] = ( H[i-1] * 10 + N[i] ) % M;

		long sol = M-1;
		for ( int i = 1; i <= l; i ++ )
			if ( N[i] != 0 )
				sol = min ( sol, intervalo( i, i+l-1) );
		System.out.print(sol);
	}

}

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

public final class big_remainder
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static FastScanner sc=new FastScanner(br);
    static PrintWriter out=new PrintWriter(System.out);
	static Random rnd=new Random();

    public static void main(String args[]) throws Exception
    {
		String s=sc.next();s+=s;int n=s.length(),m=s.length()/2;int[] a=new int[n+1];

		for(int i=1;i<=n;i++)
		{
			a[i]=s.charAt(i-1)-'0';
		}

		long mod=sc.nextLong();long[] pow=new long[n+1];pow[0]=1;

		for(int i=1;i<=n;i++)
		{
			pow[i]=(pow[i-1]*10)%mod;
		}

		long curr=0;

		for(int i=1,j=m-1;i<=m;i++,j--)
		{
			long now=(a[i]*pow[j])%mod;curr=(curr+now)%mod;
		}

		long min=curr;//out.println(Arrays.toString(a)+" "+curr);

		for(int i=m+1;i<=n;i++)
		{
			long now=(a[i-m]*pow[m-1])%mod;

			curr=(curr-now+mod)%mod;

			curr=(curr*10)%mod;

			curr=(curr+a[i])%mod;

			//out.println((i-m+1)+" "+curr);

			if(a[i-m+1]!=0) min=Math.min(min,curr);
		}

		out.println(min);out.close();
    }
}
class FastScanner
{
    BufferedReader in;
    StringTokenizer st;

    public FastScanner(BufferedReader in) {
        this.in = in;
    }

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

	public String next() throws Exception {
		return nextToken().toString();
	}

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

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

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

import java.util.Scanner;
import java.math.*;
public class project{
    public static void main(String[]args){
	    Scanner sc = new Scanner(System.in);
	    String s=sc.next();
	    int mod=sc.nextInt();
	    Long sum=0L, ans=0L, mul=1L;
	    int len=s.length();
	    for (int i=len-1; i>=0; i--) {
	        char c= s.charAt(i);
	        int j=(int)c;
	        j-=48;
	        sum+=mul*j;
	        sum%=mod;
	        mul=(mul*10)%mod;
	        //System.out.println(sum);
	    }
	    ans=sum;
	    for (int i=0; i<len-1; i++) {
	        char c= s.charAt(i);
	        int j=(int)c;
	        j-=48;
	        sum*=10;
	        sum-=mul*j;
	        sum+=j;
	        sum%=mod;
	        sum=(sum+mod)%mod;
	        if (s.charAt(i+1)!='0')ans=Math.min(ans, sum);
	    }
	    System.out.println(ans);
    }
}

import java.util.*;

public class Sol{
	public static void main(String[] argc){
		Scanner sc=new Scanner(System.in);
		String ss=sc.next();
		int MOD=sc.nextInt();
		char[] s=ss.toCharArray();
		int len=s.length;
		int cur=0;
		for(int i=0;i<len;++i) cur=(int)(((long)cur*10+(s[i]-'0'))%MOD);
		int ret=cur;
		int p=1; for(int i=0;i<len;++i) p=(int)((long)p*10%MOD);
		for(int i=0;i<len-1;++i) {
		    cur=(int)(((long)10*cur+(long)(1+MOD-p)*(s[i]-'0'))%MOD);
		    if(s[i+1]!='0'&&cur<ret) ret=cur;
		}
		System.out.println(ret);
	}
}

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

public class E {

	FastScanner in;
	PrintWriter out;

	void solve() {
		String x = in.nextToken(), y = x;
		int m = in.nextInt();
		long mx = 1;
		long cur = 0;
		int chunk = 8;
		long[] tens = new long[chunk + 1];
		tens[0] = 1;
		for (int i = 1; i <= chunk; i++)
			tens[i] = (tens[i - 1] * 10) % m;
		for (int i = 0; i < x.length(); i += chunk) {
			int end = Math.min(i + chunk, x.length());
			long xi = Long.parseLong(x.substring(i, end));
			cur = (cur * tens[end - i] + xi) % m;
			if (end == x.length())
				mx = mx * tens[end - i - 1] % m;
			else
				mx = mx * tens[end - i] % m;
		}
		long[] pre = new long[10];
		pre[0] = 0;
		for (int i = 1; i < 10; i++)
			pre[i] = (pre[i - 1] + mx) % m;
		long minx = cur;
		for (int i = 0; i < x.length() - 1; i++) {
			int c = x.charAt(i) - '0';
			cur = (((cur - pre[c]) % m * 10) % m + c + m) % m;
			if (x.charAt(i + 1) != '0' && cur < minx)
				minx = cur;
		}
		out.println(minx);
	}

	void run() {
		in = new FastScanner();
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

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

		public FastScanner(String s) {
			try {
				br = new BufferedReader(new FileReader(s));
			} catch (FileNotFoundException e) {
			}
		}

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

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

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

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

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

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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */

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

class Task {
  public void solve(int testNumber, InputReader in, PrintWriter out) {
    String s = in.next();
    int m = in.nextInt(), n = s.length();
    long cur = 0, mul = 1;
    for (int i = 0; i < n; i++) {
      cur = (cur * 10 + s.charAt(i)-'0') % m;
      mul = (mul * 10) % m;
    }
    long ans = cur;
    for (int i = 0; i < n; i++) {
      if (s.charAt(i) != '0') {
        if (ans > cur) ans = cur;
      }
      cur = (cur * 10 + s.charAt(i)-'0') % m;
      cur = (cur - mul * (s.charAt(i)-'0')) % m;
      if (cur < 0) cur += m;
    }
    out.println(ans);
  }
}

class InputReader {
  public BufferedReader reader;
  public StringTokenizer tokenizer;

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

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

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

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

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

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        ConsoleIO io = new ConsoleIO(new InputStreamReader(System.in), new PrintWriter(System.out));
//        String test = "kittens";
//        ConsoleIO io = new ConsoleIO(new FileReader("D:\\Comp\\GHC\\" + test + ".in"), new PrintWriter(new File("D:\\Comp\\GHC\\" + test + ".optimized")));

        new Main(io).solve();
        io.close();
    }

    ConsoleIO io;

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

    long MOD = 1_000_000_007;
    int INF = Integer.MAX_VALUE / 2;

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

    public void solve() {
        char[] c = io.readLine().toCharArray();
        long m = Long.parseLong(io.readLine());
        long r1 = 0;
        long r2 = 1;
        for(int i = 0;i<c.length;i++) {
            r1 = ((r1 * 10) + c[i]-'0') % m;
            r2 = (r2*10)%m;
        }
        long res = r1;
        for(int i = 1;i<c.length;i++) {
            r1 = ((r1 * 10) + c[i - 1] - '0') % m;
            r1 = (r1 - (r2 * (c[i - 1] - '0')) % m + m) % m;
            if (c[i] != '0') {
                res = Math.min(res, r1);
            }
        }

        io.writeLine(res+"");
    }
}

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.StringTokenizer;
import java.util.stream.Stream;

public class Main implements Runnable {
	static final double time = 1e9;
	static final int MOD = (int) 1e9 + 7;
	static final long mh = Long.MAX_VALUE;
	static final Reader in = new Reader();
	static final PrintWriter out = new PrintWriter(System.out);
	StringBuilder answer = new StringBuilder();
	long start = System.nanoTime();

	public static void main(String[] args) {
		new Thread(null, new Main(), "persefone", 1 << 28).start();
	}

	@Override
	public void run() {

		solve();
		printf();
		long elapsed = System.nanoTime() - start;
//		out.println("stamp : " + elapsed / time);
//		out.println("stamp : " + TimeUnit.SECONDS.convert(elapsed, TimeUnit.NANOSECONDS));
		close();
//		out.flush();
	}

	void solve() {
		String s = in.next();
		int n = s.length();
		s += s;

		int[] f = new int[n << 1];
		for (int i = 0; i < f.length; i++)
			f[i] = s.charAt(i) - 48;

		int m = in.nextInt();

		int p = 1;
		for (int i = 0; i < n - 1; i++) {
			p = p * 10 % m;
		}

		int x = 0;
		for (int i = 0; i < n; i++) {
			x = (x * 10 + f[i]) % m;
		}

		int ans = x;
		for (int i = 0; i < n; i++) {
			x = (x - f[i] * p % m + m) % m;
			x = (x * 10 + f[i + n]) % m;
			if (f[i + 1] != 0 && x < ans) {
				ans = x;
			}
		}
		printf(ans);
	}

	void printf() {
		out.print(answer);
	}

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

	void printf(Stream<?> str) {
		str.forEach(o -> add(o, " "));
		add("\n");
	}

	void printf(Object... obj) {
		printf(false, obj);
	}

	void printfWithDescription(Object... obj) {
		printf(true, obj);
	}

	private void printf(boolean b, Object... obj) {

		if (obj.length > 1) {
			for (int i = 0; i < obj.length; i++) {
				if (b) add(obj[i].getClass().getSimpleName(), " - ");
				if (obj[i] instanceof Collection<?>) {
					printf((Collection<?>) obj[i]);
				} else if (obj[i] instanceof int[][]) {
					printf((int[][])obj[i]);
				} else if (obj[i] instanceof long[][]) {
					printf((long[][])obj[i]);
				} else if (obj[i] instanceof double[][]) {
					printf((double[][])obj[i]);
				} else printf(obj[i]);
			}
			return;
		}

		if (b) add(obj[0].getClass().getSimpleName(), " - ");
		printf(obj[0]);
	}

	void printf(Object o) {
		if (o instanceof int[])
			printf(Arrays.stream((int[]) o).boxed());
		else if (o instanceof char[])
			printf(new String((char[]) o));
		else if (o instanceof long[])
			printf(Arrays.stream((long[]) o).boxed());
		else if (o instanceof double[])
			printf(Arrays.stream((double[]) o).boxed());
		else if (o instanceof boolean[]) {
			for (boolean b : (boolean[]) o) add(b, " ");
			add("\n");
		}
		else
			add(o, "\n");
	}

	void printf(int[]... obj) {
		for (int i = 0; i < obj.length; i++) printf(obj[i]);
	}

	void printf(long[]... obj) {
		for (int i = 0; i < obj.length; i++) printf(obj[i]);
	}

	void printf(double[]... obj) {
		for (int i = 0; i < obj.length; i++) printf(obj[i]);
	}

	void printf(boolean[]... obj) {
		for (int i = 0; i < obj.length; i++) printf(obj[i]);
	}

	void printf(Collection<?> col) {
		printf(col.stream());
	}

	<T, K> void add(T t, K k) {
		if (t instanceof Collection<?>) {
			((Collection<?>) t).forEach(i -> add(i, " "));
		} else if (t instanceof Object[]) {
			Arrays.stream((Object[]) t).forEach(i -> add(i, " "));
		} else
			add(t);
		add(k);
	}

	<T> void add(T t) {
		answer.append(t);
	}

	@SuppressWarnings("unchecked")
	<T extends Comparable<? super T>> T min(T... t) {
		if (t.length == 0)
			return null;
		T m = t[0];
		for (int i = 1; i < t.length; i++)
			if (t[i].compareTo(m) < 0)
				m = t[i];
		return m;
	}

	@SuppressWarnings("unchecked")
	<T extends Comparable<? super T>> T max(T... t) {
		if (t.length == 0)
			return null;
		T m = t[0];
		for (int i = 1; i < t.length; i++)
			if (t[i].compareTo(m) > 0)
				m = t[i];
		return m;
	}

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

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

	// c = gcd(a, b) -> extends gcd method: ax + by = c <----> (b % a)p + q = c;
	int[] ext_gcd(int a, int b) {
		if (b == 0) return new int[] {a, 1, 0 };
		int[] vals = ext_gcd(b, a % b);
		int d = vals[0]; // gcd
	    int p = vals[2]; //
		int q = vals[1] - (a / b) * vals[2];
		return new int[] { d, p, q };
	}

	// find any solution of the equation: ax + by = c using extends gcd
	boolean find_any_solution(int a, int b, int c, int[] root) {
		int[] vals = ext_gcd(Math.abs(a), Math.abs(b));
		if (c % vals[0] != 0) return false;
		printf(vals);
		root[0] = c * vals[1] / vals[0];
		root[1] = c * vals[2] / vals[0];
		if (a < 0) root[0] *= -1;
		if (b < 0) root[1] *= -1;
		return true;
	}

	int mod(int x) { return x % MOD; }

	int mod(int x, int y) { return mod(mod(x) + mod(y)); }

	long mod(long x) { return x % MOD; }

	long mod (long x, long y) { return mod(mod(x) + mod(y)); }

	int lw(long[] f, int l, int r, long k) {
		int R = r, m = 0;
		while (l <= r) {
			m = l + r >> 1;
		    if (m == R) return m;
		    if (f[m] >= k) r = m - 1; else l = m + 1;
		}
		return l;
	}

	int up(long[] f, int l, int r, long k) {
		int R = r, m = 0;
		while (l <= r) {
			m = l + r >> 1;
		    if (m == R) return m;
		    if (f[m] > k) r = m - 1; else l = m + 1;
		}
		return l;
	}

	int lw(int[] f, int l, int r, int k) {
		int R = r, m = 0;
		while (l <= r) {
			m = l + r >> 1;
		    if (m == R) return m;
		    if (f[m] >= k) r = m - 1; else l = m + 1;
		}
		return l;
	}

	int up(int[] f, int l, int r, int k) {
		int R = r, m = 0;
		while (l <= r) {
			m = l + r >> 1;
		    if (m == R) return m;
		    if (f[m] > k) r = m - 1; else l = m + 1;
		}
		return l;
	}

	<K extends Comparable<K>> int lw(List<K> li, int l, int r, K k) {
		int R = r, m = 0;
		while (l <= r) {
			m = l + r >> 1;
		    if (m == R) return m;
		    if (li.get(m).compareTo(k) >= 0)
		    	r = m - 1;
		    else
		    	l = m + 1;
		}
		return l;
	}

	<K extends Comparable<K>> int up(List<K> li, int l, int r, K k) {
		int R = r, m = 0;
		while (l <= r) {
			m = l + r >> 1;
		    if (m == R) return m;
		    if (li.get(m).compareTo(k) > 0)
		    	r = m - 1;
		    else
		    	l = m + 1;
		}
		return l;
	}

	<K extends Comparable<K>> int bs(List<K> li, int l, int r, K k) {
		while (l <= r) {
			int m = l + r >> 1;
		    if (li.get(m) == k) return m;
		    else if (li.get(m).compareTo(k) < 0)
		    	l = m + 1;
		    else
		    	r = m - 1;
		}
		return -1;
	}

	long calc(int base, int exponent) {
		if (exponent == 0) return 1;
		if (exponent == 1) return base % MOD;
		long m = calc(base, exponent / 2);
		if (exponent % 2 == 0) return (m * m) % MOD;
		return (base * ((m * m) % MOD)) % MOD;
	}

	long calc(int base, long exponent) {
		if (exponent == 0) return 1;
		if (exponent == 1) return base % MOD;
		long m = calc(base, exponent / 2);
		if (exponent % 2 == 0) return (m * m) % MOD;
		return (base * ((m * m) % MOD)) % MOD;
	}

	long calc(long base, long exponent) {
		if (exponent == 0) return 1;
		if (exponent == 1) return base % MOD;
		long m = calc(base, exponent / 2);
		if (exponent % 2 == 0) return (m * m) % MOD;
		return (base * (m * m % MOD)) % MOD;
	}

	long power(int base, int exponent) {
		if (exponent == 0) return 1;
		long m = power(base, exponent / 2);
		if (exponent % 2 == 0) return m * m;
		return base * m * m;
	}

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

	void swap(long[] a, int i, int j) {
		long tmp = a[i];
		a[i] = a[j];
		a[j] = tmp;
	}

	static class Pair<K extends Comparable<? super K>, V extends Comparable<? super V>>
			implements Comparable<Pair<K, V>> {
		private K k;
		private V v;

		Pair() {}

		Pair(K k, V v) {
			this.k = k;
			this.v = v;
		}

		K getK() { return k; }

		V getV() { return v; }

		void setK(K k) { this.k = k; }

		void setV(V v) { this.v = v; }

		void setKV(K k, V v) {
			this.k = k;
			this.v = v;
		}

		@SuppressWarnings("unchecked")
		@Override
		public boolean equals(Object o) {
			if (this == o) return true;
			if (o == null || !(o instanceof Pair)) return false;
			Pair<K, V> p = (Pair<K, V>) o;
			return k.compareTo(p.k) == 0 && v.compareTo(p.v) == 0;
		}

		@Override
		public int hashCode() {
			int hash = 31;
			hash = hash * 89 + k.hashCode();
			hash = hash * 89 + v.hashCode();
			return hash;
		}

		@Override
		public int compareTo(Pair<K, V> pair) {
			return k.compareTo(pair.k) == 0 ? v.compareTo(pair.v) : k.compareTo(pair.k);
		}

		@Override
		public Pair<K, V> clone() {
			return new Pair<K, V>(this.k, this.v);
		}

		@Override
		public String toString() {
			return String.valueOf(k).concat(" ").concat(String.valueOf(v)).concat("\n");
		}
	}

	static class Reader {
		private BufferedReader br;
		private StringTokenizer st;

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

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

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

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

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

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

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

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

import static java.lang.StrictMath.abs;

public class code {
    public static long n(char c){
        return c-'0';
    }

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        String S = in.next();
        long mod = in.nextInt(),val=0;
        long rs = mod;
        long P[] = new long[200100];
        P[0] = 1;
        for(int i = 1;i<=200010;i++) P[i] = (P[i-1]*10)%mod;
        char C[] = S.toCharArray();
        for(int i = 0;i<C.length;i++) val=(val+n(C[i])*P[C.length-i-1])%mod;

        for(int i = 0;i<C.length;i++){
            if(C[i] != '0') rs=min(rs,val);
            val-=n(C[i])*P[C.length-1];
            val*=10;
            val+=n(C[i]);
            if(val<0) val = (val + (abs(val)/mod+1)*mod);
            val%=mod;
        }

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

    private static long min(long a, long b) {
        return (a < b ? a : b);
    }

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

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

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

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

    }
}

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

public class Main {

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

		int divider  = sc.nextInt();
		int len = s.length();
		int cur = 0;
		int q = 0;
		int mnoj[] = new int[10];
		mnoj[1] = 1;
		for (int i = 0; i < len; i++) {
		    q = (s.charAt(i) - '0');
		    cur = (cur * 10 + q) % divider;
		    mnoj[1] = mnoj[1] * 10 % divider;
		}

		for (int i = 2; i < 10; i++)
		{
			mnoj[i] = mnoj[1] * i % divider;
		}

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

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

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

public class step_by_step {
    public static PrintStream out = System.out;
    public static Scanner in = new Scanner(System.in);

    public static long bin_pow(long a, long p, long m)
    {
        long res = 1;
        while (p > 0)
        {
            if ((p & 1) > 0)
                res = res * a % m;
            a = a * a % m;
            p >>= 1;
        }
        return res;
    }

    public static void main(String[] args) {
        String n = in.nextLine();
        long m = in.nextInt();
        int l = n.length();
        long ans = m, cnt = 0;
        for (int i = 0; i < l; i++)
            cnt = (cnt + bin_pow(10, l - i - 1, m) * (n.charAt(i) - '0') % m) % m;
        for (int i = 0; i < l; i++)
        {
            if (n.charAt(i) != '0')
                ans = Math.min(ans, cnt);
            cnt = (cnt - bin_pow(10, l - 1, m) * (n.charAt(i) - '0') % m + m) % m;
            cnt = (cnt * 10 + (n.charAt(i) - '0')) % m;
        }
        out.println(ans);
    }
}

import java.util.Scanner;
import java.math.*;
public class project{
    public static void main(String[]args){
	    Scanner sc = new Scanner(System.in);
	    String s=sc.next();
	    int mod=sc.nextInt();
	    int sum=0, ans=0, mul=1;
	    int len=s.length();
	    for (int i=len-1; i>=0; i--) {
	        char c= s.charAt(i);
	        int j=(int)c;
	        j-=48;
	        sum+=mul*j;
	        sum%=mod;
	        mul=(mul*10)%mod;
	        //System.out.println(sum);
	    }
	    ans=sum;
	    for (int i=0; i<len; i++) {
	        char c= s.charAt(i);
	        int j=(int)c;
	        j-=48;
	        sum*=10;
	        sum-=mul*j;
	        sum+=j+mod;
	        sum%=mod;
	        ans=Math.min(ans, sum);
	    }
	    System.out.println(ans);
    }
}

import java.util.Scanner;
import java.math.*;
public class project{
    public static void main(String[]args){
	    Scanner sc = new Scanner(System.in);
	    String s=sc.next();
	    int mod=sc.nextInt();
	    int sum=0, ans=0, mul=1;
	    int len=s.length();
	    for (int i=len-1; i>=0; i--) {
	        char c= s.charAt(i);
	        int j=(int)c;
	        j-=48;
	        sum+=mul*j;
	        sum%=mod;
	        mul=(mul*10)%mod;
	        //System.out.println(sum);
	    }
	    ans=sum;
	    for (int i=0; i<len; i++) {
	        char c= s.charAt(i);
	        int j=(int)c;
	        j-=48;
	        sum*=10;
	        sum-=mul*j;
	        sum+=j;
	        sum%=mod;
	        sum=(sum+mod)%mod;
	        ans=Math.min(ans, sum);
	    }
	    System.out.println(ans);
    }
}

import java.util.Scanner;
import java.math.*;
public class project{
    public static void main(String[]args){
	    Scanner sc = new Scanner(System.in);
	    String s=sc.next();
	    int mod=sc.nextInt();
	    Long sum=0L, ans=0L, mul=1L;
	    int len=s.length();
	    for (int i=len-1; i>=0; i--) {
	        char c= s.charAt(i);
	        int j=(int)c;
	        j-=48;
	        sum+=mul*j;
	        sum%=mod;
	        mul=(mul*10)%mod;
	        //System.out.println(sum);
	    }
	    ans=sum;
	    for (int i=0; i<len; i++) {
	        char c= s.charAt(i);
	        int j=(int)c;
	        j-=48;
	        sum*=10;
	        sum-=mul*j;
	        sum+=j;
	        sum%=mod;
	        sum=(sum+mod)%mod;
	        ans=Math.min(ans, sum);
	    }
	    System.out.println(ans);
    }
}

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

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

public class Main {

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

		int len = s.length();

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

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