import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class Main{
    public static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    public static int nextInt() {
        try {
            in.nextToken();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return (int) in.nval;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int k = nextInt(), n = nextInt(), max = Integer.MIN_VALUE, a[] = new int[k];
        a[0] = nextInt();
        for (int i = 1; i < n; i++) {
            a[i] = nextInt();
            int x = a[i] - a[i - 1];
            if (max < x)
                max = x;
        }
        max = Math.max(a[0] + k - a[n - 1], max);
        System.out.println(k - max);
    }

} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.StreamTokenizer; StreamTokenizer java.io public class Main{
    public static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    public static int nextInt() {
        try {
            in.nextToken();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return (int) in.nval;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int k = nextInt(), n = nextInt(), max = Integer.MIN_VALUE, a[] = new int[k];
        a[0] = nextInt();
        for (int i = 1; i < n; i++) {
            a[i] = nextInt();
            int x = a[i] - a[i - 1];
            if (max < x)
                max = x;
        }
        max = Math.max(a[0] + k - a[n - 1], max);
        System.out.println(k - max);
    }

} Main public static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); in new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))) public static int nextInt() {
        try {
            in.nextToken();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return (int) in.nval;
    } nextInt {
        try {
            in.nextToken();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return (int) in.nval;
    } try {
            in.nextToken();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } IOException e {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } e.printStackTrace() e.printStackTrace e {
            in.nextToken();
        } in.nextToken() in.nextToken in return (int) in.nval; (int) in.nval in in.nval public static void main(String[] args) {
        // TODO Auto-generated method stub
        int k = nextInt(), n = nextInt(), max = Integer.MIN_VALUE, a[] = new int[k];
        a[0] = nextInt();
        for (int i = 1; i < n; i++) {
            a[i] = nextInt();
            int x = a[i] - a[i - 1];
            if (max < x)
                max = x;
        }
        max = Math.max(a[0] + k - a[n - 1], max);
        System.out.println(k - max);
    } main {
        // TODO Auto-generated method stub
        int k = nextInt(), n = nextInt(), max = Integer.MIN_VALUE, a[] = new int[k];
        a[0] = nextInt();
        for (int i = 1; i < n; i++) {
            a[i] = nextInt();
            int x = a[i] - a[i - 1];
            if (max < x)
                max = x;
        }
        max = Math.max(a[0] + k - a[n - 1], max);
        System.out.println(k - max);
    } int k = nextInt() k nextInt() nextInt n = nextInt() n nextInt() nextInt max = Integer.MIN_VALUE max Integer.MIN_VALUE Integer Integer.MIN_VALUE a[] = new int[k]; a new int[k] k a[0] = nextInt() a[0] a 0 nextInt() nextInt for (int i = 1; i < n; i++) {
            a[i] = nextInt();
            int x = a[i] - a[i - 1];
            if (max < x)
                max = x;
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            a[i] = nextInt();
            int x = a[i] - a[i - 1];
            if (max < x)
                max = x;
        } {
            a[i] = nextInt();
            int x = a[i] - a[i - 1];
            if (max < x)
                max = x;
        } a[i] = nextInt() a[i] a i nextInt() nextInt int x = a[i] - a[i - 1]; x a[i] - a[i - 1] a[i] a i a[i - 1] a i - 1 i 1 if (max < x)
                max = x; max < x max x max = x max x max = Math.max(a[0] + k - a[n - 1], max) max Math.max(a[0] + k - a[n - 1], max) Math.max Math a[0] + k - a[n - 1] a[0] + k a[0] a 0 k a[n - 1] a n - 1 n 1 max System.out.println(k - max) System.out.println System.out System System.out k - max k max String[] args args public class Main{
    public static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    public static int nextInt() {
        try {
            in.nextToken();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return (int) in.nval;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int k = nextInt(), n = nextInt(), max = Integer.MIN_VALUE, a[] = new int[k];
        a[0] = nextInt();
        for (int i = 1; i < n; i++) {
            a[i] = nextInt();
            int x = a[i] - a[i - 1];
            if (max < x)
                max = x;
        }
        max = Math.max(a[0] + k - a[n - 1], max);
        System.out.println(k - max);
    }

} public class Main{
    public static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    public static int nextInt() {
        try {
            in.nextToken();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return (int) in.nval;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int k = nextInt(), n = nextInt(), max = Integer.MIN_VALUE, a[] = new int[k];
        a[0] = nextInt();
        for (int i = 1; i < n; i++) {
            a[i] = nextInt();
            int x = a[i] - a[i - 1];
            if (max < x)
                max = x;
        }
        max = Math.max(a[0] + k - a[n - 1], max);
        System.out.println(k - max);
    }

} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class Main{
    public static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    public static int nextInt() {
        try {
            in.nextToken();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return (int) in.nval;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int k = nextInt(), n = nextInt(), max = Integer.MIN_VALUE, a[] = new int[k];
        a[0] = nextInt();
        for (int i = 1; i < n; i++) {
            a[i] = nextInt();
            int x = a[i] - a[i - 1];
            if (max < x)
                max = x;
        }
        max = Math.max(a[0] + k - a[n - 1], max);
        System.out.println(k - max);
    }

}
