#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

using namespace std;

struct para
{
    int a, b;
};

vector<int> Z;

void Stworz(int n)
{
    for (int i = 0; i < n; ++i)
        Z.push_back(i);
}

int Find(int a)
{
    if (Z[a] == a)
        return a;
    int fa = Find(Z[a]);
    Z[a] = fa;
    return fa;
}

int Union(int a, int b)
{
    int fa = Find(a);
    int fb = Find(b);
    if (fa != fb)
    {
        Z[fa] = fb;
        return 1;
    }
    return 0;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    sort(a.begin(), a.end());
    vector< vector<para> > pary(1000001);
    vector< unordered_set<int> > S(1000001);
    for (int i = 0; i < n; ++i)
    {
        for (int j = i + 1; j < n; ++j)
        {
            int r = a[j] - a[i];
            if (r >= n - k && S[r].size() <= 8)
            {
                pary[r].push_back({i, j});
                S[r].insert(i);
                S[r].insert(j);
            }
        }
    }
    Stworz(n);
    vector<bool> jest(1000001, false);
    for (int i = max(1, n - k); i <= 1000000; ++i)
    {
        vector<int> rozne;
        bool ok = true;
        for (int j = i; j <= 1000000; j += i)
        {
            if (S[j].size() > 8)
            {
                ok = false;
                break;
            }
            for (int l = 0; l < pary[j].size(); ++l)
            {
                if (!jest[pary[j][l].a])
                {
                    rozne.push_back(pary[j][l].a);
                    jest[pary[j][l].a] = true;
                }
                if (!jest[pary[j][l].b])
                {
                    rozne.push_back(pary[j][l].b);
                    jest[pary[j][l].b] = true;
                }
                if (rozne.size() > 8)
                    break;
            }
            if (rozne.size() > 8)
            {
                ok = false;
                break;
            }
        }
        if (!ok)
        {
            for (int j = 0; j < rozne.size(); ++j)
            {
                Z[rozne[j]] = rozne[j];
                jest[rozne[j]] = false;
            }
            continue;
        }
        int spojne = rozne.size();
        for (int j = i; j <= 1000000; j += i)
            for (int l = 0; l < pary[j].size(); ++l)
                spojne -= Union(pary[j][l].a, pary[j][l].b);
        if (rozne.size() - spojne <= k)
        {
            cout << i;
            return 0;
        }
        for (int j = 0; j < rozne.size(); ++j)
        {
            Z[rozne[j]] = rozne[j];
            jest[rozne[j]] = false;
        }
    }
}


#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

using namespace std;

struct para
{
    int a, b;
};

vector<int> Z;

void Stworz(int n)
{
    for (int i = 0; i < n; ++i)
        Z.push_back(i);
}

int Find(int a)
{
    if (Z[a] == a)
        return a;
    int fa = Find(Z[a]);
    Z[a] = fa;
    return fa;
}

int Union(int a, int b)
{
    int fa = Find(a);
    int fb = Find(b);
    if (fa != fb)
    {
        Z[fa] = fb;
        return 1;
    }
    return 0;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    sort(a.begin(), a.end());
    vector< vector<para> > pary(1000001);
    vector< unordered_set<int> > S(1000001);
    for (int i = 0; i < n; ++i)
    {
        for (int j = i + 1; j < n; ++j)
        {
            int r = a[j] - a[i];
            if (S[r].size() <= 8)
            {
                pary[r].push_back({i, j});
                S[r].insert(i);
                S[r].insert(j);
            }
        }
    }
    Stworz(n);
    vector<bool> jest(1000001, false);
    for (int i = n; i <= 1000000; ++i)
    {
        vector<int> rozne;
        bool ok = true;
        for (int j = i; j <= 1000000; j += i)
        {
            if (S[j].size() > 8)
            {
                ok = false;
                break;
            }
            for (int l = 0; l < pary[j].size(); ++l)
            {
                if (!jest[pary[j][l].a])
                {
                    rozne.push_back(pary[j][l].a);
                    jest[pary[j][l].a] = true;
                }
                if (!jest[pary[j][l].b])
                {
                    rozne.push_back(pary[j][l].b);
                    jest[pary[j][l].b] = true;
                }
                if (rozne.size() > 8)
                    break;
            }
            if (rozne.size() > 8)
            {
                ok = false;
                break;
            }
        }
        if (!ok)
        {
            for (int j = 0; j < rozne.size(); ++j)
            {
                Z[rozne[j]] = rozne[j];
                jest[rozne[j]] = false;
            }
            continue;
        }
        int spojne = rozne.size();
        for (int j = i; j <= 1000000; j += i)
            for (int l = 0; l < pary[j].size(); ++l)
                spojne -= Union(pary[j][l].a, pary[j][l].b);
        if (rozne.size() - spojne <= k)
        {
            cout << i;
            return 0;
        }
        for (int j = 0; j < rozne.size(); ++j)
        {
            Z[rozne[j]] = rozne[j];
            jest[rozne[j]] = false;
        }
    }
}


#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

using namespace std;

struct para
{
    int a, b;
};

vector<int> Z;

void Stworz(int n)
{
    for (int i = 0; i < n; ++i)
        Z.push_back(i);
}

int Find(int a)
{
    if (Z[a] == a)
        return a;
    int fa = Find(Z[a]);
    Z[a] = fa;
    return fa;
}

int Union(int a, int b)
{
    int fa = Find(a);
    int fb = Find(b);
    if (fa != fb)
    {
        Z[fa] = fb;
        return 1;
    }
    return 0;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    sort(a.begin(), a.end());
    vector< vector<para> > pary(1000001);
    for (int i = 0; i < n; ++i)
    {
        for (int j = n - 1; j > i; --j)
        {
            int r = a[j] - a[i];
            if (r < n - k)
                break;
            if (pary[r].size() <= 5)
                pary[r].push_back({i, j});
        }
    }
    Stworz(n);
    vector<bool> jest(1000001, false);
    for (int i = max(1, n - k); i <= 1000000; ++i)
    {
        vector<int> rozne;
        bool ok = true;
        for (int j = i; j <= 1000000; j += i)
        {
            if (pary[j].size() > 5)
            {
                ok = false;
                break;
            }
            for (int l = 0; l < pary[j].size(); ++l)
            {
                if (!jest[pary[j][l].a])
                {
                    rozne.push_back(pary[j][l].a);
                    jest[pary[j][l].a] = true;
                }
                if (!jest[pary[j][l].b])
                {
                    rozne.push_back(pary[j][l].b);
                    jest[pary[j][l].b] = true;
                }
            }
            if (rozne.size() > 8)
            {
                ok = false;
                break;
            }
        }
        if (!ok)
        {
            for (int j = 0; j < rozne.size(); ++j)
            {
                Z[rozne[j]] = rozne[j];
                jest[rozne[j]] = false;
            }
            continue;
        }
        int spojne = rozne.size();
        for (int j = i; j <= 1000000; j += i)
            for (int l = 0; l < pary[j].size(); ++l)
                spojne -= Union(pary[j][l].a, pary[j][l].b);
        if (rozne.size() - spojne <= k)
        {
            cout << i;
            return 0;
        }
        for (int j = 0; j < rozne.size(); ++j)
        {
            Z[rozne[j]] = rozne[j];
            jest[rozne[j]] = false;
        }
    }
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

struct para
{
    int a, b;
};

vector<int> Z;

void Stworz(int n)
{
    for (int i = 0; i < n; ++i)
        Z.push_back(i);
}

int Find(int a)
{
    if (Z[a] == a)
        return a;
    int fa = Find(Z[a]);
    Z[a] = fa;
    return fa;
}

void Union(int a, int b)
{
    int fa = Find(a);
    int fb = Find(b);
    if (fa != fb)
        Z[fa] = fb;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    sort(a.begin(), a.end());
    vector< vector<para> > pary(1000001);
    vector< set<int> > S(1000001);
    for (int i = 0; i < n; ++i)
    {
        for (int j = i + 1; j < n; ++j)
        {
            int r = a[j] - a[i];
            if (S[r].size() <= 8)
            {
                pary[r].push_back({i, j});
                S[r].insert(i);
                S[r].insert(j);
            }
        }
    }
    Stworz(n);
    vector<bool> jest(1000001, false);
    for (int i = 1; i <= 1000000; ++i)
    {
        vector<int> rozne;
        vector<para> aktPary;
        bool ok = true;
        for (int j = i; j <= 1000000; j += i)
        {
            if (S[j].size() > 8)
            {
                ok = false;
                break;
            }
            for (int l = 0; l < pary[j].size(); ++l)
            {
                if (!jest[pary[j][l].a])
                    rozne.push_back(pary[j][l].a);
                if (!jest[pary[j][l].b])
                    rozne.push_back(pary[j][l].b);
                aktPary.push_back(pary[j][l]);
                jest[pary[j][l].a] = true;
                jest[pary[j][l].b] = true;
            }
            if (rozne.size() > 8)
            {
                ok = false;
                break;
            }
        }
        if (!ok)
        {
            for (int j = 0; j < rozne.size(); ++j)
            {
                Z[rozne[j]] = rozne[j];
                jest[rozne[j]] = false;
            }
            continue;
        }
        for (int j = 0; j < aktPary.size(); ++j)
            Union(aktPary[j].a, aktPary[j].b);
        set<int> r;
        for (int j = 0; j < rozne.size(); ++j)
            r.insert(Find(rozne[j]));
        if (rozne.size() - r.size() <= k)
        {
            cout << i;
            return 0;
        }
    }
}


#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

using namespace std;

struct para
{
    int a, b;
};

vector<int> Z;

void Stworz(int n)
{
    for (int i = 0; i < n; ++i)
        Z.push_back(i);
}

int Find(int a)
{
    if (Z[a] == a)
        return a;
    int fa = Find(Z[a]);
    Z[a] = fa;
    return fa;
}

int Union(int a, int b)
{
    int fa = Find(a);
    int fb = Find(b);
    if (fa != fb)
    {
        Z[fa] = fb;
        return 1;
    }
    return 0;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    sort(a.begin(), a.end());
    vector< vector<para> > pary(1000001);
    for (int i = 0; i < n; ++i)
    {
        for (int j = n - 1; j > i; --j)
        {
            int r = a[j] - a[i];
            if (r < n - k)
                break;
            if (pary[r].size() <= 6)
                pary[r].push_back({i, j});
        }
    }
    Stworz(n);
    vector<bool> jest(1000001, false);
    for (int i = max(1, n - k); i <= 1000000; ++i)
    {
        vector<int> rozne;
        bool ok = true;
        for (int j = i; j <= 1000000; j += i)
        {
            if (rozne.size() > 8)
            {
                ok = false;
                break;
            }
            for (int l = 0; l < pary[j].size(); ++l)
            {
                if (!jest[pary[j][l].a])
                {
                    rozne.push_back(pary[j][l].a);
                    jest[pary[j][l].a] = true;
                }
                if (!jest[pary[j][l].b])
                {
                    rozne.push_back(pary[j][l].b);
                    jest[pary[j][l].b] = true;
                }
                if (rozne.size() > 8)
                    break;
            }
            if (rozne.size() > 8)
            {
                ok = false;
                break;
            }
        }
        if (!ok)
        {
            for (int j = 0; j < rozne.size(); ++j)
            {
                Z[rozne[j]] = rozne[j];
                jest[rozne[j]] = false;
            }
            continue;
        }
        int spojne = rozne.size();
        for (int j = i; j <= 1000000; j += i)
            for (int l = 0; l < pary[j].size(); ++l)
                spojne -= Union(pary[j][l].a, pary[j][l].b);
        if (rozne.size() - spojne <= k)
        {
            cout << i;
            return 0;
        }
        for (int j = 0; j < rozne.size(); ++j)
        {
            Z[rozne[j]] = rozne[j];
            jest[rozne[j]] = false;
        }
    }
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

struct para
{
    int a, b;
};

vector<int> Z;

void Stworz(int n)
{
    for (int i = 0; i < n; ++i)
        Z.push_back(i);
}

int Find(int a)
{
    if (Z[a] == a)
        return a;
    int fa = Find(Z[a]);
    Z[a] = fa;
    return fa;
}

void Union(int a, int b)
{
    int fa = Find(a);
    int fb = Find(b);
    if (fa != fb)
        Z[fa] = fb;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    sort(a.begin(), a.end());
    vector< vector<para> > pary(1000001);
    vector< set<int> > S(1000001);
    for (int i = 0; i < n; ++i)
    {
        for (int j = i + 1; j < n; ++j)
        {
            int r = a[j] - a[i];
            if (S[r].size() <= 8)
            {
                pary[r].push_back({i, j});
                S[r].insert(i);
                S[r].insert(j);
            }
        }
    }
    Stworz(n);
    vector<bool> jest(1000001, false);
    for (int i = 1; i <= 1000000; ++i)
    {
        vector<int> rozne;
        vector<para> aktPary;
        bool ok = true;
        for (int j = i; j <= 1000000; j += i)
        {
            if (S[j].size() > 8)
            {
                ok = false;
                break;
            }
            for (int l = 0; l < pary[j].size(); ++l)
            {
                if (!jest[pary[j][l].a])
                    rozne.push_back(pary[j][l].a);
                if (!jest[pary[j][l].b])
                    rozne.push_back(pary[j][l].b);
                aktPary.push_back(pary[j][l]);
                jest[pary[j][l].a] = true;
                jest[pary[j][l].b] = true;
            }
            if (rozne.size() > 8)
            {
                ok = false;
                break;
            }
        }
        for (int j = 0; j < aktPary.size(); ++j)
            Union(aktPary[j].a, aktPary[j].b);
        set<int> r;
        for (int j = 0; j < rozne.size(); ++j)
            r.insert(Find(rozne[j]));
        if (rozne.size() - r.size() <= k)
        {
            cout << i;
            return 0;
        }
        for (int j = 0; j < rozne.size(); ++j)
        {
            Z[rozne[j]] = rozne[j];
            jest[rozne[j]] = false;
        }
    }
}


#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

using namespace std;

struct para
{
    int a, b;
};

vector<int> Z;

void Stworz(int n)
{
    for (int i = 0; i < n; ++i)
        Z.push_back(i);
}

int Find(int a)
{
    if (Z[a] == a)
        return a;
    int fa = Find(Z[a]);
    Z[a] = fa;
    return fa;
}

int Union(int a, int b)
{
    int fa = Find(a);
    int fb = Find(b);
    if (fa != fb)
    {
        Z[fa] = fb;
        return 1;
    }
    return 0;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    sort(a.begin(), a.end());
    vector< vector<para> > pary(1000001);
    vector< unordered_set<int> > S(1000001);
    for (int i = 0; i < n; ++i)
    {
        for (int j = n - 1; j > i; --i)
        {
            int r = a[j] - a[i];
            if (r < n - k)
                break;
            if (S[r].size() <= 8)
            {
                pary[r].push_back({i, j});
                S[r].insert(i);
                S[r].insert(j);
            }
        }
    }
    Stworz(n);
    vector<bool> jest(1000001, false);
    for (int i = max(1, n - k); i <= 1000000; ++i)
    {
        vector<int> rozne;
        bool ok = true;
        for (int j = i; j <= 1000000; j += i)
        {
            if (S[j].size() > 8)
            {
                ok = false;
                break;
            }
            for (int l = 0; l < pary[j].size(); ++l)
            {
                if (!jest[pary[j][l].a])
                {
                    rozne.push_back(pary[j][l].a);
                    jest[pary[j][l].a] = true;
                }
                if (!jest[pary[j][l].b])
                {
                    rozne.push_back(pary[j][l].b);
                    jest[pary[j][l].b] = true;
                }
                if (rozne.size() > 8)
                    break;
            }
            if (rozne.size() > 8)
            {
                ok = false;
                break;
            }
        }
        if (!ok)
        {
            for (int j = 0; j < rozne.size(); ++j)
            {
                Z[rozne[j]] = rozne[j];
                jest[rozne[j]] = false;
            }
            continue;
        }
        int spojne = rozne.size();
        for (int j = i; j <= 1000000; j += i)
            for (int l = 0; l < pary[j].size(); ++l)
                spojne -= Union(pary[j][l].a, pary[j][l].b);
        if (rozne.size() - spojne <= k)
        {
            cout << i;
            return 0;
        }
        for (int j = 0; j < rozne.size(); ++j)
        {
            Z[rozne[j]] = rozne[j];
            jest[rozne[j]] = false;
        }
    }
}


#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

using namespace std;

struct para
{
    int a, b;
};

vector<int> Z;

void Stworz(int n)
{
    for (int i = 0; i < n; ++i)
        Z.push_back(i);
}

int Find(int a)
{
    if (Z[a] == a)
        return a;
    int fa = Find(Z[a]);
    Z[a] = fa;
    return fa;
}

int Union(int a, int b)
{
    int fa = Find(a);
    int fb = Find(b);
    if (fa != fb)
    {
        Z[fa] = fb;
        return 1;
    }
    return 0;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    sort(a.begin(), a.end());
    vector< vector<para> > pary(1000001);
    for (int i = 0; i < n; ++i)
    {
        for (int j = n - 1; j > i; --j)
        {
            int r = a[j] - a[i];
            if (r < n - k)
                break;
            if (pary[r].size() <= 4)
                pary[r].push_back({i, j});
        }
    }
    Stworz(n);
    vector<bool> jest(1000001, false);
    for (int i = max(1, n - k); i <= 1000000; ++i)
    {
        vector<int> rozne;
        bool ok = true;
        for (int j = i; j <= 1000000; j += i)
        {
            if (pary[j].size() > 4)
            {
                ok = false;
                break;
            }
            for (int l = 0; l < pary[j].size(); ++l)
            {
                if (!jest[pary[j][l].a])
                {
                    rozne.push_back(pary[j][l].a);
                    jest[pary[j][l].a] = true;
                }
                if (!jest[pary[j][l].b])
                {
                    rozne.push_back(pary[j][l].b);
                    jest[pary[j][l].b] = true;
                }
            }
            if (rozne.size() > 8)
            {
                ok = false;
                break;
            }
        }
        if (!ok)
        {
            for (int j = 0; j < rozne.size(); ++j)
            {
                Z[rozne[j]] = rozne[j];
                jest[rozne[j]] = false;
            }
            continue;
        }
        int spojne = rozne.size();
        for (int j = i; j <= 1000000; j += i)
            for (int l = 0; l < pary[j].size(); ++l)
                spojne -= Union(pary[j][l].a, pary[j][l].b);
        if (rozne.size() - spojne <= k)
        {
            cout << i;
            return 0;
        }
        for (int j = 0; j < rozne.size(); ++j)
        {
            Z[rozne[j]] = rozne[j];
            jest[rozne[j]] = false;
        }
    }
}


#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

using namespace std;

struct para
{
    int a, b;
};

vector<int> Z;

void Stworz(int n)
{
    for (int i = 0; i < n; ++i)
        Z.push_back(i);
}

int Find(int a)
{
    if (Z[a] == a)
        return a;
    int fa = Find(Z[a]);
    Z[a] = fa;
    return fa;
}

int Union(int a, int b)
{
    int fa = Find(a);
    int fb = Find(b);
    if (fa != fb)
    {
        Z[fa] = fb;
        return 1;
    }
    return 0;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    sort(a.begin(), a.end());
    vector< vector<para> > pary(1000001);
    vector< unordered_set<int> > S(1000001);
    for (int i = 0; i < n; ++i)
    {
        for (int j = n - 1; j > i; --j)
        {
            int r = a[j] - a[i];
            if (r < n - k)
                break;
            if (S[r].size() <= 8)
            {
                pary[r].push_back({i, j});
                S[r].insert(i);
                S[r].insert(j);
            }
        }
    }
    if (n == 5000)
        return 0;
    Stworz(n);
    vector<bool> jest(1000001, false);
    for (int i = max(1, n - k); i <= 1000000; ++i)
    {
        vector<int> rozne;
        bool ok = true;
        for (int j = i; j <= 1000000; j += i)
        {
            if (S[j].size() > 8)
            {
                ok = false;
                break;
            }
            for (int l = 0; l < pary[j].size(); ++l)
            {
                if (!jest[pary[j][l].a])
                {
                    rozne.push_back(pary[j][l].a);
                    jest[pary[j][l].a] = true;
                }
                if (!jest[pary[j][l].b])
                {
                    rozne.push_back(pary[j][l].b);
                    jest[pary[j][l].b] = true;
                }
                if (rozne.size() > 8)
                    break;
            }
            if (rozne.size() > 8)
            {
                ok = false;
                break;
            }
        }
        if (!ok)
        {
            for (int j = 0; j < rozne.size(); ++j)
            {
                Z[rozne[j]] = rozne[j];
                jest[rozne[j]] = false;
            }
            continue;
        }
        int spojne = rozne.size();
        for (int j = i; j <= 1000000; j += i)
            for (int l = 0; l < pary[j].size(); ++l)
                spojne -= Union(pary[j][l].a, pary[j][l].b);
        if (rozne.size() - spojne <= k)
        {
            cout << i;
            return 0;
        }
        for (int j = 0; j < rozne.size(); ++j)
        {
            Z[rozne[j]] = rozne[j];
            jest[rozne[j]] = false;
        }
    }
}


#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

using namespace std;

struct para
{
    int a, b;
};

vector<int> Z;

void Stworz(int n)
{
    for (int i = 0; i < n; ++i)
        Z.push_back(i);
}

int Find(int a)
{
    if (Z[a] == a)
        return a;
    int fa = Find(Z[a]);
    Z[a] = fa;
    return fa;
}

int Union(int a, int b)
{
    int fa = Find(a);
    int fb = Find(b);
    if (fa != fb)
    {
        Z[fa] = fb;
        return 1;
    }
    return 0;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    sort(a.begin(), a.end());
    vector< vector<para> > pary(1000001);
    vector< unordered_set<int> > S(1000001);
    for (int i = 0; i < n; ++i)
    {
        for (int j = n - 1; j > i; --j)
        {
            int r = a[j] - a[i];
            if (r < n - k)
                break;
            if (S[r].size() <= 8)
            {
                pary[r].push_back({i, j});
                S[r].insert(i);
                S[r].insert(j);
            }
        }
    }
    Stworz(n);
    vector<bool> jest(1000001, false);
    for (int i = max(1, n - k); i <= 1000000; ++i)
    {
        vector<int> rozne;
        bool ok = true;
        for (int j = i; j <= 1000000; j += i)
        {
            if (S[j].size() > 8)
            {
                ok = false;
                break;
            }
            for (int l = 0; l < pary[j].size(); ++l)
            {
                if (!jest[pary[j][l].a])
                {
                    rozne.push_back(pary[j][l].a);
                    jest[pary[j][l].a] = true;
                }
                if (!jest[pary[j][l].b])
                {
                    rozne.push_back(pary[j][l].b);
                    jest[pary[j][l].b] = true;
                }
                if (rozne.size() > 8)
                    break;
            }
            if (rozne.size() > 8)
            {
                ok = false;
                break;
            }
        }
        if (!ok)
        {
            for (int j = 0; j < rozne.size(); ++j)
            {
                Z[rozne[j]] = rozne[j];
                jest[rozne[j]] = false;
            }
            continue;
        }
        int spojne = rozne.size();
        for (int j = i; j <= 1000000; j += i)
            for (int l = 0; l < pary[j].size(); ++l)
                spojne -= Union(pary[j][l].a, pary[j][l].b);
        if (rozne.size() - spojne <= k)
        {
            cout << i;
            return 0;
        }
        for (int j = 0; j < rozne.size(); ++j)
        {
            Z[rozne[j]] = rozne[j];
            jest[rozne[j]] = false;
        }
    }
}


#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

using namespace std;

struct para
{
    int a, b;
};

vector<int> Z;

void Stworz(int n)
{
    for (int i = 0; i < n; ++i)
        Z.push_back(i);
}

int Find(int a)
{
    if (Z[a] == a)
        return a;
    int fa = Find(Z[a]);
    Z[a] = fa;
    return fa;
}

int Union(int a, int b)
{
    int fa = Find(a);
    int fb = Find(b);
    if (fa != fb)
    {
        Z[fa] = fb;
        return 1;
    }
    return 0;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    sort(a.begin(), a.end());
    vector< vector<para> > pary(1000001);
    vector< unordered_set<int> > S(1000001);
    for (int i = 0; i < n; ++i)
    {
        for (int j = i + 1; j < n; ++j)
        {
            int r = a[j] - a[i];
            if (S[r].size() <= 8)
            {
                pary[r].push_back({i, j});
                S[r].insert(i);
                S[r].insert(j);
            }
        }
    }
    Stworz(n);
    vector<bool> jest(1000001, false);
    for (int i = 1; i <= 1000000; ++i)
    {
        vector<int> rozne;
        vector<para> aktPary;
        bool ok = true;
        for (int j = i; j <= 1000000; j += i)
        {
            if (S[j].size() > 8)
            {
                ok = false;
                break;
            }
            for (int l = 0; l < pary[j].size(); ++l)
            {
                if (!jest[pary[j][l].a])
                    rozne.push_back(pary[j][l].a);
                if (!jest[pary[j][l].b])
                    rozne.push_back(pary[j][l].b);
                aktPary.push_back(pary[j][l]);
                jest[pary[j][l].a] = true;
                jest[pary[j][l].b] = true;
            }
            if (rozne.size() > 8)
            {
                ok = false;
                break;
            }
        }
        if (!ok)
        {
            for (int j = 0; j < rozne.size(); ++j)
            {
                Z[rozne[j]] = rozne[j];
                jest[rozne[j]] = false;
            }
            continue;
        }
        int spojne = rozne.size();
        for (int j = 0; j < aktPary.size(); ++j)
            spojne -= Union(aktPary[j].a, aktPary[j].b);
        if (rozne.size() - spojne <= k)
        {
            cout << i;
            return 0;
        }
        for (int j = 0; j < rozne.size(); ++j)
        {
            Z[rozne[j]] = rozne[j];
            jest[rozne[j]] = false;
        }
    }
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

#define rep(i, a) for(int i = 0; i < (a); ++i)
#define repp(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

int A[5005];
int haha[5005];
int p[5005];
int n,k;
vector<vector<pii> > vec;
vector<int> cache;

int find(int u){
	if(p[u] == u)return u;
	p[u] = find(p[u]);
	return p[u];
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	cin>>n>>k;
	vec.resize(1e6+5);
	repp(i,1,n+1) p[i] = i;
	repp(i,1,n+1){
		cin>>A[i];
	}
	sort(A+1, A+n+1);
	repp(i, 1, n+1){
		repp(j, i+1, n+1){
			vec[A[j]-A[i]].pb(mp(i,j));
		}
	}
	repp(x,1,1e6+5){
		cache.clear();
		int cnt = 0;
		for(int i=x;i<=1e6+1;i+=x){
			trav(tp, vec[i]){
				int a = tp.fst, b = tp.snd;
				int pa = find(a), pb = find(b);
				if(pa != pb){
					cnt++;
					cache.pb(a);
					p[pa] = pb;
				}
				if(cnt>k)break;
			}
			if(cnt>k)break;
		}
		trav(a, cache)p[a] = a;
		if(cnt<=k){
			cout<<x<<endl;
			return 0;
		}
	}
	return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

#define rep(i, a) for(int i = 0; i < (a); ++i)
#define repp(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

int A[5005];
int n,k;
int F[1000006];
int group[1000006];

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	cin>>n>>k;
	repp(i,1,n+1){
		cin>>A[i];
	}
	sort(A+1, A+n+1);
	repp(i, 1, n+1){
		repp(j, i+1, n+1){
			F[A[j]-A[i]]++;
		}
	}
	repp(x,1,1e6+5){
		int cnt = 0;
		for(int i=x;i<=1e6+1;i+=x){
			cnt+=F[i];
		}
		if(cnt>(k*(k+1))/2)continue;
		int tmp = 0;
		repp(i,1,n+1){
			if(group[A[i]%i] == i)tmp++;
			group[A[i]%i] = i;
		}
		if(tmp<=k){
			cout<<x<<endl;
			return 0;
		}
	}
	return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

#define rep(i, a) for(int i = 0; i < (a); ++i)
#define repp(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

int a[5005];
int haha[5005];
int p[5005];
int n,k;
vector<vector<pii> > vec;
vector<int> cache;

int find(int u){
	if(p[u] == u)return u;
	return find(p[u]);
}

void pre(){
	cin>>n>>k;
	vec.resize(1e6+5);
	repp(i,1,n+1) p[i] = i;
	repp(i,1,n+1){
		cin>>a[i];
	}
	sort(a+1, a+n+1);
	repp(i, 1, n+1){
		repp(j, i+1, n+1){
			vec[a[j]-a[i]].pb(mp(i,j));
		}
	}
}

bool solve(int d){
	cache.clear();
	int cnt = 0;
	for(int i=1;d*i<=1e6+1;i++){
		trav(tp, vec[d*i]){
			int a = tp.fst, b = tp.snd;
			int pa = find(a), pb = find(b);
			if(pa != pb){
				cnt++;
				cache.pb(a);
				cache.pb(b);
				p[pa] = pb;
			}
			if(cnt>k)break;
		}
		if(cnt>k)break;
	}
	trav(a, cache)p[a] = a;
	return (cnt<=k);
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	pre();
	repp(i,1,1e6+5){
		if(solve(i)){
			cout<<i<<endl;
			return 0;
		}
	}
	return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

#define rep(i, a) for(int i = 0; i < (a); ++i)
#define repp(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

int A[5005];
int n,k;
int F[5005];
int group[1000006];

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	cin>>n>>k;
	repp(i,1,n+1){
		cin>>A[i];
	}
	sort(A+1, A+n+1);
	repp(i, 1, n+1){
		repp(j, i+1, n+1){
			F[A[j]-A[i]]++;
		}
	}
	repp(x,1,1e6+5){
		int cnt = 0;
		for(int i=x;i<=1e6+1;i+=x){
			cnt+=F[i];
		}
		if(cnt>(k*(k+1))/2)continue;
		int tmp = 0;
		repp(i,1,n+1){
			if(group[A[i]%i] == i)tmp++;
			group[A[i]%i] = i;
		}
		if(tmp<=k){
			cout<<x<<endl;
			return 0;
		}
	}
	return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

#define rep(i, a) for(int i = 0; i < (a); ++i)
#define repp(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

int a[5005];
int haha[5005];
int p[5005];
int n,k;
vector<vector<pii> > vec;
vector<int> cache;

int find(int u){
	if(p[u] == u)return u;
	p[u] = find(p[u]);
	return p[u];
}

void pre(){
	cin>>n>>k;
	vec.resize(1e6+5);
	repp(i,1,n+1) p[i] = i;
	repp(i,1,n+1){
		cin>>a[i];
	}
	sort(a+1, a+n+1);
	repp(i, 1, n+1){
		repp(j, i+1, n+1){
			vec[a[j]-a[i]].pb(mp(i,j));
		}
	}
}

bool solve(int d){
	cache.clear();
	int cnt = 0;
	for(int i=1;d*i<=1e6+1;i++){
		trav(tp, vec[d*i]){
			int a = tp.fst, b = tp.snd;
			int pa = find(a), pb = find(b);
			if(pa != pb){
				cnt++;
				cache.pb(a);
				cache.pb(b);
				p[pa] = pb;
			}
			if(cnt>k)break;
		}
		if(cnt>k)break;
	}
	trav(a, cache)p[a] = a;
	return (cnt<=k);
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	pre();
	repp(i,1,1e6+5){
		if(solve(i)){
			cout<<i<<endl;
			return 0;
		}
	}
	return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

#define rep(i, a) for(int i = 0; i < (a); ++i)
#define repp(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

int A[5005];
int n,k;
int F[1000006];
int group[1000006];

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	cin>>n>>k;
	repp(i,1,n+1){
		cin>>A[i];
	}
	sort(A+1, A+n+1);
	repp(i, 1, n+1){
		repp(j, i+1, n+1){
			F[A[j]-A[i]]++;
		}
	}
	repp(x,1,1e6+5){
		int cnt = 0;
		for(int i=x;i<=1e6+1;i+=x){
			cnt+=F[i];
		}
		if(cnt>(k*(k+1))/2)continue;
		int tmp = 0;
		repp(i,1,n+1){
			if(group[A[i]%x] == x)tmp++;
			group[A[i]%x] = x;
		}
		if(tmp<=k){
			cout<<x<<endl;
			return 0;
		}
	}
	return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

#define rep(i, a) for(int i = 0; i < (a); ++i)
#define repp(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

int A[5005];
int haha[5005];
int p[5005];
int n,k;
vector<vector<pii> > vec;
vector<int> cache;

int find(int u){
	if(p[u] == u)return u;
	p[u] = find(p[u]);
	return p[u];
}

void pre(){
	cin>>n>>k;
	vec.resize(1e6+5);
	repp(i,1,n+1) p[i] = i;
	repp(i,1,n+1){
		cin>>A[i];
	}
	sort(A+1, A+n+1);
	repp(i, 1, n+1){
		repp(j, i+1, n+1){
			vec[A[j]-A[i]].pb(mp(i,j));
		}
	}
}

bool solve(int d){
	cache.clear();
	int cnt = 0;
	for(int i=0;d*i<=1e6+1;i++){
		trav(tp, vec[d*i]){
			int a = tp.fst, b = tp.snd;
			int pa = find(a), pb = find(b);
			if(pa != pb){
				cnt++;
				cache.pb(a);
				cache.pb(b);
				p[pa] = pb;
			}
			if(cnt>k)break;
		}
		if(cnt>k)break;
	}
	trav(a, cache)p[a] = a;
	return (cnt<=k);
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	pre();
	repp(i,1,1e6+5){
		if(solve(i)){
			cout<<i<<endl;
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<int,int> ii;

const int N = 1e6 + 5;

int n,k,a[N],cnt[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  // freopen("minimod.gir","r",stdin);
  // freopen("minimod.cik","w",stdout);
  cin >> n >> k;
  for (int i = 1 ; i <= n ; i++)
    cin >> a[i];

  for (int i = 1 ; i <= n ; i++)
    for (int j = i + 1 ; j <= n ; j++)
      cnt[abs(a[i] - a[j])]++;

  for (int i = 1 ; i < N ; i++) {
    for (int j = i + i ; j < N ; j += i)
      cnt[i] += cnt[j];
    if (cnt[i] <= k) {
      cout << i << endl;
      return 0;
    }
  }

}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<int,int> ii;

const int N = 1e6 + 5;

int n,k,a[N],cnt[N],h[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  // freopen("minimod.gir","r",stdin);
  // freopen("minimod.cik","w",stdout);
  cin >> n >> k;
  for (int i = 1 ; i <= n ; i++)
    cin >> a[i];

  for (int i = 1 ; i <= n ; i++)
    for (int j = i + 1 ; j <= n ; j++)
      cnt[abs(a[i] - a[j])]++;

  for (int i = 1 ; i < N ; i++) {
    for (int j = i + i ; j < N ; j += i)
      cnt[i] += cnt[j];
    if (k * (k + 1) / 2 < cnt[i])
      continue;
    int res = 0;
    for(int j = 1 ; j <= n ; j++) {
      res += (h[a[j] % i] == i);
      h[a[j] % i] = i;
    }
    if(res <= k) {
      cout << i << endl;
      return 0;
    }
  }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
int const M=2e5+10;
vector<int>di[M*10],ch;
int mark[M*10],a[M];
int cnt[M*10],k;
void check(int tmp,int ind)
{
	if(cnt[tmp]>k || mark[tmp]==ind)return;
	for(int i=0;i<di[tmp].size();i++)
	{
		if(cnt[di[tmp][i]]>5)continue;
		if(mark[di[tmp][i]]!=ind)cnt[di[tmp][i]]++,mark[di[tmp][i]]=ind;
	}
}
int main()
{
	int n;
	cin>>n>>k;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=1e6;i++)
	{
		for(int j=1;j*i<=1e6;j++)di[j*i].pb(i);
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			int tmp=max(a[i],a[j])-min(a[i],a[j]);
			check(tmp,i);
		}
	}
	for(int i=1;i<=1e6;i++)
	{
		if(cnt[i]<=k)return cout<<i,0;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
int const M=2e5+10;
vector<int>ch[M*10];
int mark[M*10],a[M],t[M*10];
vector<int>di[M*10];
int cnt[M*10],k2,bad[M*10];
void check(int tmp,int ind)
{
	if(ch[tmp].size()>k2 || mark[tmp]==ind)return;
	mark[tmp]=ind;
	ch[tmp].pb(ind);
}
int main()
{
	int n;
	cin>>n>>k2;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=1e6;i++)t[i]=i;
	for(int i=2;i<=1e3;i++)
	{
		if(!di[i].size()){
			for(int j=i;j<=1e6;j+=i)
			{
				di[j].pb(i);
				while(t[j]%i==0)t[j]/=i;
			}
		}
	}
	for(int i=1;i<=1e6;i++)
	{
		if(t[i]!=1)di[i].pb(t[i]);
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			int tmp=a[j]-a[i];
			check(tmp,i);
		}
	}
	for(int i=1;i<=1e6;i++)mark[i]=0;
	int ans;
	for(int i=1e6;i>=1;i--)
	{
		if(ch[i].size()>k2)bad[i]=1;
		for(int j=i+i;j<=1e6;j+=i)bad[i]|=bad[j];
		if(bad[i])continue;
		if(!bad[i])ans=i;
		if(!ch[i].size())continue;
		for(int j=0;j<di[i].size();j++){
			int hlp=i/di[i][j];
			if(ch[hlp].size()<=k2)
			{
				for(int k=0;k<ch[hlp].size();k++)mark[ch[hlp][k]]=i*10+j+1;
				for(int k=0;k<ch[i].size();k++)
				{
					if(ch[hlp].size()>k2)break;
					if(mark[ch[i][k]]!=i*10+j+1)ch[hlp].pb(ch[i][k]);
				}
			}
		}
	}
	cout<<ans;
}


#include<cstdio>
#include<iostream>
using namespace std;
int const MAX=1000*100+10;
int a[MAX];
int nu[6000+10][6000+10];
int main(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=6000;j++){
			if(a[i]==0){
				nu[j][0]++;
			}
			else{
				nu[j][a[i]%j]++;
			}
		}
	}
	for(int i=1;i<=6000;i++){
		int cnt=0;
		for(int j=0;j<=i-1;j++){
			if(nu[i][j]>1){
				cnt+=nu[i][j]-1;
			}
		}
		if(cnt<=m){
			cout<<i;
			return 0;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
int const M=2e5+10;
vector<int>ch[M*10];
int mark[M*10],a[M],t[M*10];
vector<int>di[M*10],pr;
int cnt[M*10],k2,bad[M*10];
void check(int tmp,int ind)
{
	if(ch[tmp].size()>k2 || mark[tmp]==ind)return;
	mark[tmp]=ind;
	ch[tmp].pb(ind);
}
int main()
{
	int n;
	cin>>n>>k2;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=1e6;i++)t[i]=i;
	for(int i=2;i<=1e3;i++)
	{
		if(!di[i].size()){
			pr.pb(i);
			for(int j=i;j<=1e6;j+=i)
			{
				di[j].pb(i);
				while(t[j]%i==0)t[j]/=i;
			}
		}
	}
	for(int i=1;i<=1e6;i++)
	{
		if(di[i].size()==0 && i>1e3)pr.pb(i);
		if(t[i]!=1)di[i].pb(t[i]);
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			int tmp=a[j]-a[i];
			check(tmp,i);
		}
	}
	for(int i=1;i<=1e6;i++)mark[i]=0;
	int ans;
	for(int i=1e6;i>=1;i--)
	{
		//if(ch[i].size()>k2)bad[i]=1;
		for(int j=0;j<pr.size() && i*pr[j]<=1e6;j++)bad[i]|=bad[pr[j]*i];
		if(bad[i])continue;
		for(int j=0;j<pr.size() && i*pr[j]<=1e6;j++)
		{
			if(ch[i].size()>k2)break;
			int tmp=pr[j]*i;
			for(int k=0;k<ch[tmp].size();k++){
				if(mark[ch[tmp][k]]!=i)ch[i].pb(ch[tmp][k]),mark[ch[tmp][k]]=i;
			}
		}
		if(ch[i].size()>k2)bad[i]=1;
		if(!bad[i])ans=i;
		if(!ch[i].size())continue;
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
int const M=2e5+10;
vector<int>ch[M*10];
int mark[M*10],a[M],t[M*10];
vector<int>di[M*10];
int cnt[M*10],k,bad[M*10];
void check(int tmp,int ind)
{
	if(ch[tmp].size()>k || mark[tmp]==ind)return;
	mark[tmp]=ind;
	ch[tmp].pb(ind);
}
int main()
{
	int n;
	cin>>n>>k;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=1e6;i++)t[i]=i;
	for(int i=2;i<=1e3;i++)
	{
		if(!di[i].size()){
			for(int j=i;j<=1e6;j+=i)
			{
				di[j].pb(i);
				while(t[j]%i==0)t[j]/=i;
			}
		}
	}
	for(int i=1;i<=1e6;i++)
	{
		if(t[i]!=1)di[i].pb(t[i]);
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			int tmp=max(a[i],a[j])-min(a[i],a[j]);
			check(tmp,i);
		}
	}
	for(int i=1;i<=1e6;i++)mark[i]=0;
	int ans;
	for(int i=1e6;i>=1;i--)
	{
		if(ch[i].size()>k)bad[i]=1;
		if(!bad[i])ans=i;
		if(!ch[i].size() && !bad[i])continue;
		for(int j=0;j<di[i].size();j++){
			if(bad[i]){
				bad[i/di[i][j]]=1;
				continue;
			}
			int hlp=i/di[i][j];
			//if(!ch[i].size)continue;
			if(ch[hlp].size()<=k)
			{
				for(int k=0;k<ch[hlp].size();k++)mark[ch[hlp][k]]=i*10+j+1;
				for(int k=0;k<ch[i].size();k++)
				{
					if(mark[ch[i][k]]!=i*10+j+1)ch[hlp].pb(ch[i][k]),mark[ch[i][k]]=i*10+j+1;
				}
			}
		}
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
int const M=2e5+10;
vector<int>ch[M*10];
int mark[M*10],a[M],t[M*10];
vector<int>di[M*10];
int cnt[M*10],k2,bad[M*10];
void check(int tmp,int ind)
{
	if(ch[tmp].size()>k2 || mark[tmp]==ind)return;
	mark[tmp]=ind;
	ch[tmp].pb(ind);
}
int main()
{
	int n;
	cin>>n>>k2;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=1e6;i++)t[i]=i;
	for(int i=2;i<=1e3;i++)
	{
		if(!di[i].size()){
			for(int j=i;j<=1e6;j+=i)
			{
				di[j].pb(i);
				while(t[j]%i==0)t[j]/=i;
			}
		}
	}
	for(int i=1;i<=1e6;i++)
	{
		if(t[i]!=1)di[i].pb(t[i]);
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			int tmp=a[j]-a[i];
			check(tmp,i);
		}
	}
	for(int i=1;i<=1e6;i++)mark[i]=0;
	int ans;
	for(int i=1e6;i>=1;i--)
	{
		if(ch[i].size()>k2)bad[i]=1;
		if(!bad[i])ans=i;
		if(!ch[i].size() && !bad[i])continue;
		for(int j=0;j<di[i].size();j++){
			if(bad[i]){
				bad[i/di[i][j]]=1;
				continue;
			}
			int hlp=i/di[i][j];
			if(ch[hlp].size()<=k2)
			{
				for(int k=0;k<ch[hlp].size();k++)mark[ch[hlp][k]]=i*10+j+1;
				for(int k=0;k<ch[i].size();k++)
				{
					if(ch[hlp].size()>k2)break;
					if(mark[ch[i][k]]!=i*10+j+1)ch[hlp].pb(ch[i][k]);
				}
			}
		}
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
int const M=2e5+10;
vector<int>ch[M*10];
int mark[M*10],a[M],t[M*10];
vector<int>di[M*10],pr;
int cnt[M*10],k2,bad[M*10];
void check(int tmp,int ind)
{
	if(ch[tmp].size()>k2 || mark[tmp]==ind)return;
	mark[tmp]=ind;
	ch[tmp].pb(ind);
}
int main()
{
	int n;
	cin>>n>>k2;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=1e6;i++)t[i]=i;
	for(int i=2;i<=1e3;i++)
	{
		if(!di[i].size()){
			pr.pb(i);
			for(int j=i;j<=1e6;j+=i)
			{
				di[j].pb(i);
				while(t[j]%i==0)t[j]/=i;
			}
		}
	}
	for(int i=1;i<=1e6;i++)
	{
		if(di[i].size()==0 && i>1e3)pr.pb(i);
		if(t[i]!=1)di[i].pb(t[i]);
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			int tmp=a[j]-a[i];
			check(tmp,i);
		}
	}
	for(int i=1;i<=1e6;i++)mark[i]=0;
	int ans;
	for(int i=1e6;i>=1;i--)
	{
		if(ch[i].size()>k2)bad[i]=1;
		for(int j=0;j<pr.size() && i*pr[j]<=1e6;j++)bad[i]|=bad[pr[j]*i];
		if(bad[i])continue;
		if(!bad[i])ans=i;
		if(!ch[i].size())continue;
		for(int j=0;j<di[i].size();j++){
			int hlp=i/di[i][j];
			if(ch[hlp].size()<=k2)
			{
				for(int k=0;k<ch[hlp].size();k++)mark[ch[hlp][k]]=i*10+j+1;
				for(int k=0;k<ch[i].size();k++)
				{
					if(ch[hlp].size()>k2)break;
					if(mark[ch[i][k]]!=i*10+j+1)ch[hlp].pb(ch[i][k]);
				}
			}
		}
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
int const M=2e5+10;
vector<int>ch[M*10];
int mark[M*10],a[M],t[M*10];
vector<int>di[M*10];
int cnt[M*10],k,bad[M*10];
void check(int tmp,int ind)
{
	if(ch[tmp].size()>k || mark[tmp]==ind)return;
	mark[tmp]=ind;
	ch[tmp].pb(ind);
}
int main()
{
	int n;
	cin>>n>>k;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=1e6;i++)t[i]=i;
	for(int i=2;i<=1e3;i++)
	{
		if(!di[i].size()){
			for(int j=i;j<=1e6;j+=i)
			{
				di[j].pb(i);
				while(t[j]%i==0)t[j]/=i;
			}
		}
	}
	for(int i=1;i<=1e6;i++)
	{
		if(t[i]!=1)di[i].pb(t[i]);
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			int tmp=max(a[i],a[j])-min(a[i],a[j]);
			check(tmp,i);
		}
	}
	for(int i=1;i<=1e6;i++)mark[i]=0;
	int ans;
	for(int i=1e6;i>=1;i--)
	{
		if(ch[i].size()>k)bad[i]=1;
		if(!bad[i])ans=i;
		if(!ch[i].size() && !bad[i])continue;
		for(int j=0;j<di[i].size();j++){
			if(bad[i]){
				bad[i/di[i][j]]=1;
				continue;
			}
			int hlp=i/di[i][j];
			if(ch[hlp].size()<=k)
			{
				for(int k=0;k<ch[hlp].size();k++)mark[ch[hlp][k]]=i*10+j+1;
				for(int k=0;k<ch[i].size();k++)
				{
					if(ch[hlp].size()>k)break;
					if(mark[ch[i][k]]!=i*10+j+1)ch[hlp].pb(ch[i][k]),mark[ch[i][k]]=i*10+j+1;
				}
			}
		}
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
int const M=2e5+10;
int ch[M*10][5],pnt[M*10];
int mark[M*10],a[M],t[M*10];
vector<int>di[M*10],pr;
int cnt[M*10],k2,bad[M*10];
void check(int tmp,int ind)
{
	if(pnt[tmp]>k2 || mark[tmp]==ind)return;
	mark[tmp]=ind;
	ch[tmp][pnt[tmp]++]=ind;
}
int main()
{
	int n;
	cin>>n>>k2;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=1e6;i++)t[i]=i;
	for(int i=2;i<=1e3;i++)
	{
		if(!di[i].size()){
			pr.pb(i);
			for(int j=i;j<=1e6;j+=i)
			{
				di[j].pb(i);
				while(t[j]%i==0)t[j]/=i;
			}
		}
	}
	for(int i=1;i<=1e6;i++)
	{
		if(di[i].size()==0 && i>1e3)pr.pb(i);
		if(t[i]!=1)di[i].pb(t[i]);
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			int tmp=a[j]-a[i];
			check(tmp,i);
		}
	}
	for(int i=1;i<=1e6;i++)mark[i]=0;
	int ans;
	for(int i=1e6;i>=1;i--)
	{
		//if(ch[i].size()>k2)bad[i]=1;
		for(int j=0;j<pr.size() && i*pr[j]<=1e6;j++)bad[i]|=bad[pr[j]*i];
		if(bad[i])continue;
		for(int j=0;j<pr.size() && i*pr[j]<=1e6;j++)
		{
			if(pnt[i]>k2)break;
			int tmp=pr[j]*i;
			for(int k=0;k<pnt[tmp];k++){
				if(mark[ch[tmp][k]]!=i)ch[i][pnt[i]++]=ch[tmp][k],mark[ch[tmp][k]]=i;
			}
		}
		if(pnt[i]>k2)bad[i]=1;
		if(!bad[i])ans=i;
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
int const M=2e5+10;
vector<int>ch[M*10];
int mark[M*10],a[M],t[M*10];
vector<int>di[M*10],pr;
int cnt[M*10],k2,bad[M*10];
void check(int tmp,int ind)
{
	if(ch[tmp].size()>k2 || mark[tmp]==ind)return;
	mark[tmp]=ind;
	ch[tmp].pb(ind);
}
int main()
{
	int n;
	cin>>n>>k2;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=1e6;i++)t[i]=i;
	for(int i=2;i<=1e3;i++)
	{
		if(!di[i].size()){
			pr.pb(i);
			for(int j=i;j<=1e6;j+=i)
			{
				di[j].pb(i);
				while(t[j]%i==0)t[j]/=i;
			}
		}
	}
	for(int i=1;i<=1e6;i++)
	{
		if(pr.size()==1 && i>1e3)pr.pb(i);
		if(t[i]!=1)di[i].pb(t[i]);
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			int tmp=a[j]-a[i];
			check(tmp,i);
		}
	}
	for(int i=1;i<=1e6;i++)mark[i]=0;
	int ans;
	for(int i=1e6;i>=1;i--)
	{
		if(ch[i].size()>k2)bad[i]=1;
		for(int j=0;j<pr.size() && i*pr[j]<=1e6;j++)bad[i]|=bad[pr[j]*i];
		if(bad[i])continue;
		if(!bad[i])ans=i;
		if(!ch[i].size())continue;
		for(int j=0;j<di[i].size();j++){
			int hlp=i/di[i][j];
			if(ch[hlp].size()<=k2)
			{
				for(int k=0;k<ch[hlp].size();k++)mark[ch[hlp][k]]=i*10+j+1;
				for(int k=0;k<ch[i].size();k++)
				{
					if(ch[hlp].size()>k2)break;
					if(mark[ch[i][k]]!=i*10+j+1)ch[hlp].pb(ch[i][k]);
				}
			}
		}
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
int const M=2e5+10;
vector<int>ch[M*10];
int mark[M*10],a[M],t[M*10];
vector<int>di[M*10],pr;
int cnt[M*10],k2,bad[M*10];
void check(int tmp,int ind)
{
	if(ch[tmp].size()>k2 || mark[tmp]==ind)return;
	mark[tmp]=ind;
	ch[tmp].pb(ind);
}
int main()
{
	int n;
	cin>>n>>k2;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=1e6;i++)t[i]=i;
	for(int i=2;i<=1e3;i++)
	{
		if(!di[i].size()){
			pr.pb(i);
			for(int j=i;j<=1e6;j+=i)
			{
				di[j].pb(i);
				while(t[j]%i==0)t[j]/=i;
			}
		}
	}
	for(int i=1;i<=1e6;i++)
	{
		if(t[i]!=1)di[i].pb(t[i]);
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			int tmp=a[j]-a[i];
			check(tmp,i);
		}
	}
	for(int i=1;i<=1e6;i++)mark[i]=0;
	int ans;
	for(int i=1e6;i>=1;i--)
	{
		if(ch[i].size()>k2)bad[i]=1;
		for(int j=0;j<pr.size() && i*pr[j]<=1e6;j++)bad[i]|=bad[pr[j]*i];
		if(bad[i])continue;
		if(!bad[i])ans=i;
		if(!ch[i].size())continue;
		for(int j=0;j<di[i].size();j++){
			int hlp=i/di[i][j];
			if(ch[hlp].size()<=k2)
			{
				for(int k=0;k<ch[hlp].size();k++)mark[ch[hlp][k]]=i*10+j+1;
				for(int k=0;k<ch[i].size();k++)
				{
					if(ch[hlp].size()>k2)break;
					if(mark[ch[i][k]]!=i*10+j+1)ch[hlp].pb(ch[i][k]);
				}
			}
		}
	}
	cout<<ans;
}


using System;
using System.IO;
using System.Text;

namespace Sereja_and_Brackets
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            string s = reader.ReadLine();

            var nn100 = new Tuple<int, int, int>[s.Length/100];
            var nn10000 = new Tuple<int, int, int>[s.Length/10000];

            for (int i = 0; i < nn100.Length; i++)
            {
                int max = 100*(i + 1);
                int open = 0;
                int close = 0;
                int good = 0;

                for (int j = max - 100; j < max; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }
                nn100[i] = new Tuple<int, int, int>(open, close, good);
            }
            for (int i = 0; i < nn10000.Length; i++)
            {
                int max = 100*(i + 1);
                int open = 0;
                int close = 0;
                int good = 0;

                for (int j = max - 100; j < max; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }
                nn10000[i] = new Tuple<int, int, int>(open, close, good);
            }

            int m = Next();
            for (int i = 0; i < m; i++)
            {
                int l = Next() - 1;
                int r = Next() - 1;

                int l100 = l/100;
                int r100 = r/100;

                int l10000 = l/10000;
                int r10000 = r/10000;

                int open = 0;
                int close = 0;
                int good = 0;

                int max100 = Math.Min(l10000 + 1, r10000)*100;
                int maxl = Math.Min(l100 + 1, max100)*100;
                for (int j = l; j < maxl; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }

                for (int j = l100 + 1; j < max100; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }

                for (int j = l10000 + 1; j < r10000; j++)
                {
                    good += nn10000[j].Item3;
                    int closed = Math.Min(open, nn10000[j].Item2);
                    good += closed;

                    open = open + nn10000[j].Item1 - closed;
                    close = close + nn10000[j].Item2 - closed;
                }

                for (int j = r10000*100; j < r100; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }

                for (int j = Math.Max(l, r100*100); j <= r; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }

                writer.WriteLine(2*good);
            }

            writer.Flush();
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;
using System.IO;
using System.Text;

namespace Sereja_and_Brackets
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            string s = reader.ReadLine();

            var nn100 = new Tuple<int, int, int>[s.Length/100];
            var nn10000 = new Tuple<int, int, int>[s.Length/10000];

            for (int i = 0; i < nn100.Length; i++)
            {
                int max = 100*(i + 1);
                int open = 0;
                int close = 0;
                int good = 0;

                for (int j = max - 100; j < max; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }
                nn100[i] = new Tuple<int, int, int>(open, close, good);
            }
            for (int i = 0; i < nn10000.Length; i++)
            {
                int max = 100*(i + 1);
                int open = 0;
                int close = 0;
                int good = 0;

                for (int j = max - 100; j < max; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }
                nn10000[i] = new Tuple<int, int, int>(open, close, good);
            }

            int m = Next();
            for (int i = 0; i < m; i++)
            {
                int l = Next() - 1;
                int r = Next() - 1;

                int l100 = l/100;
                int r100 = r/100;

                int l10000 = l/10000;
                int r10000 = r/10000;

                int open = 0;
                int close = 0;
                int good = 0;

                int max100 = Math.Min(l10000 + 1, r10000)*100;
                int maxl = Math.Min(l100 + 1, max100)*100;

                for (int j = l; j < maxl; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }

                for (int j = l100 + 1; j < max100; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }

                for (int j = l10000 + 1; j < r10000; j++)
                {
                    good += nn10000[j].Item3;
                    int closed = Math.Min(open, nn10000[j].Item2);
                    good += closed;

                    open = open + nn10000[j].Item1 - closed;
                    close = close + nn10000[j].Item2 - closed;
                }

                for (int j = Math.Max(r10000 * 100, max100); j < r100; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }

                for (int j = Math.Max(l, r100*100); j <= r; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }

                writer.WriteLine(2*good);
            }

            writer.Flush();
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;
using System.IO;
using System.Text;

namespace Sereja_and_Brackets
{
    internal class MinSegment
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static int[] open;
        private static int n;

        private static void Main(string[] args)
        {
            string s = reader.ReadLine();
            n = s.Length + 1;

            int op = 0, go = 0;
            var good = new int[n];
            open = new int[n*2];
            for (int i = 1; i < n; i++)
            {
                if (s[i - 1] == '(')
                {
                    op++;
                }
                else
                {
                    if (op > 0)
                    {
                        op--;
                        go++;
                    }
                }
                open[i + n] = op;
                good[i] = go;
            }

            for (int i = n - 1; i > 0; --i) open[i] = Math.Min(open[i << 1], open[i << 1 | 1]);

            int m = Next();
            for (int i = 0; i < m; i++)
            {
                int l = Next();
                int r = Next();

                writer.WriteLine(2*(good[r] - good[l - 1] - Math.Max(0, (open[n + l - 1] - Query(l, r + 1)))));
            }

            writer.Flush();
        }

        private static int Query(int l, int r)
        {
            int res = int.MaxValue;
            for (l += n, r += n; l < r; l >>= 1, r >>= 1)
            {
                if ((l & 1) == 1) res = Math.Min(res, open[l++]);
                if ((r & 1) == 1) res = Math.Min(res, open[--r]);
            }
            return res;
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;
using System.IO;
using System.Text;

namespace Sereja_and_Brackets
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            string s = reader.ReadLine();

            var nn100 = new Tuple<int, int, int>[s.Length/100];
            var nn10000 = new Tuple<int, int, int>[s.Length/10000];

            for (int i = 0; i < nn100.Length; i++)
            {
                int max = 100*(i + 1);
                int open = 0;
                int close = 0;
                int good = 0;

                for (int j = max - 100; j < max; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }
                nn100[i] = new Tuple<int, int, int>(open, close, good);
            }
            for (int i = 0; i < nn10000.Length; i++)
            {
                int max = 100*(i + 1);
                int open = 0;
                int close = 0;
                int good = 0;

                for (int j = max - 100; j < max; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }
                nn10000[i] = new Tuple<int, int, int>(open, close, good);
            }

            int m = Next();

            for (int i = 0; i < m; i++)
            {
                int l = Next() - 1;
                int r = Next() - 1;

                int l100 = l/100;
                int r100 = r/100;
                if (l100 == r100)
                {
                    int open = 0;
                    int good = 0;

                    for (int j = l; j <= r; j++)
                    {
                        if (s[j] == '(')
                            open++;
                        else
                        {
                            if (open > 0)
                            {
                                good++;
                                open--;
                            }
                        }
                    }
                    writer.WriteLine(2*good);
                }
                else
                {
                    int l10000 = l/10000;
                    int r10000 = r/10000;

                    int open = 0;
                    int close = 0;
                    int good = 0;

                    if (l100*100 != l)
                    {
                        int maxl = (l100 + 1)*100;
                        for (int j = l; j < maxl; j++)
                        {
                            if (s[j] == '(')
                                open++;
                            else
                            {
                                if (open > 0)
                                {
                                    good++;
                                    open--;
                                }
                                else
                                {
                                    close++;
                                }
                            }
                        }
                    }

                    int max100 = (l10000 + 1)*100;
                    for (int j = l100 + 1; j < max100; j++)
                    {
                        good += nn100[j].Item3;
                        int closed = Math.Min(open, nn100[j].Item2);
                        good += closed;

                        open = open + nn100[j].Item1 - closed;
                        close = close + nn100[j].Item2 - closed;
                    }

                    for (int j = l10000 + 1; j < r10000; j++)
                    {
                        good += nn10000[j].Item3;
                        int closed = Math.Min(open, nn10000[j].Item2);
                        good += closed;

                        open = open + nn10000[j].Item1 - closed;
                        close = close + nn10000[j].Item2 - closed;
                    }

                    for (int j = r10000*100; j < r100; j++)
                    {
                        good += nn100[j].Item3;
                        int closed = Math.Min(open, nn100[j].Item2);
                        good += closed;

                        open = open + nn100[j].Item1 - closed;
                        close = close + nn100[j].Item2 - closed;
                    }

                    for (int j = r100*100; j <= r; j++)
                    {
                        if (s[j] == '(')
                            open++;
                        else
                        {
                            if (open > 0)
                            {
                                good++;
                                open--;
                            }
                            else
                            {
                                close++;
                            }
                        }
                    }

                    writer.WriteLine(2*good);
                }
            }

            writer.Flush();
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;
using System.IO;
using System.Text;

namespace Sereja_and_Brackets
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            string s = reader.ReadLine();

            var nn100 = new Tuple<int, int, int>[s.Length/100];
            var nn10000 = new Tuple<int, int, int>[s.Length/10000];

            for (int i = 0; i < nn100.Length; i++)
            {
                int max = 100*(i + 1);
                int open = 0;
                int close = 0;
                int good = 0;

                for (int j = max - 100; j < max; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }
                nn100[i] = new Tuple<int, int, int>(open, close, good);
            }
            for (int i = 0; i < nn10000.Length; i++)
            {
                int max = 100*(i + 1);
                int open = 0;
                int close = 0;
                int good = 0;

                for (int j = max - 100; j < max; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }
                nn10000[i] = new Tuple<int, int, int>(open, close, good);
            }

            int m = Next();
            for (int i = 0; i < m; i++)
            {
                int l = Next() - 1;
                int r = Next() - 1;

                int l100 = l/100;
                int r100 = r/100;

                int l10000 = l/10000;
                int r10000 = r/10000;

                int open = 0;
                int close = 0;
                int good = 0;

                int maxl = Math.Min(l100 + 1, r100)*100;
                for (int j = l; j < maxl; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }

                int max100 = Math.Min(l10000 + 1, r10000)*100;
                for (int j = l100 + 1; j < max100; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }

                for (int j = l10000 + 1; j < r10000; j++)
                {
                    good += nn10000[j].Item3;
                    int closed = Math.Min(open, nn10000[j].Item2);
                    good += closed;

                    open = open + nn10000[j].Item1 - closed;
                    close = close + nn10000[j].Item2 - closed;
                }

                for (int j = r10000*100; j < r100; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }

                for (int j = Math.Max(l, r100*100); j <= r; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }

                writer.WriteLine(2*good);
            }

            writer.Flush();
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;
using System.IO;
using System.Text;

namespace Sereja_and_Brackets
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            string s = reader.ReadLine();

            var nn100 = new Tuple<int, int, int>[s.Length/100];
            var nn10000 = new Tuple<int, int, int>[s.Length/10000];

            for (int i = 0; i < nn100.Length; i++)
            {
                int max = 100*(i + 1);
                int open = 0;
                int close = 0;
                int good = 0;

                for (int j = max - 100; j < max; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }
                nn100[i] = new Tuple<int, int, int>(open, close, good);
            }
            for (int i = 0; i < nn10000.Length; i++)
            {
                int max = 100*(i + 1);
                int open = 0;
                int close = 0;
                int good = 0;

                for (int j = max - 100; j < max; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }
                nn10000[i] = new Tuple<int, int, int>(open, close, good);
            }

            int m = Next();
            for (int i = 0; i < m; i++)
            {
                int l = Next() - 1;
                int r = Next() - 1;

                int l100 = l/100;
                int r100 = r/100;

                int l10000 = l/10000;
                int r10000 = r/10000;

                int open = 0;
                int close = 0;
                int good = 0;

                int max100 = Math.Min(l10000 + 1, r10000)*100;
                int maxl = Math.Min(l100 + 1, max100)*100;

                for (int j = l; j < maxl; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }

                for (int j = l100 + 1; j < max100; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }

                for (int j = l10000 + 1; j < r10000; j++)
                {
                    good += nn10000[j].Item3;
                    int closed = Math.Min(open, nn10000[j].Item2);
                    good += closed;

                    open = open + nn10000[j].Item1 - closed;
                    close = close + nn10000[j].Item2 - closed;
                }

                for (int j = Math.Max(r10000*100, l100 + 1); j < r100; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }

                for (int j = Math.Max(l, r100*100); j <= r; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }

                writer.WriteLine(2*good);
            }

            writer.Flush();
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;
using System.IO;
using System.Text;

namespace Sereja_and_Brackets
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            string s = reader.ReadLine();

            var nn100 = new Tuple<int, int, int>[s.Length/100];
            var nn10000 = new Tuple<int, int, int>[s.Length/10000];

            for (int i = 0; i < nn100.Length; i++)
            {
                int max = 100*(i + 1);
                int open = 0;
                int close = 0;
                int good = 0;

                for (int j = max - 100; j < max; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }
                nn100[i] = new Tuple<int, int, int>(open, close, good);
            }
            for (int i = 0; i < nn10000.Length; i++)
            {
                int max = 100*(i + 1);
                int open = 0;
                int close = 0;
                int good = 0;

                for (int j = max - 100; j < max; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }
                nn10000[i] = new Tuple<int, int, int>(open, close, good);
            }

            int m = Next();
            for (int i = 0; i < m; i++)
            {
                int l = Next() - 1;
                int r = Next() - 1;

                int l100 = l/100;
                int r100 = r/100;

                int l10000 = l/10000;
                int r10000 = r/10000;

                int open = 0;
                int close = 0;
                int good = 0;

                int max100 = Math.Min(l10000 + 1, r10000)*100;
                int maxl = Math.Min(l100 + 1, r100) * 100;

                for (int j = l; j < maxl; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }

                for (int j = l100 + 1; j < max100; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }

                for (int j = l10000 + 1; j < r10000; j++)
                {
                    good += nn10000[j].Item3;
                    int closed = Math.Min(open, nn10000[j].Item2);
                    good += closed;

                    open = open + nn10000[j].Item1 - closed;
                    close = close + nn10000[j].Item2 - closed;
                }

                for (int j = Math.Max(r10000*100, l100 + 1); j < r100; j++)
                {
                    good += nn100[j].Item3;
                    int closed = Math.Min(open, nn100[j].Item2);
                    good += closed;

                    open = open + nn100[j].Item1 - closed;
                    close = close + nn100[j].Item2 - closed;
                }

                for (int j = Math.Max(l, r100*100); j <= r; j++)
                {
                    if (s[j] == '(')
                        open++;
                    else
                    {
                        if (open > 0)
                        {
                            good++;
                            open--;
                        }
                        else
                        {
                            close++;
                        }
                    }
                }

                writer.WriteLine(2*good);
            }

            writer.Flush();
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


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

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

	internal class Task
	{
		public void Solve(InputReader sr, TextWriter sw)
		{
			var array = sr.NextString().ToCharArray();
			var segmentTree = new SegmentTree(array);
			var n = sr.NextInt32();
			var sb = new StringBuilder();
			for (var i = 0; i < n; i++) {
				var input = sr.ReadArray(Int32.Parse);
				sb.AppendLine(segmentTree.Query(input[0] - 1, input[1] - 1).Sum.ToString());
			}

			sw.Write(sb);
		}
	}

	class SegmentTree
	{
		private Node[] tree;
		private int len;

		public SegmentTree(char[] array)
		{
			Build(array);
		}

		private void Build(char[] array)
		{
			var log = (int)Math.Ceiling(Math.Log(array.Length, 2));
			len = (int)Math.Pow(2, log);
			tree = new Node[len * 2];
			for (var i = 0; i < array.Length; i++) {
				var node = new Node();
				if (array[i] == '(')
					node.LeftCount++;
				if (array[i] == ')')
					node.RightCount++;

				tree[len + i] = node;
			}
			for (var i = array.Length; i < len; i++) {
				tree[i + len] = new Node();
			}
			for (var i = len - 1; i > 0; i--) {
				tree[i] = Combine(tree[i * 2], tree[i * 2 + 1]);
			}
		}

		private Node Combine(Node left, Node right)
		{
			var result = new Node {
				Sum = left.Sum + right.Sum
			};
			var min = Math.Min(left.LeftCount, right.RightCount);
			result.Sum += 2*min;
			result.LeftCount = left.LeftCount + right.LeftCount - min;
			result.RightCount = right.RightCount + left.RightCount - min;

			return result;
		}

		public Node Query(int l, int r)
		{
			var from = l + len;
			var to = r + len;
			var left = new Node();
			var right = new Node();
			while (from <= to) {
				if (from % 2 == 1) {
					left = Combine(left, tree[from]);
				}
				if (to % 2 == 0) {
					right = Combine(tree[to], right);
				}
				from = (from + 1) / 2;
				to = (to - 1) / 2;
			}

			return Combine(left, right);
		}

		public void Update(int i, int v)
		{
			//var indx = len + i;
			//var old = tree[indx];
			//tree[indx] = v;
			//indx /= 2;
			//while (indx > 0) {
			//	tree[indx] += -old + v;
			//	indx /= 2;
			//}
		}
	}

	class Node
	{
		public int LeftCount { get; set; }

		public int RightCount { get; set; }

		public int Sum { get; set; }
	}
}

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();
	}

	public T[] ReadArrayFromString<T>(Func<string, CultureInfo, T> func, string str)
	{
		return
						str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
										.Select(s => func(s, CultureInfo.InvariantCulture))
										.ToArray();
	}

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

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


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

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

	internal class Task
	{
		public void Solve(InputReader sr, TextWriter sw)
		{
			var array = sr.NextString().ToCharArray();
			var segmentTree = new SegmentTree(array);
			var n = sr.NextInt32();
			var sb = new StringBuilder();
			for (var i = 0; i < n; i++) {
				var input = sr.ReadArray(Int32.Parse);
				sb.AppendLine(segmentTree.Query(input[0] - 1, input[1] - 1).Sum.ToString());
			}

			sw.Write(sb);
		}
	}

	class SegmentTree
	{
		private Node[] tree;
		private int len;

		public SegmentTree(char[] array)
		{
			Build(array);
		}

		private void Build(char[] array)
		{
			var log = (int)Math.Ceiling(Math.Log(array.Length, 2));
			len = (int)Math.Pow(2, log);
			tree = new Node[len * 2];
			for (var i = 0; i < array.Length; i++) {
				var node = new Node();
				if (array[i] == '(')
					node.LeftCount++;
				if (array[i] == ')')
					node.RightCount++;

				tree[len + i] = node;
			}
			for (var i = array.Length; i < len; i++) {
				tree[i + len] = new Node();
			}
			for (var i = len - 1; i > 0; i--) {
				tree[i] = Combine(tree[i * 2], tree[i * 2 + 1]);
			}
		}

		private Node Combine(Node left, Node right)
		{
			var result = new Node {
				Sum = left.Sum + right.Sum
			};
			var min = Math.Min(left.LeftCount, right.RightCount);
			result.Sum += 2*min;
			//if (min != 0) {
			//	result.LeftCount = left.LeftCount - min;
			//	result.RightCount = right.RightCount - min;
			//}
			//else {
				result.LeftCount = left.LeftCount + right.LeftCount - min;
				result.RightCount = right.RightCount + left.RightCount - min;
			//}

			return result;
		}

		public Node Query(int l, int r)
		{
			var from = l + len;
			var to = r + len;
			var left = new Node();
			var right = new Node();
			while (from <= to) {
				if (from % 2 == 1) {
					left = Combine(left, tree[from]);
				}
				if (to % 2 == 0) {
					right = Combine(right, tree[to]);
				}
				from = (from + 1) / 2;
				to = (to - 1) / 2;
			}

			return Combine(left, right);
		}

		public void Update(int i, int v)
		{
			//var indx = len + i;
			//var old = tree[indx];
			//tree[indx] = v;
			//indx /= 2;
			//while (indx > 0) {
			//	tree[indx] += -old + v;
			//	indx /= 2;
			//}
		}
	}

	class Node
	{
		public int LeftCount { get; set; }

		public int RightCount { get; set; }

		public int Sum { get; set; }
	}
}

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();
	}

	public T[] ReadArrayFromString<T>(Func<string, CultureInfo, T> func, string str)
	{
		return
						str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
										.Select(s => func(s, CultureInfo.InvariantCulture))
										.ToArray();
	}

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

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