/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

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

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class Main {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        boolean[] numberics = new boolean[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = true;
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i <= max; i++) {
            if (numberics[i]) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            return;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int loop = 2, limit = 0;
            while (true) {
                if ((limit = num * loop) > max) {
                    // 可优化
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                }
                int offset = 1;
                while (num - offset > maxMod) {
                    if (numberics[limit - offset]) {
                        maxMod = num - offset;
                        break;
                    }
                    offset++;
                }
                loop++;
            }
        }
        out.println(maxMod);

    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

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

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

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

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

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


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

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

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class CopyOfMain {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        SmartNumberic[] numberics = new SmartNumberic[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = new SmartNumberic(true);
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i <= max; i++) {
            numberics[i] = numberics[i] != null ? numberics[i] : new SmartNumberic();
            if (numberics[i].exist) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        int pre = 0;
        int next = sortNumberics.get(0);
        int sortOffset = 1;
        for (int i = 1; i < max; i++) {
            if (i == next) {
                sortOffset++;
            } else if (i > next) {
                pre = sortNumberics.get(sortOffset - 1);
            }
            numberics[i].pre = pre;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int limit = num;
            while (true) {
                if ((limit += num) >= max) {
                    // 可优化
                    maxMod = maxMod > (num + max - limit) ? maxMod : (num + max - limit);
                    break;
                } else if (numberics[limit].pre == num) {
                    continue;
                } else {
                    maxMod = maxMod > numberics[limit].pre % num ? maxMod : numberics[limit].pre % num;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static class SmartNumberic {
        boolean exist;
        Integer pre;

        public SmartNumberic() {};

        public SmartNumberic(boolean exist){
            this.exist = exist;
        }
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

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

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

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

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

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


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

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

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class Main {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        boolean[] numberics = new boolean[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = true;
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i <= max; i++) {
            if (numberics[i]) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int start = num, limit = 0;
            while (true) {
                if ((limit = start + num) > max) {
                    // 可优化
                    maxMod = maxMod > (num + max - limit) ? maxMod : (num + max - limit);
                    break;
                }
                int offset = 1;
                while (num - offset > maxMod) {
                    if (numberics[limit - offset]) {
                        maxMod = num - offset;
                        break;
                    }
                    offset++;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

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

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

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

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

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


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean[] numberics = new boolean[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = scan.nextInt();
        for (int i = 0; i < size; i++) {
            int num = scan.nextInt();
            max = max > num ? max : num;
            numberics[num] = true;
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size || i < max; i++) {
            if (numberics[i]) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            System.out.println(0);
            scan.close();
            return;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int loop = 2, limit = 0;
            while (true) {
                if ((limit = num * loop) > max) {
                    // 可优化
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                }
                int offset = 1;
                while (num - offset > maxMod) {
                    if (numberics[limit - offset]) {
                        maxMod = num - offset;
                    }
                    offset++;
                }
                loop++;
            }
        }
        System.out.println(maxMod);

        scan.close();
    }

}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

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

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class CopyOfMain {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        SmartNumberic[] numberics = new SmartNumberic[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = new SmartNumberic(true);
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i <= max; i++) {
            numberics[i] = numberics[i] != null ? numberics[i] : new SmartNumberic();
            if (numberics[i].exist) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        int pre = 0;
        int next = sortNumberics.get(0);
        int preOffset = 0;
        for (int i = 1; i <= max; i++) {
            if (i == next) {
                preOffset++;
            } else if (i > next && preOffset < sortNumberics.size()) {
                next = sortNumberics.get(preOffset);
                pre = sortNumberics.get(preOffset - 1);
            }
            numberics[i].pre = pre;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int limit = num;
            while (true) {
                if ((limit += num) > max) {
                    // 可优化
                    // maxMod = maxMod > (num + max - limit) ? maxMod : (num +
                    // max - limit);
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                } else if (numberics[limit].pre == num) {
                    continue;
                } else {
                    maxMod = maxMod > numberics[limit].pre % num ? maxMod : numberics[limit].pre % num;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static class SmartNumberic {
        boolean exist;
        Integer pre;

        public SmartNumberic() {};

        public SmartNumberic(boolean exist){
            this.exist = exist;
        }
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

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

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

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

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

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


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

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

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class Main {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        boolean[] numberics = new boolean[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = true;
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i <= max; i++) {
            if (numberics[i]) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int start = 2 * num, limit = 0;
            while (true) {
                if ((limit = start + num) > max) {
                    // 可优化
                    maxMod = maxMod > (num + max - limit) ? maxMod : (num + max - limit);
                    break;
                }
                int offset = 1;
                while (num - offset > maxMod) {
                    if (numberics[limit - offset]) {
                        maxMod = num - offset;
                        break;
                    }
                    offset++;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

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

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

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

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

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


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

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

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class Main {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        boolean[] numberics = new boolean[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = true;
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i <= max; i++) {
            if (numberics[i]) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int limit = num;
            while (true) {
                if ((limit += num) > max) {
                    // 可优化
                    maxMod = maxMod > (num + max - limit) ? maxMod : (num + max - limit);
                    break;
                }
                int offset = 1;
                while (num - offset > maxMod) {
                    if (numberics[limit - offset]) {
                        maxMod = num - offset;
                        break;
                    }
                    offset++;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

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

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

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

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

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


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean[] numberics = new boolean[1000000];
        int max = 0;
        // int maxMod = 0;
        int size = scan.nextInt();
        for (int i = 0; i < size; i++) {
            int num = scan.nextInt();
            max = max > num ? max : num;
            numberics[num] = true;
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size || i < max; i++) {
            if (numberics[i]) {
                sortNumberics.add(i);
            }
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int loop = 2, limit = 0;
            while (true) {
                if ((limit = num * loop) > max) {
                    // 可优化
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                }
                int offset = 1;
                while (num - offset > maxMod) {
                    if (numberics[limit - offset]) {
                        maxMod = num - offset;
                    }
                    offset++;
                }
                loop++;
            }
        }
        System.out.println(maxMod);

        scan.close();
    }

}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

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

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class CopyOfMain {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        SmartNumberic[] numberics = new SmartNumberic[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = new SmartNumberic(true);
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i <= max; i++) {
            numberics[i] = numberics[i] != null ? numberics[i] : new SmartNumberic();
            if (numberics[i].exist) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        int pre = 0;
        int next = sortNumberics.get(0);
        int sortOffset = 1;
        for (int i = 1; i < max; i++) {
            if (i == next) {
                sortOffset++;
            } else if (i > next) {
                pre = sortNumberics.get(sortOffset - 1);
            }
            numberics[i].pre = pre;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int limit = num;
            while (true) {
                if ((limit += num) > max) {
                    // 可优化
                    maxMod = maxMod > (num + max - limit) ? maxMod : (num + max - limit);
                    break;
                } else if (numberics[limit].pre == num) {
                    continue;
                } else {
                    maxMod = maxMod > numberics[limit].pre % num ? maxMod : numberics[limit].pre % num;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static class SmartNumberic {
        boolean exist;
        Integer pre;

        public SmartNumberic() {};

        public SmartNumberic(boolean exist){
            this.exist = exist;
        }
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

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

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

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

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

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


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

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

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class CopyOfMain {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        SmartNumberic[] numberics = new SmartNumberic[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = new SmartNumberic(true);
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i <= max; i++) {
            numberics[i] = numberics[i] != null ? numberics[i] : new SmartNumberic();
            if (numberics[i].exist) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        int pre = 0;
        int next = sortNumberics.get(0);
        int sortOffset = 1;
        for (int i = 1; i <= max; i++) {
            if (i == next) {
                sortOffset++;
            } else if (i > next) {
                pre = sortNumberics.get(sortOffset - 1);
            }
            numberics[i].pre = pre;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int limit = num;
            while (true) {
                if ((limit += num) > max) {
                    // 可优化
                    // maxMod = maxMod > (num + max - limit) ? maxMod : (num +
                    // max - limit);
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                } else if (numberics[limit].pre == num) {
                    continue;
                } else {
                    maxMod = maxMod > numberics[limit].pre % num ? maxMod : numberics[limit].pre % num;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static class SmartNumberic {
        boolean exist;
        Integer pre;

        public SmartNumberic() {};

        public SmartNumberic(boolean exist){
            this.exist = exist;
        }
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

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

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

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

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

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


import java.util.Scanner;

public class MaximumValue1 {

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

		Scanner input = new Scanner(System.in);
		int max = 2000050;
		int n = input.nextInt();
		int[] arr = new int[max];
		for(int i=0; i<n;i++){
			int x = input.nextInt();
			arr[x] = x;
		}
		for(int i=1; i<max;i++){
			if(arr[i]!=i){
				arr[i] = arr[i-1];
			}
		}
		int ans=0;
		for(int i=1; i<max;i++){
			if(arr[i]==i){
				for(int j=i+i-1;j<max;j+=i){
					if(ans<arr[j]%i && arr[j]>i)ans = arr[j]%i;
				}
			}
		}
		System.out.println(ans);

		//for()

	}

}


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

public class MaximumValue {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// TODO Auto-generated method stub

		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		int[] arr = new int[n];
		for(int i=0; i<n;i++)arr[i] = input.nextInt();
		Arrays.sort(arr);
		int rem = 0;
		for(int i=0; i<n;i++){
			for(int j=i+1; j<n;j++){
				if(arr[j]%arr[i] >rem)rem = arr[j]%arr[i];
			}
		}
		System.out.println(rem);
	}

}


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

public class Task {
    final int N = 1_000_000 + 10;

    public void run() throws IOException {
        FastScanner in = new FastScanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int n = in.nextInt();
        boolean[] was = new boolean[N];
        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            was[a] = true;
        }
        int res = 0;
        int[] last = new int[N];
        int prev = 0;
        for (int i = 1; i < N; i++) {
            if (was[i])
                prev = i;
            last[i] = prev;
        }
        for (int i = 2; i < N; i++)
            if (was[i])
                for (int j = 1; i * j < N; j++) {
                    int border = Math.min(N - 1, i * j + i - 1);
                    int biggest = last[border];
                    res = Math.max(res, biggest % i);
                }
        out.println(res);
        out.flush();
    }

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

    class FastScanner {
        private StringTokenizer tokenizer;
        private BufferedReader reader;

        public FastScanner(InputStream inputStream) {
            reader = new BufferedReader(new InputStreamReader(inputStream));
        }

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

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

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

        public void close() {
            try {
                reader.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

public class D276
{

    public static void main(String args[])
    {
        FasterScanner s=new FasterScanner();
        int h[]=new int[2*1000010];

        boolean b[]=new boolean[2*1000010];
        int n=s.nextInt();
        //ArrayList<Integer> z=new ArrayList<Integer>();
        //int a[]=new int[n];
        for(int i=0;i<n;i++)
            {
            int tmp=s.nextInt();
            b[tmp]=true;
            h[tmp]=1;
            }

        for(int i=1;i<2*1000010;i++)
        {
            if(h[i]==0)
                h[i]=h[i-1];
            else
                h[i]=i;
        }
        int max=0;
        for(int i=1;i<1000001;i++)
        {
            if(b[i])
            for(int j=2*i;j<2*1000010;j+=i)
            {
                    max=Math.max(h[j-1]%i,max);
            }
        }

        System.out.println(max);
    }

    public static class FasterScanner {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

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

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

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

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

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

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

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

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

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


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

public class D276
{

    public static void main(String args[])
    {
        FasterScanner s=new FasterScanner();
        int h[]=new int[2*1000010];
        int n=s.nextInt();
        ArrayList<Integer> z=new ArrayList<Integer>();
        int a[]=new int[n];
        for(int i=0;i<n;i++)
            {
            a[i]=s.nextInt();
            h[a[i]]=1;
            }
        if(h[0]==1)
            h[0]=0;

        for(int i=1;i<2*1000010;i++)
        {
            if(h[i]==0)
                h[i]=h[i-1];
            else
                h[i]=i;
        }
        int max=0;
        for(int i=0;i<n;i++)
        {
            for(int j=2*a[i];j<1000010;j+=a[i])
            {
                if((h[j])%a[i]>max)
                    max=h[j]%a[i];
            }
        }

        System.out.println(max);
    }

    public static class FasterScanner {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

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

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

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

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

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

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

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

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

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


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

public class D276
{

    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        int h[]=new int[1000010];
        int n=s.nextInt();
        ArrayList<Integer> z=new ArrayList<Integer>();
        int a[]=new int[n];
        for(int i=0;i<n;i++)
            {
            a[i]=s.nextInt();
            if(h[a[i]]==0)
                z.add(a[i]);

            h[a[i]]=1;
            }

        Object[] b=z.toArray();

        Arrays.sort(b);
        //System.out.println(Arrays.toString(b));
        int max=0;
        for(int i=z.size()-2;i>=0 && (int)b[i]>max;i--)
        {
            for(int j=i+1;j<z.size();j++)
            {
                //System.out.println(a[i]+" "+a[j]+" "+max);
                if((int)b[j]%(int)b[i]>max)
                    max=(int)b[j]%(int)b[i];
            }
        }

        System.out.println(max);
    }
}


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

public class D276
{

    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        int h[]=new int[2*1000010];
        int n=s.nextInt();
        ArrayList<Integer> z=new ArrayList<Integer>();
        int a[]=new int[n];
        for(int i=0;i<n;i++)
            {
            a[i]=s.nextInt();
            h[a[i]]=1;
            }
        if(h[0]==1)
            h[0]=0;

        for(int i=1;i<2*1000010;i++)
        {
            if(h[i]==0)
                h[i]=h[i-1];
            else
                h[i]=i;
        }
        int max=0;
        for(int i=0;i<n;i++)
        {
            for(int j=2*a[i];j<2*1000010;j+=a[i])
            {
                if((h[j])%a[i]>max)
                    max=h[j]%a[i];
            }
        }

        System.out.println(max);
    }
}


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

public class D276
{

    public static void main(String args[])
    {
        FasterScanner s=new FasterScanner();
        int h[]=new int[2*1000010];
        int n=s.nextInt();
        ArrayList<Integer> z=new ArrayList<Integer>();
        int a[]=new int[n];
        for(int i=0;i<n;i++)
            {
            a[i]=s.nextInt();
            h[a[i]]=1;
            }
        if(h[0]==1)
            h[0]=0;

        for(int i=1;i<2*1000010;i++)
        {
            if(h[i]==0)
                h[i]=h[i-1];
            else
                h[i]=i;
        }
        int max=0;
        for(int i=0;i<n;i++)
        {
            for(int j=2*a[i];j<2*1000010;j+=a[i])
            {
                if(h[j]>a[i])
                    max=Math.max(h[j]%a[i],max);
            }
        }

        System.out.println(max);
    }

    public static class FasterScanner {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

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

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

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

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

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

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

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

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

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


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

public class D276
{

    public static void main(String args[])
    {
        FasterScanner s=new FasterScanner();
        int h[]=new int[2*1000010];
        int n=s.nextInt();
        ArrayList<Integer> z=new ArrayList<Integer>();
        int a[]=new int[n];
        for(int i=0;i<n;i++)
            {
            a[i]=s.nextInt();
            h[a[i]]=1;
            }
        if(h[0]==1)
            h[0]=0;

        for(int i=1;i<2*1000010;i++)
        {
            if(h[i]==0)
                h[i]=h[i-1];
            else
                h[i]=i;
        }
        int max=0;
        for(int i=0;i<n;i++)
        {
            for(int j=2*a[i];j<2*1000010;j+=a[i])
            {
                if((h[j])%a[i]>max)
                    max=h[j]%a[i];
            }
        }

        System.out.println(max);
    }

    public static class FasterScanner {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

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

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

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

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

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

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

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

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

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


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

public class D276
{

    public static void main(String args[])
    {
        FasterScanner s=new FasterScanner();
        int h[]=new int[2*1000010];

        boolean b[]=new boolean[2*1000010];
        int n=s.nextInt();
        //ArrayList<Integer> z=new ArrayList<Integer>();
        //int a[]=new int[n];
        for(int i=0;i<n;i++)
            {
            int tmp=s.nextInt();
            b[tmp]=true;
            h[tmp]=1;
            }

        for(int i=1;i<2*1000010;i++)
        {
            if(h[i]==0)
                h[i]=h[i-1];
            else
                h[i]=i;
        }
        int max=0;
        for(int i=1;i<1000001;i++)
        {
            if(b[i])
            for(int j=2*i;j<2*1000010;j+=i)
            {
                    max=Math.max(h[j]%i,max);
            }
        }

        System.out.println(max);
    }

    public static class FasterScanner {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

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

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

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

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

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

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

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

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

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


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

public class D276
{

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

        Arrays.sort(a);

        int max=0;
        for(int i=n-2;i>=0 && a[i]>max;i--)
        {
            for(int j=i+1;j<n;j++)
            {
                //System.out.println(a[i]+" "+a[j]+" "+max);
                if(a[j]%a[i]>max)
                    max=a[j]%a[i];
            }
        }

        System.out.println(max);
    }
}


import java.util.*;
import java.math.*;
import java.io.*;
 import java.text.DecimalFormat;
public class Main{
    static int[] arr;
    static String h;
    static int mod=1000000007 ;
    public static void main(String[] args)  throws IOException {
        s.init(System.in);
        int n=s.nextInt();
        int[] a=new int[n];
        boolean[] is=new boolean[200001];
        int m=200001;
        for(int i=0;i<n;i++){
            a[i]=s.nextInt();
            is[a[i]]=true;
        }
        int[] last=new int[m];
        int p=0;
        for(int i=1;i<n;i++){
            if(is[i])
                p=i;
            last[i]=p;
        }
        int ans=0;
        for(int i=2;i<m;i++){
            if(!is[i])
                continue;
            for(int j=1;j*i<m;j++){
                int z=Math.min(m-1,i+i*j-1);
                int x=last[z];
                ans=Math.max(ans,x%i);
            }
        }
        System.out.println(ans);
    }

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

        /** call this method to initialize reader for InputStream */
        static void init(InputStream input) {
            reader = new BufferedReader(
                        new InputStreamReader(input) );
            tokenizer = new StringTokenizer("");
        }

        /** get next word */
        static String next() throws IOException {
            while ( ! tokenizer.hasMoreTokens() ) {
                //TODO add check for eof if necessary
                tokenizer = new StringTokenizer(
                    reader.readLine() );
            }
            return tokenizer.nextToken();
        }

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

        static double nextDouble() throws IOException {
            return Double.parseDouble( next() );
        }
        static long nextLong() throws IOException {
            return Long.parseLong( next() );
        }

    }
}


import java.util.*;
import java.math.*;
import java.io.*;
 import java.text.DecimalFormat;
public class Main{
    static int[] arr;
    static String h;
    static int mod=1000000007 ;
    public static void main(String[] args)  throws IOException {
        s.init(System.in);
        int n=s.nextInt();
        int[] a=new int[n];
        boolean[] is=new boolean[200001];
        int m=200001;
        for(int i=0;i<n;i++){
            a[i]=s.nextInt();
            is[a[i]]=true;
        }
        int[] last=new int[m];
        int p=0;
        for(int i=1;i<n;i++){
            if(is[i])
                p=i;
            last[i]=p;
        }
        int ans=0;
        for(int i=2;i<m;i++){
            if(!is[i])
                continue;
            for(int j=1;j*i<m;j++){
                int z=Math.min(m-1,i+i*j-1);
                int x=last[z];
                ans=Math.max(ans,i%x);
            }
        }
        System.out.println(ans);
    }

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

        /** call this method to initialize reader for InputStream */
        static void init(InputStream input) {
            reader = new BufferedReader(
                        new InputStreamReader(input) );
            tokenizer = new StringTokenizer("");
        }

        /** get next word */
        static String next() throws IOException {
            while ( ! tokenizer.hasMoreTokens() ) {
                //TODO add check for eof if necessary
                tokenizer = new StringTokenizer(
                    reader.readLine() );
            }
            return tokenizer.nextToken();
        }

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

        static double nextDouble() throws IOException {
            return Double.parseDouble( next() );
        }
        static long nextLong() throws IOException {
            return Long.parseLong( next() );
        }

    }
}


import java.util.*;
import java.math.*;
import java.io.*;
 import java.text.DecimalFormat;
public class Main{
    static int[] arr;
    static String h;
    static int mod=1000000007 ;
    public static void main(String[] args)  throws IOException {
        s.init(System.in);
        int n=s.nextInt();
        int[] a=new int[n];
        boolean[] is=new boolean[2000001];
        int m=2000001;
        for(int i=0;i<n;i++){
            a[i]=s.nextInt();
            is[a[i]]=true;
        }
        int[] last=new int[m];
        int p=0;
        for(int i=1;i<m;i++){
            if(is[i])
                p=i;
            last[i]=p;
        }
        int ans=0;
        for(int i=2;i<m;i++){
            if(!is[i])
                continue;
            for(int j=1;j*i<m;j++){
                int z=Math.min(m-1,i+i*j-1);
                int x=last[z];
                ans=Math.max(ans,x%i);
            }
        }
        System.out.println(ans);
    }

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

        /** call this method to initialize reader for InputStream */
        static void init(InputStream input) {
            reader = new BufferedReader(
                        new InputStreamReader(input) );
            tokenizer = new StringTokenizer("");
        }

        /** get next word */
        static String next() throws IOException {
            while ( ! tokenizer.hasMoreTokens() ) {
                //TODO add check for eof if necessary
                tokenizer = new StringTokenizer(
                    reader.readLine() );
            }
            return tokenizer.nextToken();
        }

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

        static double nextDouble() throws IOException {
            return Double.parseDouble( next() );
        }
        static long nextLong() throws IOException {
            return Long.parseLong( next() );
        }

    }
}


import java.util.*;
import java.math.*;
import java.io.*;
 import java.text.DecimalFormat;
public class Main{
    static int[] arr;
    static String h;
    static int mod=1000000007 ;
    public static void main(String[] args)  throws IOException {
        s.init(System.in);
        int n=s.nextInt();
        int[] a=new int[n];
        boolean[] is=new boolean[200001];
        int m=200001;
        for(int i=0;i<n;i++){
            a[i]=s.nextInt();
            is[a[i]]=true;
        }
        int[] last=new int[m];
        int p=0;
        for(int i=1;i<m;i++){
            if(is[i])
                p=i;
            last[i]=p;
        }
        int ans=0;
        for(int i=2;i<m;i++){
            if(!is[i])
                continue;
            for(int j=1;j*i<m;j++){
                int z=Math.min(m-1,i+i*j-1);
                int x=last[z];
                ans=Math.max(ans,x%i);
            }
        }
        System.out.println(ans);
    }

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

        /** call this method to initialize reader for InputStream */
        static void init(InputStream input) {
            reader = new BufferedReader(
                        new InputStreamReader(input) );
            tokenizer = new StringTokenizer("");
        }

        /** get next word */
        static String next() throws IOException {
            while ( ! tokenizer.hasMoreTokens() ) {
                //TODO add check for eof if necessary
                tokenizer = new StringTokenizer(
                    reader.readLine() );
            }
            return tokenizer.nextToken();
        }

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

        static double nextDouble() throws IOException {
            return Double.parseDouble( next() );
        }
        static long nextLong() throws IOException {
            return Long.parseLong( next() );
        }

    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = MyConsoleInputStreamParser.GetInt;
            var firstArray = MyConsoleInputStreamParser.GetIntArray;
            var secondArray = MyConsoleInputStreamParser.GetIntArray;
            var firstOR = firstArray[0];
            var seconOR = secondArray[0];
            for (int i = 1; i < n; i++)
            {
                firstOR = firstOR | firstArray[i];
                seconOR = seconOR | secondArray[i];
            }
            Console.WriteLine(firstOR + seconOR);
        }

        public static class MyConsoleInputStreamParser
        {
            private static TextReader reader = new StreamReader(Console.OpenStandardInput());
            private static Queue<string> currentLineStrings = new Queue<string>();

            public static string GetLine { get { return reader.ReadLine(); } }

            public static string GetString { get { while (currentLineStrings.Count == 0) currentLineStrings = new Queue<string>(GetStringArray); return currentLineStrings.Dequeue(); } }

            public static int GetInt { get { return int.Parse(GetString); } }

            public static long GetLong { get { return long.Parse(GetString); } }

            public static double GetDouble { get { return double.Parse(GetString, CultureInfo.InvariantCulture); } }

            public static char[] GetCharArray { get { return GetLine.ToCharArray(); } }

            public static string[] GetStringArray { get { return GetLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); } }

            public static int[] GetIntArray { get { return GetStringArray.Select(int.Parse).ToArray(); } }

            public static long[] GetLongArray { get { return GetStringArray.Select(long.Parse).ToArray(); } }

            public static double[] GetDoubleArray { get { return GetStringArray.Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); } }

            public static char[][] ReturnCharMatrix(int numberOfRows) { char[][] matrix = new char[numberOfRows][]; for (int i = 0; i < numberOfRows; i++) matrix[i] = GetCharArray; return matrix; }

            public static string[][] ReturnStringMatrix(int numberOfRows) { string[][] matrix = new string[numberOfRows][]; for (int i = 0; i < numberOfRows; i++) matrix[i] = GetStringArray; return matrix; }

            public static int[][] ReturnIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++) matrix[i] = GetIntArray; return matrix; }

            public static long[][] ReturnLongMatrix(int numberOfRows) { long[][] matrix = new long[numberOfRows][]; for (int i = 0; i < numberOfRows; i++) matrix[i] = GetLongArray; return matrix; }

            public static double[][] ReturnDoubleMatrix(int numberOfRows) { double[][] matrix = new double[numberOfRows][]; for (int i = 0; i < numberOfRows; i++) matrix[i] = GetDoubleArray; return matrix; }
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Globalization;

class ProgramHr
{
    static void Main ( string[] args )
    {
        baseFileName = @"d:\zin";
        try
        {
            string inpFileName = baseFileName + ".txt";
            if ( File.Exists ( inpFileName ) )
                Console.SetIn ( new StreamReader ( inpFileName ) );
            sw1 = new StreamWriter ( baseFileName + "-out.txt" );
            localTest = true;
            //Console.SetOut( sw1 );
        }
        catch
        {
        }
        inp = new ConsoleInput ( );
        inp.ReadAll ( );

        //long T = inp.NextInt();
        long T = 1;
        for ( int t = 0; t < T; t++ )
        {
            // start case
            int N = inp.NextInt ( );
            long[] a = new long[N];
            long[] b = new long[N];
            for ( int i = 0; i < N; i++ )
                a[i] = inp.NextLong ( );
            for ( int i = 0; i < N; i++ )
                b[i] = inp.NextLong ( );

            long ans = long.MinValue;
            for ( int l = 0; l < N; l++ )
            {
                long aor = a[l];
                long bor = b[l];
                for ( int r = l; r < N; r++ )
                {
                    aor = aor | a[r];
                    bor = bor | b[r];
                    long sum = aor + bor;
                    ans = Math.Max ( ans, sum );
                }
            }
            Console.WriteLine ( ans );

            // end case
            // ====
        }

        if ( sw1 != null ) sw1.Close ( );
    }

    //====================================================//
    static ConsoleInput inp;
    public static string baseFileName = @"d:\zin";
    public static StreamWriter sw1 = null;
    public static bool localTest = false;

    class ConsoleInput
    {
        List<string> lines = new List<string> ( );
        int idx = 0;

        public void ReadAll ( )
        {
            while ( true )
            {
                string line = Console.ReadLine ( );
                if ( line == null )
                    break;
                lines.AddRange ( line.Split ( new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries ) );
            }
        }

        public string NextString ( )
        {
            return lines[idx++];
        }

        public int NextInt ( )
        {
            return int.Parse ( lines[idx++] );
        }

        public long NextLong ( )
        {
            return long.Parse ( lines[idx++] );
        }

        public double NextDouble ( )
        {
            return double.Parse ( lines[idx++] );
        }

    }

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _631A
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            string[] input1 = Console.ReadLine().Split(' '), input2 = Console.ReadLine().Split(' ');
            long a = 0, b = 0;
            for (int i = 0; i < n; i++)
            {
                a |= long.Parse(input1[i]);
                b |= long.Parse(input2[i]);
            }
            Console.WriteLine(a + b);
            //Console.ReadLine();
        }
    }
}


using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Numerics;

namespace ConsoleApplication1
{
    class Program : ProgramBase
    {
        private const long commonMod = 1000000007;
        private int index = 0;
        static void Main(string[] args)
        {
            var p = new Program();
            var x = p.Get();
            p.Out(x);
        }
        class Cut
        {
            public int a;
            public int b;
            public int line;
        }

        object Get()
        {
            checked
            {
                var n = ReadInt();
                var a = ReadInts();
                var b = ReadInts();
                var ora = 0;
                var orb = 0;
                for (int i = 0; i < n; i++)
                {
                    ora |= a[i];
                    orb |= b[i];
                }
                return ora + orb;
            }
        }
        class SegmentTree {
            private Node root;

            public SegmentTree()
            {
                root = new Node();
                Build(root, 20);
            }

            private void Build(Node node, int level)
            {
                if (level == 0)
                {
                    node.IsLeaf = true;
                    node.Level = level;
                }
                else
                {
                    node.Level = level;
                    node.Left = new Node();
                    Build(node.Left, level - 1);
                    node.Right = new Node();
                    Build(node.Right, level - 1);
                }
            }

            public void InsertDay(int d, long a, long min, long max)
            {
                root.Insert(d, a, min, max);
            }

            public long SumSmall(int d)
            {
                return root.SumSmall(d);
            }

            public long SumLarge(int d)
            {
                return root.SumLarge(d);
            }

            private class Node {
                public long Small;
                public long Large;
                public long Value;
                public bool IsLeaf;
                public Node Left;
                public Node Right;
                public int Level;
                public Tuple<long, long> Insert(int d, long v, long min, long max)
                {
                    if (IsLeaf)
                    {
                        Value += v;
                        var minDelta = Math.Min(Value, min) - Small;
                        var maxDelta = Math.Min(Value, max) - Large;
                        Small = Math.Min(Value, min);
                        Large = Math.Min(Value, max);
                        return Tuple.Create(minDelta, maxDelta);
                    }
                    else
                    {
                        var leftCount = (1 << (Level - 1));
                        Tuple<long, long> result;
                        if (leftCount >= d)
                            result = Left.Insert(d, v, min, max);
                        else
                            result = Right.Insert(d - leftCount, v, min, max);
                        Small += result.Item1;
                        Large += result.Item2;
                        return result;
                    }
                }

                public long SumSmall(int d)
                {
                    if (d == 0)
                        return 0;
                    if (IsLeaf)
                    {
                        return Small;
                    }
                    else
                    {
                        var leftCount = (1 << (Level - 1));
                        if (leftCount >= d)
                            return Left.SumSmall(d);
                        return Left.Small + Right.SumSmall(d - leftCount);
                    }
                }
                public long SumLarge(int d)
                {
                    if (IsLeaf)
                        return d == 1 ? Large : 0;
                    else
                    {
                        var leftCount = (1 << (Level - 1));
                        if (leftCount >= d)
                            return Right.Large + Left.SumLarge(d);
                        return Right.SumLarge(d - leftCount);
                    }
                }
            }
        }
        object Get2()
        {
            FileName = "strange";
            checked
            {
                var s = Read();
                var first = GetNext(s);
                var second = GetNext(s);
                var single = new Dictionary<char, int>();
                var twin = new Dictionary<Tuple<char, char>, List<Tuple<long, long>>>();
                single[first.Item1] = first.Item2;
                if (second == null)
                    return first.Item2;
                do
                {
                    if (!single.ContainsKey(second.Item1) || single[second.Item1] < second.Item2)
                        single[second.Item1] = second.Item2;
                    var pair = Tuple.Create(first.Item1, second.Item1);
                    if (!twin.ContainsKey(pair))
                        twin[pair] = new List<Tuple<long, long>>();
                    twin[pair].Add(Tuple.Create((long)first.Item2, (long)second.Item2));
                    first = second;
                    second = GetNext(s);
                } while (second != null);
                long result = single.Select(x => x.Value).Sum();
                foreach (var kv in twin)
                    result += ProcessPair(kv.Value);
                return result;
            }
        }

        public long ProcessPair(List<Tuple<long, long>> counts)
        {
            checked
            {
                var sorted = counts.OrderByDescending(x => x.Item1).ThenByDescending(x => x.Item2).ToArray();
                var filtered = new List<Tuple<long, long>>() { sorted[0] };
                for (int i = 1; i < sorted.Length; i++)
                    if (sorted[i].Item2 > filtered.Last().Item2)
                        filtered.Add(sorted[i]);

                var previous = filtered[0];
                var result = previous.Item1 * previous.Item2;
                for (int i = 1; i < filtered.Count; i++)
                    result += (filtered[i].Item2 - previous.Item2) * filtered[i].Item1;
                return result;
            }
        }

        public Tuple<char, int> GetNext(string s)
        {
            if (index == s.Length)
                return null;
            var ch = s[index];
            var count = 0;
            while (index < s.Length && ch == s[index])
            {
                count++;
                index++;
            }
            return Tuple.Create(ch, count);
        }

        public double ProcessPair(long[] a, long[] b, int p)
        {
            return ((double)NumberOfDivs(a, p) * 2000) / Count(a) +
                ((double)NumberOfDivs(b, p) * 2000) / Count(b) -
                ((double)(NumberOfDivs(a, p) * NumberOfDivs(b, p))) * 2000 / (Count(a) * Count(b));
        }

        public long NumberOfDivs(long[] a, int p)
        {
            return a[1] / p - (a[0] - 1) / p;
        }

        public long Count(long[] a)
        {
            return a[1] - a[0] + 1;
        }

        bool DoubleEquals(double a, double b)
        {
            return Math.Abs(a - b) < 0.0000000001;
        }

        bool SameLine(double[] a, double[] b, double[] c)
        {
            var x = a[0];
            var y = a[1];
            var x1 = b[0];
            var y1 = b[1];
            var x2 = c[0];
            var y2 = c[1];
            return DoubleEquals((x - x1) * (y2 - y1), (y - y1) * (x2 - x1));
        }

        double Distance(double[] a, double[] b)
        {
            return Math.Sqrt((a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]));
        }

        object Get7()
        {
            checked
            {
                var m = ReadInt();
                var p = ReadLongs();
                var arr = p.GroupBy(x => x).Select(g => new { g.Key, Count = g.Count() }).ToArray();
                var even = arr.Where(x => (x.Count & 1) == 0).FirstOrDefault();
                if (even != null)
                {
                    var index = Array.IndexOf(arr, even);
                    //arr[index].Count = arr[index].Count / 2;
                }
                long divs = 1;
                long result = 1;
                foreach (var a in arr)
                {
                    var pi = a.Key;
                    var curr = pi;
                    var olddivs = divs;
                    var oldres = result;
                    for (int i = 0; i < a.Count; i++)
                    {
                        result = (result * oldres % commonMod) * FastPow(curr, olddivs, commonMod) % commonMod;
                        curr = curr * pi % commonMod;
                        divs = (divs + olddivs) % (commonMod - 1);
                    }
                }
                return result;
            }
        }

        public long FastPow(long x, BigInteger pow, long mod)
        {
            if (pow == 0)
                return 1;
            if ((pow & 1) == 0)
                return FastPow(x * x % mod, pow / 2, mod);
            return x * FastPow(x, pow - 1, mod) % mod;
        }

        //object Get3()
        //{
        //    FileName = "";
        //    checked
        //    {
        //        var n = ReadInt();
        //        var hor = new List<Cut>();
        //        var ver = new List<Cut>();
        //        for (int i = 0; i < n; i++)
        //        {
        //            var a = ReadInts();
        //            if (a[0] == a[2])
        //                ver.Add(new Cut() { a = Math.Min(a[1], a[3]), b = Math.Max(a[1], a[3]), line = a[0] });
        //            else
        //                hor.Add(new Cut() { a = Math.Min(a[0], a[2]), b = Math.Max(a[0], a[2]), line = a[1] });
        //        }
        //        ver = Merge(ver);
        //        hor = Merge(hor);

        //    }
        //}

        List<Cut> Merge(List<Cut> l)
        {
            var a = l.OrderBy(x => x.line).ThenBy(x => x.a).ToArray();
            Cut previous = null;
            var result = new List<Cut>();
            for (int i = 0; i < a.Length; i++)
                if (previous == null)
                    previous = a[i];
                else
                    if (previous.line == a[i].line && previous.b >= a[i].a)
                        previous.b = Math.Max(previous.b, a[i].b);
                    else
                    {
                        result.Add(previous);
                        previous = a[i];
                    }
            if (previous != null)
                result.Add(previous);
            return result;
        }

    }
    abstract class ProgramBase
    {
        bool? prod = null;
        StreamReader f;
        protected string FileName { private get; set; }

        protected string Read()
        {
            if (string.IsNullOrEmpty(FileName))
                return Console.ReadLine();
            if (f == null)
                f = new StreamReader((GetProd() ? "" : "c:\\dev\\") + FileName + ".in");
            return f.ReadLine();
        }

        protected int ReadInt()
        {
            return Convert.ToInt32(Read());
        }
        protected int[] ReadInts()
        {
            return Read().Split(' ').Select(x => Convert.ToInt32(x)).ToArray();
        }

        protected long ReadLong()
        {
            return Convert.ToInt64(Read());
        }

        protected long[] ReadLongs()
        {
            return Read().Split(' ').Select(x => Convert.ToInt64(x)).ToArray();
        }

        public void Out(object r)
        {
            if (string.IsNullOrEmpty(FileName))
                Console.WriteLine(r);
            else if (GetProd())
                File.WriteAllText(FileName + ".out", r.ToString());
            else
                Console.WriteLine(r);
        }

        private bool GetProd()
        {
            if (!prod.HasValue)
                try
                {
                    prod = Environment.MachineName != "RENADEEN-NB-W7";
                }
                catch (Exception)
                {
                    prod = true;
                }
            return prod.Value;
        }
    }

    public static class Huj
    {
        public static T MinBy<T>(this IEnumerable<T> source, Func<T, double> func) where T : class
        {
            T result = null;
            var min = double.MaxValue;
            foreach (var item in source)
            {
                var current = func(item);
                if (min > current)
                {
                    min = current;
                    result = item;
                }
            }
            return result;
        }

        public static T MaxBy<T>(this IEnumerable<T> source, Func<T, double> func)
        {
            T result = default(T);
            var max = double.MinValue;
            foreach (var item in source)
            {
                var current = func(item);
                if (max < current)
                {
                    max = current;
                    result = item;
                }
            }
            return result;
        }

        public static string JoinStrings(this IEnumerable<string> source, string delimeter)
        {
            return string.Join(delimeter, source.ToArray());
        }
    }
}


using System;

class Program
{
    static void Main(string[] args)
    {
        int n = int.Parse(Console.ReadLine());
        var inp = Console.ReadLine().Split(' ');
        var inp2 = Console.ReadLine().Split(' ');
        int[] a1 = new int[n], a2 = new int[n];
        int r1 = 0, r2 = 0;
        for (int i = 0; i < n; i++)
        {
            a1[i] = int.Parse(inp[i]);
            a2[i] = int.Parse(inp2[i]);
            r1 |= a1[i];
            r2 |= a2[i];
        }
        long r = r1 + r2;
        Console.WriteLine(r);
        Console.ReadLine();
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Globalization;

#if DEBUG
using System.Diagnostics;
#endif

namespace CompetitiveProgramming
{
    class Program
    {
        static bool s_IsMultipleTestCases = false;
        static void Main(string[] args)
        {
#if DEBUG
            s_IsMultipleTestCases = true;
            var streamReader = new StreamReader("../../input.txt", Encoding.ASCII, false, 32768);
            var streamWriter = new StreamWriter("../../output.txt", false, Encoding.ASCII, 32768);
            var timer = new Stopwatch();
#else
            var streamReader = new StreamReader(Console.OpenStandardInput(32768), Encoding.ASCII, false, 32768);
            var streamWriter = new StreamWriter(Console.OpenStandardOutput(32768), Encoding.ASCII, 32768);
#endif
            var reader = new InputReader(streamReader);

            var numberOfTest = 1;
            if (s_IsMultipleTestCases)
            {
                numberOfTest = reader.NextInt();
            }
            for (var testNumber = 1; testNumber <= numberOfTest; ++testNumber)
            {
#if DEBUG
                timer.Start();
#endif
                Task solver = new Task();
                solver.Solve(testNumber, reader, streamWriter);
#if DEBUG
                streamWriter.WriteLine("Eslapsed Time: " + timer.ElapsedMilliseconds + " ms\n");
                timer.Reset();
#endif
            }
            streamReader.Close();
            streamWriter.Close();
        }
    }

    class Task
    {
        const long MOD = 1000000007;
        const int MAXN = 200000;
        long ans = 0;
        string res = string.Empty;

        public void Solve(int testNumber, InputReader reader, StreamWriter writer)
        {
            int n = reader.NextInt();
            var a = reader.NLS(n);
            var b = reader.NLS(n);
            var xa = new long[n + 1];
            var xb = new long[n + 1];

            for (int i = 0; i < n; i++)
            {
                xa[i + 1] = xa[i] | a[i];
                xb[i + 1] = xb[i] | b[i];
            }

            for (int i = 0; i <= n; i++)
            {
                for (int j = i; j <= n; j++)
                {
                    ans = Max(ans, (xa[j] | xa[i]) + (xb[j] | xb[i]));
                }
            }
            writer.WriteLine(ans);
        }

        #region Utilities
        private string ConvertArrayToString(int[] arrs)
        {
            var sb = new StringBuilder();
            foreach (var item in arrs)
            {
                sb.Append(item);
                sb.Append("\n");
            }
            return sb.ToString();
        }

        private string ConvertArrayToString(long[] arrs)
        {
            var sb = new StringBuilder();
            foreach (var item in arrs)
            {
                sb.Append(item);
                sb.Append("\n");
            }
            return sb.ToString();
        }

        private string ConvertDoubleToString(double x)
        {
            return x.ToString(CultureInfo.InvariantCulture);
        }

        private int Max(int a, int b)
        {
            return Math.Max(a, b);
        }

        private int Min(int a, int b)
        {
            return Math.Min(a, b);
        }

        private long Max(long a, long b)
        {
            return Math.Max(a, b);
        }

        private long Min(long a, long b)
        {
            return Math.Min(a, b);
        }

        private long GCD(long a, long b)
        {
            return a > 0 ? b : GCD(b, a % b);
        }

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

        #endregion
    }

    class InputReader
    {
        private char[] _inbuf;
        private int _lenbuf = 0, _ptrbuf = 0;
        private StreamReader _reader;

        public InputReader(StreamReader reader)
        {
            _reader = reader;
            _inbuf = new char[1024];
            _lenbuf = 0;
            _ptrbuf = 0;
        }

        private int ReadByte()
        {
            if (_lenbuf == -1)
                throw new Exception();
            if (_ptrbuf >= _lenbuf)
            {
                _ptrbuf = 0;
                try
                {
                    _lenbuf = _reader.Read(_inbuf, 0, 1024);
                }
                catch (IOException e)
                {
                    throw e;
                }
                if (_lenbuf <= 0)
                    return -1;
            }
            return _inbuf[_ptrbuf++];
        }

        private bool IsSpaceChar(int c)
        {
            return !(c >= 33 && c <= 126);
        }

        private int Skip()
        {
            int b;
            while ((b = ReadByte()) != -1 && IsSpaceChar(b)) ;
            return b;
        }

        public int NextInt()
        {
            int num = 0, b;
            var minus = false;
            while ((b = ReadByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
            if (b == '-')
            {
                minus = true;
                b = ReadByte();
            }

            while (true)
            {
                if (b >= '0' && b <= '9')
                {
                    num = num * 10 + (b - '0');
                }
                else
                {
                    return minus ? -num : num;
                }
                b = ReadByte();
            }
        }

        public long NextLong()
        {
            long num = 0, b;
            var minus = false;
            while ((b = ReadByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
            if (b == '-')
            {
                minus = true;
                b = ReadByte();
            }

            while (true)
            {
                if (b >= '0' && b <= '9')
                {
                    num = num * 10 + (b - '0');
                }
                else
                {
                    return minus ? -num : num;
                }
                b = ReadByte();
            }
        }

        public string Next()
        {
            var b = Skip();
            var sb = new StringBuilder();
            while (!(IsSpaceChar(b)))
            {
                sb.Append((char)b);
                b = ReadByte();
            }
            return sb.ToString();
        }

        public double NextDouble()
        {
            return double.Parse(Next(), CultureInfo.InvariantCulture);
        }

        public int[] NIS(int n)
        {
            var arrs = new int[n];
            for (int i = 0; i < n; i++)
            {
                arrs[i] = NextInt();
            }
            return arrs;
        }

        public long[] NLS(int n)
        {
            var arrs = new long[n];
            for (int i = 0; i < n; i++)
            {
                arrs[i] = NextLong();
            }
            return arrs;
        }
    }

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.IO;
using System.Numerics;
using System.Globalization;
using System.Threading;

namespace probleme {
    class Program {
        static void Main(string[] args) {
            //Console.SetIn(new StreamReader("file.txt"));
            int n = int.Parse(Console.ReadLine());
            int fa = 0, fb = 0;
            int[] a = Array.ConvertAll(Console.ReadLine().Split(' '), tz => int.Parse(tz));
            int[] b = Array.ConvertAll(Console.ReadLine().Split(' '), tz => int.Parse(tz));
            for (int i = 0; i < n; i++) {
                fa = fa | a[i];
                fb = fb | b[i];
            }
            Console.Write(fa + fb);
            //Console.ReadKey();
        }
    }
}


using System;
using System.Globalization;
using System.IO;
using System.Linq;

namespace CF317
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var sr = new InputReader(Console.In);
            //var sr = new InputReader(new StreamReader("input.txt"));
            var task = new Task();
            using (var sw = Console.Out)
            {
                task.Solve(1, sr, sw);
                //Console.ReadKey();
            }
        }
    }

    internal class Task
    {
        public void Solve(int testNumber, InputReader sr, TextWriter sw)
        {
            var n = sr.NextInt32();
            var arrayA = sr.ReadArray(Int64.Parse);
            var arrayB = sr.ReadArray(Int64.Parse);
            var currMax = 0L;
            for (var i = 0; i < n; i++)
            {
                var currA = arrayA[i];
                var currB = arrayB[i];
                currMax = Math.Max(currA + currB, currMax);
                for (var j = i + 1; j < n; j++)
                {
                    var nextA = currA | arrayA[j];
                    var nextB = currB | arrayB[j];
                    currMax = Math.Max(currMax, nextA + nextB);
                    currA = nextA;
                    currB = nextB;
                }
            }

            sw.WriteLine(currMax);
        }
    }

    internal class InputReader : IDisposable
    {
        private bool isDispose;
        private readonly TextReader sr;

        public InputReader(TextReader stream)
        {
            sr = stream;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        public string NextString()
        {
            var result = sr.ReadLine();
            return result;
        }

        public int NextInt32()
        {
            return Int32.Parse(NextString());
        }

        public long NextInt64()
        {
            return Int64.Parse(NextString());
        }

        public string[] NextSplitStrings()
        {
            return NextString()
                .Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
        }

        public T[] ReadArray<T>(Func<string, CultureInfo, T> func)
        {
            return NextSplitStrings()
                .Select(s => func(s, CultureInfo.InvariantCulture))
                .ToArray();
        }

        protected void Dispose(bool dispose)
        {
            if (!isDispose)
            {
                if (dispose)
                    sr.Close();
                isDispose = true;
            }
        }

        ~InputReader()
        {
            Dispose(false);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Task1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] arr1 = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
            int[] arr2 = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
            long sum1 = 0;
            long sum2 = 0;
            for (int i = 0; i < n; i++)
            {
                sum1 |= arr1[i];
                sum2 |= arr2[i];
            }
            Console.WriteLine(sum1 + sum2);
        }
    }
}