#include <bits/stdc++.h>
#include <string>
#include <vector>
#include <algorithm>
#define rep(i,n) for (int i = 0;i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
int INF = 1e9;


int main(){
    int k,t;
    cin >> k >> t;
    multiset<int> s;
    rep(i,t){
        int temp;
        cin >> temp;
        s.insert(temp);
    }
    int st = 0;
    rep(i,k){
        if(s.size()==0){
            cout << st << endl;
            return 0;
        }
 
 
        int temp = *(--s.end());
        --temp;
        //cout << " " << temp << endl;
        s.erase(--s.end());
        //cout << "  " << s.size() << endl;
        if(st>0&&i>0) s.insert(st);
        st = temp;
    }

    cout << 0 << endl;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define erep(n) for (ll i = 0; i <= n; ++i)
#define erep2(i, n) for (ll i = 0; i <= n; ++i)
#define erep3(i, a, b) for (ll i = a; i <= b; ++i)
#define erep4(i, a, b, c) for (ll i = a; i <= b; i += c)
#define rrep(i, a, b) for (ll i = a; i > b; --i)
#define rrep2(i, a, b, c) for (ll i = a; i > b; i -= c)
#define errep(i, a, b) for (ll i = a; i >= b; --i)
#define errep2(i, a, b, c) for (ll i = a; i >= b; i -= c)
#define print(a) cout << a << endl;
#define printVec(v) printf("{"); for (const auto& i : v) { std::cout << i << ", "; } printf("}\n");
#define cin(x) cin >> x;
const ll MOD = 1000000007;
const double PI = 3.14159265358979323846264338327950;
inline int newline() { putchar('\n'); return 0; }

int main() {
    ll left, right;
    cin(left); cin(right);

    if (2019 - left % 2019 <= right - left) {
        print(0);
        return 0;

    } else {
        ll result = LLONG_MAX;
        rep3(i, left, right) {
            erep3(j, i + 1, right) {
                if (i * j % 2019 < result) result = i * j % 2019;
            }
        }
        print(result);
        return 0;
    }
}