#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> g;
set<int> ind;
vector<int> vis;

void independent(int v, int k) {
	if (ind.size() == k) {
		return;
	}

	ind.clear();

	queue<pair<int, bool>> q;
	q.push({v, true});

	while (!q.empty()) {
		pair<int, bool> vv = q.front();
		q.pop();
		v = vv.first;

		vis[v] = true;

		bool pick = vv.second;
		for (int u : g[v]) {
			if (ind.find(u) != ind.end()) {
				pick = false;
			}

			if (!vis[u]) {
				q.push({u, !vv.second});
			}
		}

		if (pick) {
			ind.insert(v);
		}

		if (ind.size() == k) {
			return;
		}
	}
}

vector<int> path;
vector<int> findCycle(int v, int p, int k) {
	if (vis[v] == 1) {
		vector<int> cycle;
		int ancestor = p;
		while (ancestor != v) {
			cycle.push_back(ancestor);
			ancestor = path[ancestor];
		}

		cycle.push_back(v);

		if (cycle.size() > k) {
			cycle.clear();
		}

		return cycle;
	}

	vis[v] = 1;
	path[v] = p;

	for (int u : g[v]) {
		if (u != p && vis[u] != 2) {
			vector<int> c = findCycle(u, v, k);
			if (c.size() > 0) {
				return c;
			}
		}
	}

	vis[v] = 2;

	return vector<int>();
}

int main() {
	int n, m, k;
	cin >> n >> m >> k;

	g = vector<vector<int>>(n);
	vis = vector<int>(n, 0);

	for (int i = 0; i < m; ++i) {
		int u, v;
		cin >> u >> v;

		--u;
		--v;

		g[u].push_back(v);
		g[v].push_back(u);
	}

	int indNum = (k + 1) / 2;
	independent(0, indNum);

	vis = vector<int>(n, 0);
	independent(g[0][0], indNum);
	if (ind.size() == indNum) {
		cout << 1 << endl;
		for (int i : ind) {
			cout << (i + 1) << " ";
		}
		return 0;
	}

	vis = vector<int>(n, 0);
	path = vector<int>(n);

	vector<int> cycle = findCycle(0, 0, k);
	reverse(cycle.begin(), cycle.end());

	cout << 2 << endl;
	cout << cycle.size() << endl;
	for (int ci : cycle) {
		cout << (ci + 1) << " ";
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> g;
set<int> ind;
vector<int> vis;

void independent(int v, int k, bool pick) {
	if (ind.size() == k) {
		return;
	}

	ind.clear();

	queue<pair<int, bool>> q;
	q.push({v, true});

	while (!q.empty()) {
		pair<int, bool> vv = q.front();
		q.pop();
		v = vv.first;

		vis[v] = true;

		bool pick = vv.second;
		for (int u : g[v]) {
			if (ind.find(u) != ind.end()) {
				pick = false;
			}

			if (!vis[u]) {
				q.push({u, !vv.second});
			}
		}

		if (pick) {
			ind.insert(v);
		}

		if (ind.size() == k) {
			return;
		}
	}
}

// vector<int> path;
// vector<int> findCycle(int v, int p, int k) {
// 	vector<int> cycle;

// 	if (vis[v] == 1) {
// 		int ancestor = p;
// 		while (ancestor != v) {
// 			cycle.push_back(ancestor);
// 			ancestor = path[ancestor];
// 		}

// 		cycle.push_back(v);

// 		return cycle;
// 	}

// 	vis[v] = 1;
// 	path[v] = p;

// 	for (int u : g[v]) {
// 		if (u != p && vis[u] != 2) {
// 			cycle = findCycle(u, v, k);
// 			if (cycle.size() <= k) {
// 				break;
// 			}
// 		}
// 	}

// 	vis[v] = 2;

// 	return cycle;
// }

vector<int> findCycle(int n)
{
    int md = INT_MAX;
	vector<int> ans;

    for (int i = 0; i < n; i++) {
        vector<int> dist(n, (int)(1e9));
        dist[i] = 0;

        vector<int> par(n, -1);
        queue<int> q;
        q.push(i);

        while (!q.empty()) {

            int x = q.front();
            q.pop();

            for (int child : g[x]) {
                if (dist[child] == (int)(1e9)) {
                    dist[child] = 1 + dist[x];
                    par[child] = x;
                    q.push(child);
                } else if (par[x] != child && par[child] != x) {
					int d = dist[x] + dist[child] + 1;
					if (d < md) {
						md = d;

						int anc = x;
						vector<int> a;
						while (anc >= 0) {
							a.push_back(anc);
							anc = par[anc];
						}
						reverse(a.begin(), a.end());

						anc = child;
						while (anc > 0) {
							a.push_back(anc);
							anc = par[anc];
						}

						if (a[0] == a[a.size() - 1]) {
							a.erase(a.end() - 1);
						}

						ans = a;
					}
				}
            }
        }
    }

    return ans;
}

int main() {
	int n, m, k;
	cin >> n >> m >> k;

	g = vector<vector<int>>(n);
	vis = vector<int>(n, 0);

	for (int i = 0; i < m; ++i) {
		int u, v;
		cin >> u >> v;

		--u;
		--v;

		g[u].push_back(v);
		g[v].push_back(u);
	}

	int indNum = (k + 1) / 2;
	independent(0, indNum, true);
	independent(1, indNum, true);
	if (ind.size() == indNum) {
		cout << 1 << endl;
		for (int i : ind) {
			cout << (i + 1) << " ";
		}
		return 0;
	}

	vis = vector<int>(n, 0);
	// path = vector<int>(n);

	vector<int> cycle = findCycle(n);
	// reverse(cycle.begin(), cycle.end());

	cout << 2 << endl;
	cout << cycle.size() << endl;
	for (int ci : cycle) {
		cout << (ci + 1) << " ";
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> g;
set<int> ind;
vector<int> vis;

void independent(int v, int k) {
	if (ind.size() == k) {
		return;
	}

	ind.clear();

	queue<pair<int, bool>> q;
	q.push({v, true});

	while (!q.empty()) {
		pair<int, bool> vv = q.front();
		q.pop();
		v = vv.first;

		vis[v] = true;

		bool pick = vv.second;
		for (int u : g[v]) {
			if (ind.find(u) != ind.end()) {
				pick = false;
			}

			if (!vis[u]) {
				q.push({u, !vv.second});
			}
		}

		if (pick) {
			ind.insert(v);
		}

		if (ind.size() == k) {
			return;
		}
	}
}

vector<int> path;
vector<int> findCycle(int v, int p, int k) {
	if (vis[v] == 1) {
		vector<int> cycle;
		int ancestor = p;
		while (ancestor != v) {
			cycle.push_back(ancestor);
			ancestor = path[ancestor];
		}

		cycle.push_back(v);

		if (cycle.size() > k) {
			cycle.clear();
		}

		return cycle;
	}

	vis[v] = 1;
	path[v] = p;

	for (int u : g[v]) {
		if (u != p && vis[u] != 2) {
			vector<int> c = findCycle(u, v, k);
			if (c.size() > 0) {
				return c;
			}
		}
	}

	vis[v] = 2;

	return vector<int>();
}

int main() {
	int n, m, k;
	cin >> n >> m >> k;

	g = vector<vector<int>>(n);
	vis = vector<int>(n, 0);

	for (int i = 0; i < m; ++i) {
		int u, v;
		cin >> u >> v;

		--u;
		--v;

		g[u].push_back(v);
		g[v].push_back(u);
	}

	int indNum = (k + 1) / 2;
	independent(0, indNum);
	independent(g[0][0], indNum);
	if (ind.size() == indNum) {
		cout << 1 << endl;
		for (int i : ind) {
			cout << (i + 1) << " ";
		}
		return 0;
	}

	vis = vector<int>(n, 0);
	path = vector<int>(n);

	vector<int> cycle = findCycle(0, 0, k);
	reverse(cycle.begin(), cycle.end());

	cout << 2 << endl;
	cout << cycle.size() << endl;
	for (int ci : cycle) {
		cout << (ci + 1) << " ";
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> g;
set<int> ind;
vector<int> vis;

void independent(int v, int k) {
	if (ind.size() == k) {
		return;
	}

	ind.clear();

	queue<pair<int, bool>> q;
	q.push({v, true});

	while (!q.empty()) {
		pair<int, bool> vv = q.front();
		q.pop();
		v = vv.first;

		vis[v] = true;

		bool pick = vv.second;
		for (int u : g[v]) {
			if (ind.find(u) != ind.end()) {
				pick = false;
			}

			if (!vis[u]) {
				q.push({u, !vv.second});
			}
		}

		if (pick) {
			ind.insert(v);
		}

		if (ind.size() == k) {
			return;
		}
	}
}

vector<int> path;
vector<int> findCycle(int v, int p, int k) {
	if (vis[v] == 1) {
		vector<int> cycle;
		int ancestor = p;
		while (ancestor != v) {
			cycle.push_back(ancestor);
			ancestor = path[ancestor];
		}

		cycle.push_back(v);

		if (cycle.size() > k) {
			cycle.clear();
		}

		return cycle;
	}

	vis[v] = 1;
	path[v] = p;

	for (int u : g[v]) {
		if (u != p && vis[u] != 2) {
			vector<int> c = findCycle(u, v, k);
			if (c.size() > 0) {
				return c;
			}
		}
	}

	vis[v] = 2;

	return vector<int>();
}

int main() {
	int n, m, k;
	cin >> n >> m >> k;

	g = vector<vector<int>>(n);
	vis = vector<int>(n, 0);

	int mn = 1e9;
	int r;
	for (int i = 0; i < m; ++i) {
		int u, v;
		cin >> u >> v;

		--u;
		--v;

		g[u].push_back(v);
		g[v].push_back(u);

		if (g[u].size() < mn) {
			mn = g[u].size();
			r = u;
		}

		if (g[v].size() < mn) {
			mn = g[v].size();
			r = v;
		}
	}

	int indNum = (k + 1) / 2;
	independent(r, indNum);
	independent(g[r][0], indNum);
	if (ind.size() == indNum) {
		cout << 1 << endl;
		for (int i : ind) {
			cout << (i + 1) << " ";
		}
		return 0;
	}

	vis = vector<int>(n, 0);
	path = vector<int>(n);

	vector<int> cycle = findCycle(0, 0, k);
	reverse(cycle.begin(), cycle.end());

	cout << 2 << endl;
	cout << cycle.size() << endl;
	for (int ci : cycle) {
		cout << (ci + 1) << " ";
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> g;
set<int> ind;
vector<int> vis;

void independent(int v, int k, bool pick) {
	if (ind.size() == k) {
		return;
	}

	ind.clear();

	queue<pair<int, bool>> q;
	q.push({v, true});

	while (!q.empty()) {
		pair<int, bool> vv = q.front();
		q.pop();
		v = vv.first;

		vis[v] = true;

		bool pick = vv.second;
		for (int u : g[v]) {
			if (ind.count(u) > 0) {
				pick = false;
			}

			if (!vis[u]) {
				q.push({u, !vv.second});
			}
		}

		if (pick) {
			ind.insert(v);
		}

		if (ind.size() == k) {
			return;
		}
	}
}

vector<int> path;
vector<int> findCycle(int v, int p, int k) {
	if (vis[v] == 1) {
		vector<int> cycle;
		int ancestor = p;
		while (ancestor != v) {
			cycle.push_back(ancestor);
			ancestor = path[ancestor];
		}

		cycle.push_back(v);

		if (cycle.size() > k) {
			cycle.clear();
		}

		return cycle;
	}

	vis[v] = 1;
	path[v] = p;

	for (int u : g[v]) {
		if (u != p && vis[u] != 2) {
			vector<int> c = findCycle(u, v, k);
			if (c.size() > 0) {
				return c;
			}
		}
	}

	vis[v] = 2;

	return vector<int>();
}

int main() {
	int n, m, k;
	cin >> n >> m >> k;

	g = vector<vector<int>>(n);
	vis = vector<int>(n, 0);

	for (int i = 0; i < m; ++i) {
		int u, v;
		cin >> u >> v;

		--u;
		--v;

		g[u].push_back(v);
		g[v].push_back(u);
	}

	int indNum = (k + 1) / 2;
	independent(0, indNum, true);
	independent(1, indNum, true);
	if (ind.size() == indNum) {
		cout << 1 << endl;
		for (int i : ind) {
			cout << (i + 1) << " ";
		}
		return 0;
	}

	vis = vector<int>(n, 0);
	path = vector<int>(n);

	vector<int> cycle = findCycle(0, 0, k);
	reverse(cycle.begin(), cycle.end());

	cout << 2 << endl;
	cout << cycle.size() << endl;
	for (int ci : cycle) {
		cout << (ci + 1) << " ";
	}

	return 0;
}


#include <algorithm>
#include <stack>
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
#define mkp pair <int,int>
using namespace std;
const int maxn = 1 * 1e6 + 1;
typedef long long ll;
const int Maxn = 1e9 + 1;
void read(int &x)
{
	char ch;
	bool flag = 0;
	for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
	for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar());
	x *= 1 - 2 * flag;
}

int val[maxn];
vector <int> vec[maxn];
//vector < pair <int,int> > vec[maxn];
int main() {
    int n,m;
    read(n),read(m);
    for(int i=1;i<=n;i++)read(val[i]);
    for(int i=0;i<m;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
         vec[l].push_back(r);
    }
    int ans=INT_MAX;

    for(int i=1;i<=n;i++)
    {
        bool fuck=1;
        for(int j=0;j<vec[i].size();j++)
        {
            int tmp=vec[i][j];
            for(int  k=0;k<vec[tmp].size();k++)
            {
                  int temp=vec[tmp][k];
                  for(int l=0;l<vec[temp].size();l++)
                  {
                      //cout<<i<<' ' << tmp <<' ' << temp<<endl;
                      if(vec[temp][l]==i)
                      {
                          ans=min(ans,val[i]+val[tmp]+val[temp]);
                          fuck=0;
                          break;
                      }
                  }
                  if(!fuck)break;
            }
            if(!fuck)break;
        }
    }

    if(ans==INT_MAX)
    {
        cout<<-1<<endl;
    }
    else {
        cout<<ans<<endl;
    }
    return 0;
}


#include <algorithm>
#include <stack>
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
#define mkp pair <int,int>
using namespace std;
const int maxn = 2 * 1e6 + 1;
typedef long long ll;
const int Maxn = 1e9 + 1;
void read(int &x)
{
	char ch;
	bool flag = 0;
	for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
	for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar());
	x *= 1 - 2 * flag;
}

int val[maxn];
int edge[1005][1005];
//vector < pair <int,int> > vec[maxn];
int main() {
    int n,m,ans=INT_MAX;
    read(n),read(m);
    for(int i=1;i<=n;i++)read(val[i]);
    for(int i=0;i<m;i++)
    {
        int l,r;
        read(l),read(r);
        edge[l][r]=edge[r][l]=1;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            for(int k=1;k<=n;k++)
            {
                for(int h=1;h<=n;h++)
                {
                    if(edge[i][j]&&edge[j][k]&&edge[k][h]&&h==i)
                    {
                        ans=min(ans,val[i]+val[j]+val[k]);
                    }
                }
            }
        }
    }
    if(ans==INT_MAX)
    {
        cout<<-1<<endl;
    }
    else {
        cout<<ans<<endl;
    }
    return 0;
}

/*

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
    ll n;
    cin>>n;
    vector<ll>l,r;

    ll x,y;

    for(int i=0 ;i<n ;i++)
    {
        cin>>x>>y;
       l.push_back(x);
       r.push_back(y);
    }

    int cnt=0,c1=0,c2=0;
    sort(l.begin(),l.end());
    sort(r.begin(),r.end());
    for(int i=0 ;c1<n&&c2<n;i++)
    {
        if(l[c1]<=r[c2])
         {
             cnt++;
             c1++;
         }
         else
         {
             cnt--;
             c2++;
         }

    if(cnt>2)
    {
        cout<<"NO"<<endl;
        return 0;
    }
    }

    {
        cout<<"YES"<<endl;
    }

    return 0;
}
*/

/*#include <algorithm>
#include <stack>
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
using namespace std;
const int maxn = 3e3 + 1000;
typedef long long ll;
int  arr[maxn];

double ans[100];
int n;
bool check(double mid)
{
	int top = 0;

	for (int i = 0; i < n; )
	{
		ans[top++] = arr[i] + mid;
		i = lower_bound(arr, arr + n, arr[i] + 2 * mid) - arr;
		if(arr[i]<=ans[top-1]+mid)i++;
		if(i>=n)break;
		//if(mid==5)
		//cout<<ans[top-1]<<' '<<i<<endl;
		if (top >= 3)return true;
	}
	return false;
}
int main() {
	//int n;
	scanf("%d", &n);
	for (int i = 0; i<n; i++)scanf("%d", &arr[i]);
	sort(arr, arr + n);
	double ans = 0;
	double l = 0, r = arr[n - 1] - arr[0];
	for (int i = 0; i <= 100; i++)
	{
		double mid = (l + r) / 2;
		if (check(mid))
		{
			ans = mid;
			l = mid;
		}
		else {
			r = mid;
		}
	}
	printf("%.2lf\n", ans);
	return 0;
}

*/

/*
  long long int s[maxn+4],p[maxn+4],k[maxn+4];
  inline int getsum(int k){int sum=0;while(k){sum++;k/=10;}return sum;}
  int main(){
      s[1]=1;
      int cnt=1;
      p[0]=0;k[0]=0;
      for(int i=1;i<= 52849;i++)s[i]=getsum(i);
      for(int i=1;i<= 52849;i++)p[i]=p[i-1]+s[i];
      for(int i=1;i<= 52849;i++)k[i]=p[i]+k[i-1];
      int t;
      scanf("%d",&t);
      int left,pos;
      while(t--)
      {
          int num;
          scanf("%d", &num);

          left=lower_bound(k+1,k+1+maxn,num)-(k+1);

          pos=num-k[left];
          left=lower_bound(p+1,p+1+maxn,pos)-(p+1);

          pos=pos-p[left];
          left++;
          pos=s[left]-pos+1;
          int i=1;
          while(i<pos){left/=10;i++;}
          cout<<left%10<<endl;

      }

      return 0;
  }I*/
/*#include <algorithm>
#include <stack>k,
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
using namespace std;
const int maxn = 5e3 + 5;
typedef long long ll;

int n,m;
int arr[maxn][maxn];
char out[maxn][maxn];
int tmp[maxn][maxn];
int vis[maxn];
void work()
{
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
         tmp[i][j]=arr[i][j];
}
void solve(int i,int j)
{
    tmp[i][j]|=tmp[i][j];
    if(i+1<=n)tmp[i+1][j]|=tmp[i+1][j];
    if(j+1<=m)tmp[i][j+1]|=tmp[i][j+1];
    if(i-1>=1)tmp[i-1][j]|=tmp[i-1][j];
    if(j-1>=1)tmp[i][j-1]|=tmp[i][j-1];
}
int main(){
  //  int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
             int tem;
             scanf("%d",&tem);
             out[i][j]=tem+'0';
        }
    }
    int len=strlen(out[1]);
    for(int i=1;i<=(1<<len);i++){
         work();
         for(int j=1;j<=len;j++){
            if(i&(1<<j)){
               solve(i,j);
            }
         }
         for(int j=2;j<=n;j++){
            for(int k=1;k<=m;k++){

            }
         }
    }
    return 0;
}
*/

//1 1 1 1 1 2 1 2

/*#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#define ll long long
#define N 405
using namespace std;
const int dx[5]={1,-1,0,0},dy[5]={0,0,1,-1};
int n,m,f[N][N],ex,ey,sx,sy,q,u1[N*3],v1[N*3];
char ch;
char a[N][N];
struct he{
    int u,v,t;
}b[N][N];
queue<he>Q;
void bfs(int sx,int sy){
    int ans=10000000;
    while(!Q.empty()) Q.pop();
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++) f[i][j]=10000000;
    f[sx][sy]=0;
    Q.push((he){sx,sy,0});
    while(!Q.empty()){
        he p=Q.front();Q.pop();
        if(p.u==ex&&p.v==ey) {
            ans=min(ans,p.t);
            continue;
        }
        if(b[p.u][p.v].u!=-1){
            int x1=b[p.u][p.v].u;
            int y1=b[p.u][p.v].v;
            if(a[x1][y1]!='#'&&f[x1][y1]>p.t+3){
                f[x1][y1]=p.t+3;
                Q.push((he){x1,y1,p.t+3});
            }
        }
        for(int i=0;i<4;i++){
            int x1=p.u+dx[i];
            int y1=p.v+dy[i];
            if(x1<n&&x1>=0&&y1>=0&&y1<m&&a[x1][y1]!='#'&&f[x1][y1]>p.t+1){
                f[x1][y1]=p.t+1;
                Q.push((he){x1,y1,p.t+1});
            }
        }
    }
    if(ans!= 10000000)
    printf("%d\n",ans);else printf("-1\n");
}
int main(){
    //freopen("1.in","r",stdin);
    for(int i=0;i<=300;i++)
        for(int j=0;j<=300;j++) b[i][j].u=b[i][j].v=-1;
    while(scanf("%d%d%d",&n,&m,&q)!=EOF){
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++){
                scanf("%c",&ch);
                while(ch!='.'&&ch!='S'&&ch!='T'&&ch!='#') ch=getchar();
                a[i][j]=ch;
                if(ch=='S') sx=i,sy=j;
                if(ch=='T') ex=i,ey=j;
            }
        for(int i=1;i<=q;i++){
            scanf("%d%d",&u1[i],&v1[i]);
            scanf("%d%d",&b[u1[i]][v1[i]].u,&b[u1[i]][v1[i]].v);
        }
        bfs(sx,sy);
        for(int i=1;i<=q;i++)
            b[u1[i]][v1[i]].u=-1,b[u1[i]][v1[i]].v=-1;
    }
}*/
/*
long long int n,arr[maxn];
int vis[maxn];
long long int sum[maxn];
void init()
{
    memset(vis,0,sizeof(vis));
    memset(sum,0,sizeof(sum));
    vis[1]=1;sum[1]=0;
    vis[2]=0;sum[2]=0;
    for(int i = 1;i<sqrt(maxn);i++) {
         if(!vis[i]) {
            for(int j = 2;j*i<maxn;j++) {
                vis[i]=1;
                sum[i*j]++;
            }
         }
    }//for(int i = 1;i<100;i++)printf("%d ",sum[i]);
}
int main() {
    int T;
    init();
    while(~scanf("%d",&T))
    {
        scanf("%d",&n);
        long long int ans = 0;
        for(int i = 0;i<n;i++)
        {
            long long int val;
            scanf("%lld",&val);
            ans+=val-sum[val]-1;
            //scanf("%d",&arr[i]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

*/

/*
int arr[maxn][maxn];
int dp[maxn][maxn];
int N;
int dfs(int Curi,int Curj,int Sum)
{
    if(Curi>=N||Curj>=N)return 0;
    if(dp[Curi][Curj]!=-1)return dp[Curi][Curj];
    int tmp = Sum,temp=0;
    while(tmp) {
        if(tmp%10!=0)break;
        tmp/=10;
        temp++;
    }
    return dp[Curi][Curj]=temp+min(dfs(Curi+1,Curj,Sum*arr[Curi+1][Curj]),dfs(Curi,Curj+1,Sum*arr[Curi][Curj+1]));
}
void Print(int sum,int Nowi,int Nowj)
{
    int u = 0;
    int Tmp=sum;
    while(Tmp)
    {
        if(Tmp%10)break;
        Tmp/=10;u++;
    }
    Tmp=dp[Nowi][Nowj];
    if(dp[Nowi+1][Nowj])
}
int main() {
   // int n;
    scanf("%d",&N);
    memset(dp,-1,sizeof(dp));
    for(int i = 0;i<N;i++)
       for(int j = 0;j<N;j++) {
        scanf("%d",&arr[i][j]);
       }
    dfs(0,0,arr[0][0]);

    cout<<dp[0][0]<<endl;
    Print(arr[0][0],0,0);
    return 0;
}
*/

/*
long long int n,arr[maxn];
int vis[maxn];
long long int sum[maxn];
void init()
{
    memset(vis,0,sizeof(vis));
    memset(sum,0,sizeof(sum));
    vis[1]=1;sum[1]=0;
    vis[2]=0;sum[2]=0;
    for(int i = 1;i<sqrt(maxn);i++) {
         if(!vis[i]) {
            for(int j = 2;j*i<maxn;j++) {
                vis[i]=1;
                sum[i*j]++;
            }
         }
    }//for(int i = 1;i<100;i++)printf("%d ",sum[i]);
}
int main() {
    int T;
    init();
    while(~scanf("%d",&T))
    {
        scanf("%d",&n);
        long long int ans = 0;
        for(int i = 0;i<n;i++)
        {
            long long int val;
            scanf("%lld",&val);
            ans+=val-sum[val]-1;
            //scanf("%d",&arr[i]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

*/

/*
int arr[maxn][maxn];
int dp[maxn][maxn];
int N;
int dfs(int Curi,int Curj,int Sum)
{
    if(Curi>=N||Curj>=N)return 0;
    if(dp[Curi][Curj]!=-1)return dp[Curi][Curj];
    int tmp = Sum,temp=0;
    while(tmp) {
        if(tmp%10!=0)break;
        tmp/=10;
        temp++;
    }
    return dp[Curi][Curj]=temp+min(dfs(Curi+1,Curj,Sum*arr[Curi+1][Curj]),dfs(Curi,Curj+1,Sum*arr[Curi][Curj+1]));
}
void Print(int sum,int Nowi,int Nowj)
{
    int u = 0;
    int Tmp=sum;
    while(Tmp)
    {
        if(Tmp%10)break;
        Tmp/=10;u++;
    }
    Tmp=dp[Nowi][Nowj];
    if(dp[Nowi+1][Nowj])
}
int main() {
   // int n;
    scanf("%d",&N);
    memset(dp,-1,sizeof(dp));
    for(int i = 0;i<N;i++)
       for(int j = 0;j<N;j++) {
        scanf("%d",&arr[i][j]);
       }
    dfs(0,0,arr[0][0]);

    cout<<dp[0][0]<<endl;
    Print(arr[0][0],0,0);
    return 0;
}
*/


#include <algorithm>
#include <stack>
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
#define mkp pair <int,int>
using namespace std;
const int maxn = 2 * 1e7 + 1;
typedef long long ll;
const int Maxn = 1e9 + 1;
void read(int &x)
{
	char ch;
	bool flag = 0;
	for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
	for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar());
	x *= 1 - 2 * flag;
}

int val[maxn];
vector <int> vec[maxn];
//vector < pair <int,int> > vec[maxn];
int main() {
    int n,m;
    read(n),read(m);
    for(int i=1;i<=n;i++)read(val[i]);
    for(int i=0;i<m;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
         vec[l].push_back(r);
         vec[r].push_back(l);
    }
    int ans=INT_MAX;

    for(int i=1;i<=n;i++)
    {
        bool fuck=1;
        for(int j=0;j<vec[i].size();j++)
        {
            int tmp=vec[i][j];
            for(int  k=0;k<vec[tmp].size();k++)
            {
                  int temp=vec[tmp][k];
                  for(int l=0;l<vec[temp].size();l++)
                  {
                      //cout<<i<<' ' << tmp <<' ' << temp<<endl;
                      if(vec[temp][l]==i)
                      {
                          ans=min(ans,val[i]+val[tmp]+val[temp]);
                          fuck=0;
                          break;
                      }
                  }
                  if(!fuck)break;
            }
            if(!fuck)break;
        }
    }

    if(ans==INT_MAX)
    {
        cout<<-1<<endl;
    }
    else {
        cout<<ans<<endl;
    }
    return 0;
}

/*

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
    ll n;
    cin>>n;
    vector<ll>l,r;

    ll x,y;

    for(int i=0 ;i<n ;i++)
    {
        cin>>x>>y;
       l.push_back(x);
       r.push_back(y);
    }

    int cnt=0,c1=0,c2=0;
    sort(l.begin(),l.end());
    sort(r.begin(),r.end());
    for(int i=0 ;c1<n&&c2<n;i++)
    {
        if(l[c1]<=r[c2])
         {
             cnt++;
             c1++;
         }
         else
         {
             cnt--;
             c2++;
         }

    if(cnt>2)
    {
        cout<<"NO"<<endl;
        return 0;
    }
    }

    {
        cout<<"YES"<<endl;
    }

    return 0;
}
*/

/*#include <algorithm>
#include <stack>
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
using namespace std;
const int maxn = 3e3 + 1000;
typedef long long ll;
int  arr[maxn];

double ans[100];
int n;
bool check(double mid)
{
	int top = 0;

	for (int i = 0; i < n; )
	{
		ans[top++] = arr[i] + mid;
		i = lower_bound(arr, arr + n, arr[i] + 2 * mid) - arr;
		if(arr[i]<=ans[top-1]+mid)i++;
		if(i>=n)break;
		//if(mid==5)
		//cout<<ans[top-1]<<' '<<i<<endl;
		if (top >= 3)return true;
	}
	return false;
}
int main() {
	//int n;
	scanf("%d", &n);
	for (int i = 0; i<n; i++)scanf("%d", &arr[i]);
	sort(arr, arr + n);
	double ans = 0;
	double l = 0, r = arr[n - 1] - arr[0];
	for (int i = 0; i <= 100; i++)
	{
		double mid = (l + r) / 2;
		if (check(mid))
		{
			ans = mid;
			l = mid;
		}
		else {
			r = mid;
		}
	}
	printf("%.2lf\n", ans);
	return 0;
}

*/

/*
  long long int s[maxn+4],p[maxn+4],k[maxn+4];
  inline int getsum(int k){int sum=0;while(k){sum++;k/=10;}return sum;}
  int main(){
      s[1]=1;
      int cnt=1;
      p[0]=0;k[0]=0;
      for(int i=1;i<= 52849;i++)s[i]=getsum(i);
      for(int i=1;i<= 52849;i++)p[i]=p[i-1]+s[i];
      for(int i=1;i<= 52849;i++)k[i]=p[i]+k[i-1];
      int t;
      scanf("%d",&t);
      int left,pos;
      while(t--)
      {
          int num;
          scanf("%d", &num);

          left=lower_bound(k+1,k+1+maxn,num)-(k+1);

          pos=num-k[left];
          left=lower_bound(p+1,p+1+maxn,pos)-(p+1);

          pos=pos-p[left];
          left++;
          pos=s[left]-pos+1;
          int i=1;
          while(i<pos){left/=10;i++;}
          cout<<left%10<<endl;

      }

      return 0;
  }I*/
/*#include <algorithm>
#include <stack>k,
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
using namespace std;
const int maxn = 5e3 + 5;
typedef long long ll;

int n,m;
int arr[maxn][maxn];
char out[maxn][maxn];
int tmp[maxn][maxn];
int vis[maxn];
void work()
{
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
         tmp[i][j]=arr[i][j];
}
void solve(int i,int j)
{
    tmp[i][j]|=tmp[i][j];
    if(i+1<=n)tmp[i+1][j]|=tmp[i+1][j];
    if(j+1<=m)tmp[i][j+1]|=tmp[i][j+1];
    if(i-1>=1)tmp[i-1][j]|=tmp[i-1][j];
    if(j-1>=1)tmp[i][j-1]|=tmp[i][j-1];
}
int main(){
  //  int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
             int tem;
             scanf("%d",&tem);
             out[i][j]=tem+'0';
        }
    }
    int len=strlen(out[1]);
    for(int i=1;i<=(1<<len);i++){
         work();
         for(int j=1;j<=len;j++){
            if(i&(1<<j)){
               solve(i,j);
            }
         }
         for(int j=2;j<=n;j++){
            for(int k=1;k<=m;k++){

            }
         }
    }
    return 0;
}
*/

//1 1 1 1 1 2 1 2

/*#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#define ll long long
#define N 405
using namespace std;
const int dx[5]={1,-1,0,0},dy[5]={0,0,1,-1};
int n,m,f[N][N],ex,ey,sx,sy,q,u1[N*3],v1[N*3];
char ch;
char a[N][N];
struct he{
    int u,v,t;
}b[N][N];
queue<he>Q;
void bfs(int sx,int sy){
    int ans=10000000;
    while(!Q.empty()) Q.pop();
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++) f[i][j]=10000000;
    f[sx][sy]=0;
    Q.push((he){sx,sy,0});
    while(!Q.empty()){
        he p=Q.front();Q.pop();
        if(p.u==ex&&p.v==ey) {
            ans=min(ans,p.t);
            continue;
        }
        if(b[p.u][p.v].u!=-1){
            int x1=b[p.u][p.v].u;
            int y1=b[p.u][p.v].v;
            if(a[x1][y1]!='#'&&f[x1][y1]>p.t+3){
                f[x1][y1]=p.t+3;
                Q.push((he){x1,y1,p.t+3});
            }
        }
        for(int i=0;i<4;i++){
            int x1=p.u+dx[i];
            int y1=p.v+dy[i];
            if(x1<n&&x1>=0&&y1>=0&&y1<m&&a[x1][y1]!='#'&&f[x1][y1]>p.t+1){
                f[x1][y1]=p.t+1;
                Q.push((he){x1,y1,p.t+1});
            }
        }
    }
    if(ans!= 10000000)
    printf("%d\n",ans);else printf("-1\n");
}
int main(){
    //freopen("1.in","r",stdin);
    for(int i=0;i<=300;i++)
        for(int j=0;j<=300;j++) b[i][j].u=b[i][j].v=-1;
    while(scanf("%d%d%d",&n,&m,&q)!=EOF){
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++){
                scanf("%c",&ch);
                while(ch!='.'&&ch!='S'&&ch!='T'&&ch!='#') ch=getchar();
                a[i][j]=ch;
                if(ch=='S') sx=i,sy=j;
                if(ch=='T') ex=i,ey=j;
            }
        for(int i=1;i<=q;i++){
            scanf("%d%d",&u1[i],&v1[i]);
            scanf("%d%d",&b[u1[i]][v1[i]].u,&b[u1[i]][v1[i]].v);
        }
        bfs(sx,sy);
        for(int i=1;i<=q;i++)
            b[u1[i]][v1[i]].u=-1,b[u1[i]][v1[i]].v=-1;
    }
}*/
/*
long long int n,arr[maxn];
int vis[maxn];
long long int sum[maxn];
void init()
{
    memset(vis,0,sizeof(vis));
    memset(sum,0,sizeof(sum));
    vis[1]=1;sum[1]=0;
    vis[2]=0;sum[2]=0;
    for(int i = 1;i<sqrt(maxn);i++) {
         if(!vis[i]) {
            for(int j = 2;j*i<maxn;j++) {
                vis[i]=1;
                sum[i*j]++;
            }
         }
    }//for(int i = 1;i<100;i++)printf("%d ",sum[i]);
}
int main() {
    int T;
    init();
    while(~scanf("%d",&T))
    {
        scanf("%d",&n);
        long long int ans = 0;
        for(int i = 0;i<n;i++)
        {
            long long int val;
            scanf("%lld",&val);
            ans+=val-sum[val]-1;
            //scanf("%d",&arr[i]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

*/

/*
int arr[maxn][maxn];
int dp[maxn][maxn];
int N;
int dfs(int Curi,int Curj,int Sum)
{
    if(Curi>=N||Curj>=N)return 0;
    if(dp[Curi][Curj]!=-1)return dp[Curi][Curj];
    int tmp = Sum,temp=0;
    while(tmp) {
        if(tmp%10!=0)break;
        tmp/=10;
        temp++;
    }
    return dp[Curi][Curj]=temp+min(dfs(Curi+1,Curj,Sum*arr[Curi+1][Curj]),dfs(Curi,Curj+1,Sum*arr[Curi][Curj+1]));
}
void Print(int sum,int Nowi,int Nowj)
{
    int u = 0;
    int Tmp=sum;
    while(Tmp)
    {
        if(Tmp%10)break;
        Tmp/=10;u++;
    }
    Tmp=dp[Nowi][Nowj];
    if(dp[Nowi+1][Nowj])
}
int main() {
   // int n;
    scanf("%d",&N);
    memset(dp,-1,sizeof(dp));
    for(int i = 0;i<N;i++)
       for(int j = 0;j<N;j++) {
        scanf("%d",&arr[i][j]);
       }
    dfs(0,0,arr[0][0]);

    cout<<dp[0][0]<<endl;
    Print(arr[0][0],0,0);
    return 0;
}
*/

/*
long long int n,arr[maxn];
int vis[maxn];
long long int sum[maxn];
void init()
{
    memset(vis,0,sizeof(vis));
    memset(sum,0,sizeof(sum));
    vis[1]=1;sum[1]=0;
    vis[2]=0;sum[2]=0;
    for(int i = 1;i<sqrt(maxn);i++) {
         if(!vis[i]) {
            for(int j = 2;j*i<maxn;j++) {
                vis[i]=1;
                sum[i*j]++;
            }
         }
    }//for(int i = 1;i<100;i++)printf("%d ",sum[i]);
}
int main() {
    int T;
    init();
    while(~scanf("%d",&T))
    {
        scanf("%d",&n);
        long long int ans = 0;
        for(int i = 0;i<n;i++)
        {
            long long int val;
            scanf("%lld",&val);
            ans+=val-sum[val]-1;
            //scanf("%d",&arr[i]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

*/

/*
int arr[maxn][maxn];
int dp[maxn][maxn];
int N;
int dfs(int Curi,int Curj,int Sum)
{
    if(Curi>=N||Curj>=N)return 0;
    if(dp[Curi][Curj]!=-1)return dp[Curi][Curj];
    int tmp = Sum,temp=0;
    while(tmp) {
        if(tmp%10!=0)break;
        tmp/=10;
        temp++;
    }
    return dp[Curi][Curj]=temp+min(dfs(Curi+1,Curj,Sum*arr[Curi+1][Curj]),dfs(Curi,Curj+1,Sum*arr[Curi][Curj+1]));
}
void Print(int sum,int Nowi,int Nowj)
{
    int u = 0;
    int Tmp=sum;
    while(Tmp)
    {
        if(Tmp%10)break;
        Tmp/=10;u++;
    }
    Tmp=dp[Nowi][Nowj];
    if(dp[Nowi+1][Nowj])
}
int main() {
   // int n;
    scanf("%d",&N);
    memset(dp,-1,sizeof(dp));
    for(int i = 0;i<N;i++)
       for(int j = 0;j<N;j++) {
        scanf("%d",&arr[i][j]);
       }
    dfs(0,0,arr[0][0]);

    cout<<dp[0][0]<<endl;
    Print(arr[0][0],0,0);
    return 0;
}
*/


#include <algorithm>
#include <stack>
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
#define mkp pair <int,int>
using namespace std;
const int maxn = 1 * 1e6 + 1;
typedef long long ll;
const int Maxn = 1e9 + 1;
void read(int &x)
{
	char ch;
	bool flag = 0;
	for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
	for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar());
	x *= 1 - 2 * flag;
}

int val[maxn];
vector <int> vec[maxn];
//vector < pair <int,int> > vec[maxn];
int main() {
    int n,m;
    read(n),read(m);
    for(int i=1;i<=n;i++)read(val[i]);
    for(int i=0;i<m;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
         vec[l].push_back(r);
         vec[r].push_back(l);
    }
    int ans=INT_MAX;

    for(int i=1;i<=n;i++)
    {
        bool fuck=1;
        for(int j=0;j<vec[i].size();j++)
        {
            int tmp=vec[i][j];
            for(int  k=0;k<vec[tmp].size();k++)
            {
                  int temp=vec[tmp][k];
                  for(int l=0;l<vec[temp].size();l++)
                  {
                      //cout<<i<<' ' << tmp <<' ' << temp<<endl;
                      if(vec[temp][l]==i)
                      {
                          ans=min(ans,val[i]+val[tmp]+val[temp]);
                          fuck=0;
                          break;
                      }
                  }
                  if(!fuck)break;
            }
            if(!fuck)break;
        }
    }

    if(ans==INT_MAX)
    {
        cout<<-1<<endl;
    }
    else {
        cout<<ans<<endl;
    }
    return 0;
}

/*

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
    ll n;
    cin>>n;
    vector<ll>l,r;

    ll x,y;

    for(int i=0 ;i<n ;i++)
    {
        cin>>x>>y;
       l.push_back(x);
       r.push_back(y);
    }

    int cnt=0,c1=0,c2=0;
    sort(l.begin(),l.end());
    sort(r.begin(),r.end());
    for(int i=0 ;c1<n&&c2<n;i++)
    {
        if(l[c1]<=r[c2])
         {
             cnt++;
             c1++;
         }
         else
         {
             cnt--;
             c2++;
         }

    if(cnt>2)
    {
        cout<<"NO"<<endl;
        return 0;
    }
    }

    {
        cout<<"YES"<<endl;
    }

    return 0;
}
*/

/*#include <algorithm>
#include <stack>
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
using namespace std;
const int maxn = 3e3 + 1000;
typedef long long ll;
int  arr[maxn];

double ans[100];
int n;
bool check(double mid)
{
	int top = 0;

	for (int i = 0; i < n; )
	{
		ans[top++] = arr[i] + mid;
		i = lower_bound(arr, arr + n, arr[i] + 2 * mid) - arr;
		if(arr[i]<=ans[top-1]+mid)i++;
		if(i>=n)break;
		//if(mid==5)
		//cout<<ans[top-1]<<' '<<i<<endl;
		if (top >= 3)return true;
	}
	return false;
}
int main() {
	//int n;
	scanf("%d", &n);
	for (int i = 0; i<n; i++)scanf("%d", &arr[i]);
	sort(arr, arr + n);
	double ans = 0;
	double l = 0, r = arr[n - 1] - arr[0];
	for (int i = 0; i <= 100; i++)
	{
		double mid = (l + r) / 2;
		if (check(mid))
		{
			ans = mid;
			l = mid;
		}
		else {
			r = mid;
		}
	}
	printf("%.2lf\n", ans);
	return 0;
}

*/

/*
  long long int s[maxn+4],p[maxn+4],k[maxn+4];
  inline int getsum(int k){int sum=0;while(k){sum++;k/=10;}return sum;}
  int main(){
      s[1]=1;
      int cnt=1;
      p[0]=0;k[0]=0;
      for(int i=1;i<= 52849;i++)s[i]=getsum(i);
      for(int i=1;i<= 52849;i++)p[i]=p[i-1]+s[i];
      for(int i=1;i<= 52849;i++)k[i]=p[i]+k[i-1];
      int t;
      scanf("%d",&t);
      int left,pos;
      while(t--)
      {
          int num;
          scanf("%d", &num);

          left=lower_bound(k+1,k+1+maxn,num)-(k+1);

          pos=num-k[left];
          left=lower_bound(p+1,p+1+maxn,pos)-(p+1);

          pos=pos-p[left];
          left++;
          pos=s[left]-pos+1;
          int i=1;
          while(i<pos){left/=10;i++;}
          cout<<left%10<<endl;

      }

      return 0;
  }I*/
/*#include <algorithm>
#include <stack>k,
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
using namespace std;
const int maxn = 5e3 + 5;
typedef long long ll;

int n,m;
int arr[maxn][maxn];
char out[maxn][maxn];
int tmp[maxn][maxn];
int vis[maxn];
void work()
{
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
         tmp[i][j]=arr[i][j];
}
void solve(int i,int j)
{
    tmp[i][j]|=tmp[i][j];
    if(i+1<=n)tmp[i+1][j]|=tmp[i+1][j];
    if(j+1<=m)tmp[i][j+1]|=tmp[i][j+1];
    if(i-1>=1)tmp[i-1][j]|=tmp[i-1][j];
    if(j-1>=1)tmp[i][j-1]|=tmp[i][j-1];
}
int main(){
  //  int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
             int tem;
             scanf("%d",&tem);
             out[i][j]=tem+'0';
        }
    }
    int len=strlen(out[1]);
    for(int i=1;i<=(1<<len);i++){
         work();
         for(int j=1;j<=len;j++){
            if(i&(1<<j)){
               solve(i,j);
            }
         }
         for(int j=2;j<=n;j++){
            for(int k=1;k<=m;k++){

            }
         }
    }
    return 0;
}
*/

//1 1 1 1 1 2 1 2

/*#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#define ll long long
#define N 405
using namespace std;
const int dx[5]={1,-1,0,0},dy[5]={0,0,1,-1};
int n,m,f[N][N],ex,ey,sx,sy,q,u1[N*3],v1[N*3];
char ch;
char a[N][N];
struct he{
    int u,v,t;
}b[N][N];
queue<he>Q;
void bfs(int sx,int sy){
    int ans=10000000;
    while(!Q.empty()) Q.pop();
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++) f[i][j]=10000000;
    f[sx][sy]=0;
    Q.push((he){sx,sy,0});
    while(!Q.empty()){
        he p=Q.front();Q.pop();
        if(p.u==ex&&p.v==ey) {
            ans=min(ans,p.t);
            continue;
        }
        if(b[p.u][p.v].u!=-1){
            int x1=b[p.u][p.v].u;
            int y1=b[p.u][p.v].v;
            if(a[x1][y1]!='#'&&f[x1][y1]>p.t+3){
                f[x1][y1]=p.t+3;
                Q.push((he){x1,y1,p.t+3});
            }
        }
        for(int i=0;i<4;i++){
            int x1=p.u+dx[i];
            int y1=p.v+dy[i];
            if(x1<n&&x1>=0&&y1>=0&&y1<m&&a[x1][y1]!='#'&&f[x1][y1]>p.t+1){
                f[x1][y1]=p.t+1;
                Q.push((he){x1,y1,p.t+1});
            }
        }
    }
    if(ans!= 10000000)
    printf("%d\n",ans);else printf("-1\n");
}
int main(){
    //freopen("1.in","r",stdin);
    for(int i=0;i<=300;i++)
        for(int j=0;j<=300;j++) b[i][j].u=b[i][j].v=-1;
    while(scanf("%d%d%d",&n,&m,&q)!=EOF){
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++){
                scanf("%c",&ch);
                while(ch!='.'&&ch!='S'&&ch!='T'&&ch!='#') ch=getchar();
                a[i][j]=ch;
                if(ch=='S') sx=i,sy=j;
                if(ch=='T') ex=i,ey=j;
            }
        for(int i=1;i<=q;i++){
            scanf("%d%d",&u1[i],&v1[i]);
            scanf("%d%d",&b[u1[i]][v1[i]].u,&b[u1[i]][v1[i]].v);
        }
        bfs(sx,sy);
        for(int i=1;i<=q;i++)
            b[u1[i]][v1[i]].u=-1,b[u1[i]][v1[i]].v=-1;
    }
}*/
/*
long long int n,arr[maxn];
int vis[maxn];
long long int sum[maxn];
void init()
{
    memset(vis,0,sizeof(vis));
    memset(sum,0,sizeof(sum));
    vis[1]=1;sum[1]=0;
    vis[2]=0;sum[2]=0;
    for(int i = 1;i<sqrt(maxn);i++) {
         if(!vis[i]) {
            for(int j = 2;j*i<maxn;j++) {
                vis[i]=1;
                sum[i*j]++;
            }
         }
    }//for(int i = 1;i<100;i++)printf("%d ",sum[i]);
}
int main() {
    int T;
    init();
    while(~scanf("%d",&T))
    {
        scanf("%d",&n);
        long long int ans = 0;
        for(int i = 0;i<n;i++)
        {
            long long int val;
            scanf("%lld",&val);
            ans+=val-sum[val]-1;
            //scanf("%d",&arr[i]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

*/

/*
int arr[maxn][maxn];
int dp[maxn][maxn];
int N;
int dfs(int Curi,int Curj,int Sum)
{
    if(Curi>=N||Curj>=N)return 0;
    if(dp[Curi][Curj]!=-1)return dp[Curi][Curj];
    int tmp = Sum,temp=0;
    while(tmp) {
        if(tmp%10!=0)break;
        tmp/=10;
        temp++;
    }
    return dp[Curi][Curj]=temp+min(dfs(Curi+1,Curj,Sum*arr[Curi+1][Curj]),dfs(Curi,Curj+1,Sum*arr[Curi][Curj+1]));
}
void Print(int sum,int Nowi,int Nowj)
{
    int u = 0;
    int Tmp=sum;
    while(Tmp)
    {
        if(Tmp%10)break;
        Tmp/=10;u++;
    }
    Tmp=dp[Nowi][Nowj];
    if(dp[Nowi+1][Nowj])
}
int main() {
   // int n;
    scanf("%d",&N);
    memset(dp,-1,sizeof(dp));
    for(int i = 0;i<N;i++)
       for(int j = 0;j<N;j++) {
        scanf("%d",&arr[i][j]);
       }
    dfs(0,0,arr[0][0]);

    cout<<dp[0][0]<<endl;
    Print(arr[0][0],0,0);
    return 0;
}
*/

/*
long long int n,arr[maxn];
int vis[maxn];
long long int sum[maxn];
void init()
{
    memset(vis,0,sizeof(vis));
    memset(sum,0,sizeof(sum));
    vis[1]=1;sum[1]=0;
    vis[2]=0;sum[2]=0;
    for(int i = 1;i<sqrt(maxn);i++) {
         if(!vis[i]) {
            for(int j = 2;j*i<maxn;j++) {
                vis[i]=1;
                sum[i*j]++;
            }
         }
    }//for(int i = 1;i<100;i++)printf("%d ",sum[i]);
}
int main() {
    int T;
    init();
    while(~scanf("%d",&T))
    {
        scanf("%d",&n);
        long long int ans = 0;
        for(int i = 0;i<n;i++)
        {
            long long int val;
            scanf("%lld",&val);
            ans+=val-sum[val]-1;
            //scanf("%d",&arr[i]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

*/

/*
int arr[maxn][maxn];
int dp[maxn][maxn];
int N;
int dfs(int Curi,int Curj,int Sum)
{
    if(Curi>=N||Curj>=N)return 0;
    if(dp[Curi][Curj]!=-1)return dp[Curi][Curj];
    int tmp = Sum,temp=0;
    while(tmp) {
        if(tmp%10!=0)break;
        tmp/=10;
        temp++;
    }
    return dp[Curi][Curj]=temp+min(dfs(Curi+1,Curj,Sum*arr[Curi+1][Curj]),dfs(Curi,Curj+1,Sum*arr[Curi][Curj+1]));
}
void Print(int sum,int Nowi,int Nowj)
{
    int u = 0;
    int Tmp=sum;
    while(Tmp)
    {
        if(Tmp%10)break;
        Tmp/=10;u++;
    }
    Tmp=dp[Nowi][Nowj];
    if(dp[Nowi+1][Nowj])
}
int main() {
   // int n;
    scanf("%d",&N);
    memset(dp,-1,sizeof(dp));
    for(int i = 0;i<N;i++)
       for(int j = 0;j<N;j++) {
        scanf("%d",&arr[i][j]);
       }
    dfs(0,0,arr[0][0]);

    cout<<dp[0][0]<<endl;
    Print(arr[0][0],0,0);
    return 0;
}
*/


#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <utility>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pai;
typedef vector< pai > vpi;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define fr1(n) for(int i=0;i<n;++i)
#define fr(n,i) for(int i=0;i<n;++i)
#define tr(c) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())

int main()
{

    int n,m,p,q,sum1=1000001;
    cin>>n>>m;
    vi ar(n);
    vvi arr((n+1),vector<int>(n+1));
    fr1(n){
        cin>>ar[i];
    }
    fr1(m){
        cin>>p>>q;
        arr[p][q]=arr[q][p]=1;
    }
    for(int i=1;i<n-1;++i){
        for(int j=i+1;j<n;j++){
            for(int k=j+1;k<n+1;++k){
                if(arr[i][j] && arr[j][k] && arr[k][i]){
                    sum1=min(ar[i-1]+ar[j-1]+ar[k-1],sum1);
                }
            }
        }
    }
    if(sum1!=1000001) cout<<sum1;
    else cout<<-1;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <utility>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pai;
typedef vector< pai > vpi;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define fr1(n) for(int i=0;i<n;++i)
#define fr(n,i) for(int i=0;i<n;++i)
#define tr(c) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())

int main()
{

    int n,m,p,q;
    int sum1=1000001000;
    cin>>n>>m;
    vi ar(n);
    vvi arr((n+1),vector<int>(n+1));
    fr1(n){
        cin>>ar[i];
    }
    fr1(m){
        cin>>p>>q;
        arr[p][q]=arr[q][p]=1;
    }
    for(int i=1;i<n-1;++i){
        for(int j=i+1;j<n;j++){
            for(int k=j+1;k<n+1;++k){
                if(arr[i][j] && arr[j][k] && arr[k][i]){
                    sum1=min(ar[i-1]+ar[j-1]+ar[k-1],sum1);
                }
            }
        }
    }
    if(sum1!=1000001000) cout<<sum1;
    else cout<<-1;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <utility>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pai;
typedef vector< pai > vpi;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define fr1(n) for(int i=0;i<n;++i)
#define fr(n,i) for(int i=0;i<n;++i)
#define tr(c) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())

int main()
{

    int n,m,p,q;
    long long int sum1=1000001000;
    cin>>n>>m;
    vi ar(n);
    vvi arr((n+1),vector<int>(n+1));
    fr1(n){
        cin>>ar[i];
    }
    fr1(m){
        cin>>p>>q;
        arr[p][q]=arr[q][p]=1;
    }
    for(int i=1;i<n-1;++i){
        for(int j=i+1;j<n;j++){
            for(int k=j+1;k<n+1;++k){
                if(arr[i][j] && arr[j][k] && arr[k][i]){
                    sum1=min(ar[i-1]+ar[j-1]+ar[k-1],sum1);
                }
            }
        }
    }
    if(sum1!=1000001000) cout<<sum1;
    else cout<<-1;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <utility>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pai;
typedef vector< pai > vpi;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define fr1(n) for(int i=0;i<n;++i)
#define fr(n,i) for(int i=0;i<n;++i)
#define tr(c) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())

int main()
{

    int n,m,p,q,sum1=1000001;
    vi ar(n);
    vvi arr((n+1),vector<int>(n+1));
    cin>>n>>m;
    fr1(n){
        cin>>ar[i];
    }
    fr1(m){
        cin>>p>>q;
        arr[p][q]=arr[q][p]=1;
    }
    for(int i=1;i<n-1;++i){
        for(int j=i+1;j<n;j++){
            for(int k=j+1;k<n+1;++k){
                if(arr[i][j] && arr[j][k] && arr[k][i]){
                    sum1=min(ar[i-1]+ar[j-1]+ar[k-1],sum1);
                }
            }
        }
    }
    if(sum1!=1000001) cout<<sum1;
    else cout<<-1;
    return 0;
}


/***************************************************************************
 Md. Maruf Ur Rahman
 Dept of CSE.
 Islamic University,Bangladesh.
 ***************************************************************************/
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<int>
#define VS vector<string>
#define LL long long
#define MII map<int,int>
#define MSI map<string,int>
#define MIS map<int,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)

using namespace std;
bool is_palindrome(const string &s, int start, int end) {
 int sz = end - start + 1;
if (sz % 2 == 0) {
  int middle = start + sz/2;
    int i;
  for (i = 0; middle - 1 - i >= 0 && middle+i <= end && s[middle-1-i] == s[middle+i]; i++);
   if (middle - i - 1 < 0 || middle+i > end)
    return true;
 } else {
   int middle = start + sz/2;
int i;
 for (i = 1; middle - i >= 0 && middle+i <= end && s[middle-i]==s[middle+i]; i++);
   if (middle - i < 0 || middle+i > end)
return true;
 }
return false;
}
//Polya-Burnside theory : (n^6+3n^4+12n^3+8n^2)/24
int euclidd,euclidx,euclidy;
void extendedeuclid(int a,int b)
{
	if(b==0)
	{
		euclidd=a;
		euclidx=1;
		euclidy=0;
		return ;
	}
	extendedeuclid(b,a%b);
	int d1,x1,y1;
	d1=euclidd;
	x1=euclidx;
	y1=euclidy;
	euclidx=y1;
	euclidy=x1-((a/b)*y1);
}
double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;
}
double dist_2point(double x1,double y1,double x2,double y2)
{
    double d;
    d=sqrt(pow((x1-x2),2)+pow((y1-y2),2));
    return d;
}
int binary_search(int a[],int n,int l,int u)
{
	int mid;
  if(l>u)
	  return 0;
  mid=floor(l+u)/2;
  if(a[mid]==n)
	  return mid;
  else if(a[mid]>n)
	  binary_search(a,n,l,mid-1);
	  else
	  binary_search(a,n,mid+1,u);
}
int gcd(int a,int b)
{
while(b>0)
{
a=a%b;
a=a^b;
b=b^a;
a=a^b;
}
return a;
}
long lcm(long a,long b)
{
    long x=(a*b)/gcd(a,b);
    return x;
}
long is_prime(long n)
{
	long ii;
if (n == 1)
 return 0;
if (n == 2)
 return 1;
if (n%2 == 0)
 return 0;
for (ii=3;ii*ii<=n;ii=ii+2)
     if (n%ii == 0)
           return 0;
return 1;
}

int main()
{
ios_base::sync_with_stdio(false);
//freopen("input.txt","r",stdin);
long n,m,i,a[1005]={0},ar[1005]={0},b,c,x,t,f,sum,l;
scanf("%ld %ld",&n,&m);
for(i=1;i<=n;i++)
scanf("%ld",&a[i]);

for(i=0;i<m;i++)
{
    scanf("%ld %ld",&b,&c);
    ar[b]=c;
}
long mn=1000000005;
f=0;
int color[1005]={0};
//printf("%ld %ld %ld\n",ar[1],ar[2],ar[3]);
for(i=1;i<=n;i++)
{
    if(color[i]) continue;
    l=i;
    t=0;
    sum=a[i];
    color[i]=1;
  while(ar[l]!=i)
  {

     // printf("%ld \n",a[ar[l]]);
      sum+=a[ar[l]];
      int v=ar[l];
      color[v]=1;
     //  printf("x=%ld \n",v);
      l=v;
      t++;
      if(t>2) break;
  }
//printf("\n");
  if(t==2)
  {
      f=1;
      if(sum<mn)
         mn=sum;
      //  printf(" bbb %d\n",sum);
  }

}
if(f)
printf("%ld\n",mn);
else printf("-1\n");
return 0;
}


/***************************************************************************
 Md. Maruf Ur Rahman
 Dept of CSE.
 Islamic University,Bangladesh.
 ***************************************************************************/
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<int>
#define VS vector<string>
#define LL long long
#define MII map<int,int>
#define MSI map<string,int>
#define MIS map<int,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)

using namespace std;
bool is_palindrome(const string &s, int start, int end) {
 int sz = end - start + 1;
if (sz % 2 == 0) {
  int middle = start + sz/2;
    int i;
  for (i = 0; middle - 1 - i >= 0 && middle+i <= end && s[middle-1-i] == s[middle+i]; i++);
   if (middle - i - 1 < 0 || middle+i > end)
    return true;
 } else {
   int middle = start + sz/2;
int i;
 for (i = 1; middle - i >= 0 && middle+i <= end && s[middle-i]==s[middle+i]; i++);
   if (middle - i < 0 || middle+i > end)
return true;
 }
return false;
}
//Polya-Burnside theory : (n^6+3n^4+12n^3+8n^2)/24
int euclidd,euclidx,euclidy;
void extendedeuclid(int a,int b)
{
	if(b==0)
	{
		euclidd=a;
		euclidx=1;
		euclidy=0;
		return ;
	}
	extendedeuclid(b,a%b);
	int d1,x1,y1;
	d1=euclidd;
	x1=euclidx;
	y1=euclidy;
	euclidx=y1;
	euclidy=x1-((a/b)*y1);
}
double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;
}
double dist_2point(double x1,double y1,double x2,double y2)
{
    double d;
    d=sqrt(pow((x1-x2),2)+pow((y1-y2),2));
    return d;
}
int binary_search(int a[],int n,int l,int u)
{
	int mid;
  if(l>u)
	  return 0;
  mid=floor(l+u)/2;
  if(a[mid]==n)
	  return mid;
  else if(a[mid]>n)
	  binary_search(a,n,l,mid-1);
	  else
	  binary_search(a,n,mid+1,u);
}
int gcd(int a,int b)
{
while(b>0)
{
a=a%b;
a=a^b;
b=b^a;
a=a^b;
}
return a;
}
long lcm(long a,long b)
{
    long x=(a*b)/gcd(a,b);
    return x;
}
long is_prime(long n)
{
	long ii;
if (n == 1)
 return 0;
if (n == 2)
 return 1;
if (n%2 == 0)
 return 0;
for (ii=3;ii*ii<=n;ii=ii+2)
     if (n%ii == 0)
           return 0;
return 1;
}

int main()
{
ios_base::sync_with_stdio(false);
//freopen("input.txt","r",stdin);
long n,m,i,a[1005]={0},ar[1005]={0},b,c,x,t,f,sum,l;
scanf("%ld %ld",&n,&m);
for(i=1;i<=n;i++)
scanf("%ld",&a[i]);

for(i=0;i<m;i++)
{
    scanf("%ld %ld",&b,&c);
    ar[b]=c;
}
long mn=100000;
f=0;
int color[1005]={0};
//printf("%ld %ld %ld\n",ar[1],ar[2],ar[3]);
for(i=1;i<=n;i++)
{
    if(color[i]) continue;
    l=i;
    t=0;
    sum=a[i];
    color[i]=1;
  while(ar[l]!=i)
  {

     // printf("%ld \n",a[ar[l]]);
      sum+=a[ar[l]];
      int v=ar[l];
      color[v]=1;
     //  printf("x=%ld \n",v);
      l=v;
      t++;
      if(t>2) break;
  }
//printf("\n");
  if(t==2)
  {
      f=1;
      if(sum<mn)
         mn=sum;
      //  printf(" bbb %d\n",sum);
  }

}
if(f)
printf("%ld\n",mn);
else printf("-1\n");
return 0;
}


/***************************************************************************
 Md. Maruf Ur Rahman
 Dept of CSE.
 Islamic University,Bangladesh.
 ***************************************************************************/
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<int>
#define VS vector<string>
#define LL long long
#define MII map<int,int>
#define MSI map<string,int>
#define MIS map<int,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)

using namespace std;
bool is_palindrome(const string &s, int start, int end) {
 int sz = end - start + 1;
if (sz % 2 == 0) {
  int middle = start + sz/2;
    int i;
  for (i = 0; middle - 1 - i >= 0 && middle+i <= end && s[middle-1-i] == s[middle+i]; i++);
   if (middle - i - 1 < 0 || middle+i > end)
    return true;
 } else {
   int middle = start + sz/2;
int i;
 for (i = 1; middle - i >= 0 && middle+i <= end && s[middle-i]==s[middle+i]; i++);
   if (middle - i < 0 || middle+i > end)
return true;
 }
return false;
}
//Polya-Burnside theory : (n^6+3n^4+12n^3+8n^2)/24
int euclidd,euclidx,euclidy;
void extendedeuclid(int a,int b)
{
	if(b==0)
	{
		euclidd=a;
		euclidx=1;
		euclidy=0;
		return ;
	}
	extendedeuclid(b,a%b);
	int d1,x1,y1;
	d1=euclidd;
	x1=euclidx;
	y1=euclidy;
	euclidx=y1;
	euclidy=x1-((a/b)*y1);
}
double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;
}
double dist_2point(double x1,double y1,double x2,double y2)
{
    double d;
    d=sqrt(pow((x1-x2),2)+pow((y1-y2),2));
    return d;
}
int binary_search(int a[],int n,int l,int u)
{
	int mid;
  if(l>u)
	  return 0;
  mid=floor(l+u)/2;
  if(a[mid]==n)
	  return mid;
  else if(a[mid]>n)
	  binary_search(a,n,l,mid-1);
	  else
	  binary_search(a,n,mid+1,u);
}
int gcd(int a,int b)
{
while(b>0)
{
a=a%b;
a=a^b;
b=b^a;
a=a^b;
}
return a;
}
long lcm(long a,long b)
{
    long x=(a*b)/gcd(a,b);
    return x;
}
long is_prime(long n)
{
	long ii;
if (n == 1)
 return 0;
if (n == 2)
 return 1;
if (n%2 == 0)
 return 0;
for (ii=3;ii*ii<=n;ii=ii+2)
     if (n%ii == 0)
           return 0;
return 1;
}

int ar[1005][1005]={0};

int main()
{
ios_base::sync_with_stdio(false);
//freopen("input.txt","r",stdin);
long n,m,i,a[1005]={0},b,c,j,k,sum;
scanf("%ld %ld",&n,&m);
for(i=1;i<=n;i++)
scanf("%ld",&a[i]);

for(i=0;i<m;i++)
{
    scanf("%ld %ld",&b,&c);
    ar[b][c]=ar[c][b]=1;
}
sum=0;
long mn=1000000005;
for(i=1;i<n-1;i++)
for(j=i+1;j<n;j++)
for(k=j+1;k<=n;k++)
{
    if(ar[i][j]&&ar[j][k]&&ar[k][i])
    {
        sum=a[i]+a[j]+a[k];
        if(sum<mn)
        mn=sum;
    }
}
printf("%ld\n",mn);
return 0;
}


/***************************************************************************
 Md. Maruf Ur Rahman
 Dept of CSE.
 Islamic University,Bangladesh.
 ***************************************************************************/
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<int>
#define VS vector<string>
#define LL long long
#define MII map<int,int>
#define MSI map<string,int>
#define MIS map<int,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)

using namespace std;
bool is_palindrome(const string &s, int start, int end) {
 int sz = end - start + 1;
if (sz % 2 == 0) {
  int middle = start + sz/2;
    int i;
  for (i = 0; middle - 1 - i >= 0 && middle+i <= end && s[middle-1-i] == s[middle+i]; i++);
   if (middle - i - 1 < 0 || middle+i > end)
    return true;
 } else {
   int middle = start + sz/2;
int i;
 for (i = 1; middle - i >= 0 && middle+i <= end && s[middle-i]==s[middle+i]; i++);
   if (middle - i < 0 || middle+i > end)
return true;
 }
return false;
}
//Polya-Burnside theory : (n^6+3n^4+12n^3+8n^2)/24
int euclidd,euclidx,euclidy;
void extendedeuclid(int a,int b)
{
	if(b==0)
	{
		euclidd=a;
		euclidx=1;
		euclidy=0;
		return ;
	}
	extendedeuclid(b,a%b);
	int d1,x1,y1;
	d1=euclidd;
	x1=euclidx;
	y1=euclidy;
	euclidx=y1;
	euclidy=x1-((a/b)*y1);
}
double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;
}
double dist_2point(double x1,double y1,double x2,double y2)
{
    double d;
    d=sqrt(pow((x1-x2),2)+pow((y1-y2),2));
    return d;
}
int binary_search(int a[],int n,int l,int u)
{
	int mid;
  if(l>u)
	  return 0;
  mid=floor(l+u)/2;
  if(a[mid]==n)
	  return mid;
  else if(a[mid]>n)
	  binary_search(a,n,l,mid-1);
	  else
	  binary_search(a,n,mid+1,u);
}
int gcd(int a,int b)
{
while(b>0)
{
a=a%b;
a=a^b;
b=b^a;
a=a^b;
}
return a;
}
long lcm(long a,long b)
{
    long x=(a*b)/gcd(a,b);
    return x;
}
long is_prime(long n)
{
	long ii;
if (n == 1)
 return 0;
if (n == 2)
 return 1;
if (n%2 == 0)
 return 0;
for (ii=3;ii*ii<=n;ii=ii+2)
     if (n%ii == 0)
           return 0;
return 1;
}

int ar[1005][1005]={0};

int main()
{
ios_base::sync_with_stdio(false);
//freopen("input.txt","r",stdin);
long n,m,i,a[1005]={0},b,c,j,k,sum;
scanf("%ld %ld",&n,&m);
for(i=1;i<=n;i++)
scanf("%ld",&a[i]);

for(i=0;i<m;i++)
{
    scanf("%ld %ld",&b,&c);
    ar[b][c]=ar[c][b]=1;
}
sum=0;
long mn=1000000005;
for(i=1;i<n-1;i++)
for(j=i+1;j<n;j++)
for(k=j+1;k<=n;k++)
{
    if(ar[i][j]&&ar[j][k]&&ar[k][i])
    {
        sum=a[i]+a[j]+a[k];
        if(sum<mn)
        mn=sum;
    }
}
if(mn==1000000005)
printf("-1\n");
else
printf("%ld\n",mn);
return 0;
}


#define _USE_MATH_DEFINES

#include <math.h>
#include <stdio.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
long long n,a[1000],mins=100000000,m[10000][10000],k,e,d;
int main()
{

	cin>>n>>k;
	for (int i=0;i<n;i++)
		cin>>a[i];
	for (int i=0;i<k;i++)
	{
		cin>>d>>e;
		m[d][e]=1;
		m[e][d]=1;
	}

	for (int i=1;i<=n-2;i++)
		for (int j=i+1;j<=n-1;j++)
			for (int g=j+1;g<=n;g++)
				if ((m[i][j]==1) && (m[g][j]==1) && (m[i][g]==1) && (a[i]+a[j]+a[g]<mins))
					mins=a[i]+a[j]+a[g];
	if (mins==100000000)
		cout<<-1;
	else
		cout<<mins;
	return 0;
}


#define _USE_MATH_DEFINES

#include <math.h>
#include <stdio.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
long long n,a[100],mins=10000000,m[4450][4450],k,e,d;
int main()
{
	cin>>n>>k;
	for (int i=1;i<=n;i++)
		cin>>a[i];
	for (int i=0;i<k;i++)
	{
		cin>>d>>e;
		m[d][e]=1;
		m[e][d]=1;
	}

	for (int i=1;i<=n-2;i++)
		for (int j=i+1;j<=n-1;j++)
			for (int g=j+1;g<=n;g++)
				if ((m[i][j]==1) && (m[g][j]==1) && (m[i][g]==1) && (a[i]+a[j]+a[g]<mins))
					mins=a[i]+a[j]+a[g];
	if (mins==10000000)
		cout<<-1;
	else
		cout<<mins;
	return 0;
}


#define _USE_MATH_DEFINES

#include <math.h>
#include <stdio.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
long long n,a[101],mins=10000000,m[4450][4450],k,e,d;
int main()
{
        cin>>n>>k;
        for (int i=1;i<=n;i++)
                cin>>a[i];
        for (int i=0;i<k;i++)
        {
                cin>>d>>e;
                m[d][e]=1;
                m[e][d]=1;
        }

        for (int i=1;i<=n-2;i++)
                for (int j=i+1;j<=n-1;j++)
                        for (int g=j+1;g<=n;g++)
                                if ((m[i][j]==1) && (m[g][j]==1) && (m[i][g]==1) && (a[i]+a[j]+a[g]<mins))
                                        mins=a[i]+a[j]+a[g];
        if (mins==10000000)
                cout<<-1;
        else
                cout<<mins;
        return 0;
}


#define _USE_MATH_DEFINES

#include <math.h>
#include <stdio.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
long long n,a[100],mins=10000000,m[4450][4450],k,e,d;
int main()
{
	cin>>n>>k;
	for (int i=0;i<n;i++)
		cin>>a[i];
	for (int i=0;i<k;i++)
	{
		cin>>d>>e;
		m[d][e]=1;
		m[e][d]=1;
	}

	for (int i=1;i<=n-2;i++)
		for (int j=i+1;j<=n-1;j++)
			for (int g=j+1;g<=n;g++)
				if ((m[i][j]==1) && (m[g][j]==1) && (m[i][g]==1) && (a[i]+a[j]+a[g]<mins))
					mins=a[i]+a[j]+a[g];
	if (mins==100000000)
		cout<<-1;
	else
		cout<<mins;
	return 0;
}


#include <bits/stdc++.h>
using namespace std ;
int n , m ,x ,z;
int p[101];
int a[101][101];
int main () {
scanf ("%d %d",&n,&m);
for (int i = 1 ; i <= n ; i++)scanf(" %d",&p[i]);
for (int i = 0 ; i < m ; i++){
    scanf(" %d %d",&x,&z);
    a[x][z]=1;
    a[z][x]=1;
}
int mx = -1;
for (int i = 1 ; i < n-1; i++){
    for (int j = i+1 ; j < n ; j++){
        for (int k = j+1; k <= n; k++){
            if (a[i][j]&&a[j][k]&&a[i][k])mx=max(mx,p[i]+p[j]+p[k]);
        }
    }
}
  printf("%d\n",mx);
return 0 ;
}


#include <iostream>
#include <cstdio>
const int maxn=101;
using namespace std;
int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);

    long price[maxn],a[maxn][maxn],kq,n,m;

    cin >> n >> m;
    for (int i=1;i<=n;++i) cin >> price[i];
    int u,v;
    for (int i=1;i<=m;++i) {
        cin >> u >> v;
        a[u][v]=a[v][u]=1;
    }
    kq=-1;
    int i,j,k;
    for (i=1;i<=n-2;++i)
        for (j=i+1;j<=n-1;++j)
            for (k=j+1;k<=n;++k)
                if ( (a[i][j]) && (a[j][k]) && (a[i][k]) && ( kq==-1 || price[i]+price[j]+price[k]<kq) ) kq=price[i]+price[j]+price[k];
    cout << kq;
    return 0;
}


#include <iostream>
#include <cstdio>
const int maxn=101;
const long maxmax=1000000001;
using namespace std;
int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);

    long price[maxn],a[maxn][maxn],kq,n,m;

    scanf("%ld %ld",&n,&m);
    for (int i=1;i<=n;++i) scanf("%ld",&price[i]);
    int u,v;
    for (int i=1;i<=m;++i) {
        scanf("%d %d",&u,&v);
        a[u][v]=1;
        a[v][u]=1;
    }
    kq=maxmax;
    int i,j,k;
    for (i=1;i<=n;++i)
        for (j=1;j<=n;++j)
            for (k=1;k<=n;++k)
                if ((i!=j) && (j!=k) && (k!=i) && (a[i][j]) && (a[j][k]) && (a[k][i])) kq=min(kq,price[i]+price[j]+price[k]);
    if (kq==maxmax) cout<<-1; else cout<<kq;
    return 0;
}


#include <iostream>
#include <cstdio>
const int maxn=101;
using namespace std;

int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);

    long price[maxn],kq,n,m;
    bool a[maxn][maxn];

    cin >> n >> m;
    for (int i=1;i<=n;i++) cin >> price[i];

    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++) a[i][j]=false;
    int u,v;
    for (int i=1;i<=m;i++) {
        cin >> u >> v;
        a[u][v]=a[v][u]=true;
    }
    kq=-1;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (i!=j && a[i][j])
               for (int k=1;k<=n;k++)
                if ( k!=i && k!=j && a[j][k] && a[i][k] ) {
                   int s=price[i]+price[j]+price[k];
                   if (kq==-1 || s<kq ) kq=s;
                }
    cout << kq;
    return 0;
}


#include <iostream>
#include <cstdio>
const int maxn=101;
using namespace std;
int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);

    int price[maxn],a[maxn][maxn],kq,n,m;

    cin >> n >> m;
    for (int i=1;i<=n;++i) cin >> price[i];
    int u,v;
    for (int i=1;i<=m;++i) {
        cin >> u >> v;
        a[u][v]=a[v][u]=1;
    }
    kq=-1;
    int i,j,k;
    for (i=1;i<=n-2;++i)
        for (j=i+1;j<=n-1;++j)
            for (k=j+1;k<=n;++k)
                if ( (a[i][j]) && (a[j][k]) && (a[i][k]) && ( (kq==-1) || (price[i]+price[j]+price[k]<kq) ) ) kq=price[i]+price[j]+price[k];
    cout << kq;
    return 0;
}


#include <iostream>
#include <cstdio>
const int maxn=101;
using namespace std;
int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);

    int price[maxn],a[maxn][maxn],kq,n,m;

    cin >> n >> m;
    for (int i=1;i<=n;i++) cin >> price[i];
    int u,v;
    for (int i=1;i<=m;i++) {
        cin >> u >> v;
        a[u][v]=a[v][u]=1;
    }
    kq=-1;
    int i,j,k;
    for (i=1;i<=n-2;i++)
        for (j=i+1;j<=n-1;j++)
            for (k=j+1;k<=n;k++)
                if ( (a[i][j]) && (a[j][k]) && (a[i][k]) && ( (kq==-1) || (price[i]+price[j]+price[k]<kq) ) ) kq=price[i]+price[j]+price[k];
    cout << kq;
    return 0;
}


#include <iostream>
#include <cstdio>
const int maxn=101;
const long maxmax=1000000001;
using namespace std;
int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);

    long price[maxn],a[maxn][maxn],kq,n,m;

    scanf("%ld %ld",&n,&m);
    for (int i=1;i<=n;++i) scanf("%ld",&price[i]);
    int u,v;
    for (int i=1;i<=m;++i) {
        scanf("%d %d",&u,&v);
        a[u][v]=a[v][u]=1;
    }
    kq=-1;
    int i,j,k;
    for (i=1;i<=n-2;++i)
        for (j=i+1;j<=n-1;++j)
            for (k=j+1;k<=n;++k)
                if (((a[i][j]) && (a[j][k]) && (a[k][i]))||(kq==-1)) kq=price[i]+price[j]+price[k];
    if (kq==maxmax) cout<<-1; else cout<<kq;
    return 0;
}


#include <iostream>
#include <cstdio>
const int maxn=101;
const long maxmax=1000000001;
using namespace std;
int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);

    long price[maxn],a[maxn][maxn],kq,n,m;

    scanf("%ld %ld",&n,&m);
    for (int i=1;i<=n;++i) scanf("%ld",&price[i]);
    int u,v;
    for (int i=1;i<=m;++i) {
        scanf("%d %d",&u,&v);
        a[u][v]=1;
        a[v][u]=1;
    }
    kq=maxmax;
    int i,j,k;
    for (i=1;i<=n;++i)
        for (j=1;j<=n;++j)
            for (k=1;k<=n;++k)
                if ((a[i][j]) && (a[j][k]) && (a[k][i])) kq=min(kq,price[i]+price[j]+price[k]);
    if (kq==maxmax) cout<<-1; else cout<<kq;
    return 0;
}


#include <iostream>
#include <cstdio>
const int maxn=101;
using namespace std;
int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);

    long price[maxn],a[maxn][maxn],kq,n,m;

    cin >> n >> m;
    for (int i=1;i<=n;++i) cin >> price[i];
    int u,v;
    for (int i=1;i<=m;++i) {
        cin >> u >> v;
        a[u][v]=a[v][u]=1;
    }
    kq=-1;
    int i,j,k;
    for (i=1;i<=n-2;++i)
        for (j=i+1;j<=n-1;++j)
            for (k=j+1;k<=n;++k)
                if ( (a[i][j]) && (a[j][k]) && (a[k][i]) && ( kq==-1 || price[i]+price[j]+price[k]<kq) ) kq=price[i]+price[j]+price[k];
    cout << kq;
    return 0;
}


#include <iostream>
#include <cstdio>
const int maxn=101;
const long maxmax=1000000001;
using namespace std;
int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);

    long price[maxn],a[maxn][maxn],kq,n,m;

    scanf("%ld %ld",&n,&m);
    for (int i=1;i<=n;++i) scanf("%ld",&price[i]);
    int u,v;
    for (int i=1;i<=m;++i) {
        scanf("%d %d",&u,&v);
        a[u][v]=a[v][u]=1;
    }
    kq=-1;
    int i,j,k;
    for (i=1;i<=n-2;++i)
        for (j=i+1;j<=n-1;++j)
            for (k=j+1;k<=n;++k)
                if ( (a[i][j]) && (a[j][k]) && (a[k][i]) && ( kq==-1 || price[i]+price[j]+price[k]<kq) ) kq=price[i]+price[j]+price[k];
    if (kq==maxmax) cout<<-1; else cout<<kq;
    return 0;
}


#include <iostream>
#include <cstdio>
const int maxn=101;
using namespace std;
int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);

    long price[maxn],a[maxn][maxn],kq,n,m;

    scanf("%ld %ld",&n,&m);
    for (int i=1;i<=n;++i) scanf("%ld",&price[i]);
    int u,v;
    for (int i=1;i<=m;++i) {
        scanf("%d %d",&u,&v);
        a[u][v]=a[v][u]=1;
    }
    kq=-1;
    int i,j,k;
    for (i=1;i<=n-2;++i)
        for (j=i+1;j<=n-1;++j)
            for (k=j+1;k<=n;++k)
                if ( (a[i][j]) && (a[j][k]) && (a[k][i]) && ( kq==-1 || price[i]+price[j]+price[k]<kq) ) kq=price[i]+price[j]+price[k];
    cout<<kq;
    return 0;
}


#include <iostream>
#include <cstdio>
const int maxn=101;
using namespace std;
int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);

    int price[maxn],a[maxn][maxn],kq,n,m;

    cin >> n >> m;
    for (int i=1;i<=n;i++) cin >> price[i];
    int u,v;
    for (int i=1;i<=m;i++) {
        cin >> u >> v;
        a[u][v]=a[v][u]=1;
    }
    kq=-1;
    int i,j,k;
    for (i=1;i<=n;i++)
        for (j=1;j<=n;j++)
            if (i!=j && a[i][j])
               for (k=1;k<=n;k++)
                if ( k!=i && k!=j && a[j][k] && a[i][k] ) {
                   int s=price[i]+price[j]+price[k];
                   if (kq==-1 || s<kq ) kq=s;
                }
    cout << kq;
    return 0;
}


#include <iostream>
#include <cstdio>
const int maxn=101;
using namespace std;
int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);

    int price[maxn],kq,n,m;
    bool a[maxn][maxn]
    cin >> n >> m;
    for (int i=1;i<=n;i++) cin >> price[i];
    int u,v;
    for (int i=1;i<=m;i++) {
        cin >> u >> v;
        a[u][v]=a[v][u]=1;
    }
    kq=-1;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (i!=j && a[i][j])
               for (int k=1;k<=n;k++)
                if ( k!=i && k!=j && a[j][k] && a[i][k] ) {
                   int s=price[i]+price[j]+price[k];
                   if (kq==-1 || s<kq ) kq=s;
                }
    cout << kq;
    return 0;
}


#include <iostream>
#include <cstdio>
const int maxn=101;
using namespace std;
int price[maxn],kq,n,m;
bool a[maxn][maxn];
int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);

    cin >> n >> m;
    for (int i=1;i<=n;i++) cin >> price[i];

    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++) a[i][j]=false;
    int u,v;
    for (int i=1;i<=m;i++) {
        cin >> u >> v;
        a[u][v]=a[v][u]=true;
    }
    kq=-1;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (i!=j && a[i][j])
               for (int k=1;k<=n;k++)
                if ( k!=i && k!=j && a[j][k] && a[i][k] ) {
                   int s=price[i]+price[j]+price[k];
                   if (kq==-1 || s<kq ) kq=s;
                }
    cout << kq;
    return 0;
}


#include <iostream>
#include <cstdio>
const int maxn=101;
const long maxmax=1000000001;
using namespace std;
int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);

    long price[maxn],a[maxn][maxn],kq,n,m;

    scanf("%ld %ld",&n,&m);
    for (int i=1;i<=n;++i) scanf("%ld",&price[i]);
    int u,v;
    for (int i=1;i<=m;++i) {
        scanf("%d %d",&u,&v);
        a[u][v]=1;
        a[v][u]=1;
    }
    kq=maxmax;
    int i,j,k;
    for (i=1;i<=n-2;++i)
        for (j=i+1;j<=n-1;++j)
            for (k=j+1;k<=n;++k)
                if ((a[i][j]) && (a[j][k]) && (a[k][i])) kq=min(kq,price[i]+price[j]+price[k]);
    if (kq==maxmax) cout<<-1; else cout<<kq;
    return 0;
}


#include <iostream>
#include <cstdio>
const int maxn=101;
using namespace std;
int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);

    int price[maxn],kq,n,m;
    bool a[maxn][maxn];
    cin >> n >> m;
    for (int i=1;i<=n;i++) cin >> price[i];
    int u,v;
    for (int i=1;i<=m;i++) {
        cin >> u >> v;
        a[u][v]=a[v][u]=1;
    }
    kq=-1;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (i!=j && a[i][j])
               for (int k=1;k<=n;k++)
                if ( k!=i && k!=j && a[j][k] && a[i][k] ) {
                   int s=price[i]+price[j]+price[k];
                   if (kq==-1 || s<kq ) kq=s;
                }
    cout << kq;
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

const int MAXN = 100 + 10, MaxX = 400 * 1000;
int arr[MAXN][MAXN], p[MAXN];
long long ans[MaxX], counter = 0, maaxx = 0;
vector <int> vec[MAXN];

void T(int v)
{
	if(vec[v].size() < 2)
		return;
	for(int i = 0; i < vec[v].size(); i++)
		for(int j = 0; j < vec[v].size(); j++)
			if(arr[vec[v][i]][vec[v][j]] == 1)
			{
				ans[counter] = p[vec[v][i]] + p[vec[v][j]] + p[v];
				maaxx = max(maaxx, ans[counter]);
				counter++;
			}

}

int main()
{
	int n, m;
	cin>> n >>m;
	for(int i = 0; i < n; i++)
		cin>> p[i];
	for(int i = 0; i < m; i++)
	{
		int a, b;
		cin>> a >> b;
		a--;
		b--;
		arr[a][b] = 1;
		arr[b][a] = 1;
		vec[a].push_back(b);
		vec[b].push_back(a);
	}

	for(int i = 0; i < n; i++)
		T(i);

	for(int i = 0; i < counter; i++)
		maaxx = min(ans[i], maaxx);

	if(maaxx == 0)
		cout<< -1 << endl;
	else
		cout<< maaxx<< endl;

}


#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

const int MAXN = 100, MaxX = 400 * 1000;
int arr[MAXN][MAXN], p[MAXN], ans[MaxX], counter = 0;
vector <int> vec[MAXN];

void T(int v)
{
	if(vec[v].size() < 2)
		return;
	for(int i = 0; i < vec[v].size(); i++)
		for(int j = 0; j < vec[v].size(); j++)
			if(arr[vec[v][i]][vec[v][j]] == 1)
			{
				ans[counter] = p[vec[v][i]] + p[vec[v][j]] + p[v];
				counter++;
			}

}

int main()
{
	int n, m;
	cin>> n >>m;
	for(int i = 0; i < n; i++)
		cin>> p[i];
	for(int i = 0; i < m; i++)
	{
		int a, b;
		cin>> a >> b;
		a--;
		b--;
		arr[a][b] = 1;
		arr[b][a] = 1;
		vec[a].push_back(b);
		vec[b].push_back(a);
	}

	for(int i = 0; i < n; i++)
		T(i);

	int maxall = 0;
	for(int i = 0; i < counter; i++)
		maxall = max(ans[i], maxall);

	if(maxall == 0)
		cout<< -1 << endl;
	else
		cout<< maxall << endl;

}


#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

const int MAXN = 100, MaxX = 400 * 1000;
int arr[MAXN][MAXN], p[MAXN];
long long ans[MaxX], counter = 0, maaxx = 0;
vector <int> vec[MAXN];

void T(int v)
{
	if(vec[v].size() < 2)
		return;
	for(int i = 0; i < vec[v].size(); i++)
		for(int j = 0; j < vec[v].size(); j++)
			if(arr[vec[v][i]][vec[v][j]] == 1)
			{
				ans[counter] = p[vec[v][i]] + p[vec[v][j]] + p[v];
				maaxx = max(maaxx, ans[counter]);
				counter++;
			}

}

int main()
{
	int n, m;
	cin>> n >>m;
	for(int i = 0; i < n; i++)
		cin>> p[i];
	for(int i = 0; i < m; i++)
	{
		int a, b;
		cin>> a >> b;
		a--;
		b--;
		arr[a][b] = 1;
		arr[b][a] = 1;
		vec[a].push_back(b);
		vec[b].push_back(a);
	}

	for(int i = 0; i < n; i++)
		T(i);

	for(int i = 0; i < counter; i++)
		maaxx = min(ans[i], maaxx);

	if(maaxx == 0)
		cout<< -1 << endl;
	else
		cout<< maaxx<< endl;

}


#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

const int MAXN = 100 + 10, MaxX = 400 * 1000;
bool arr[MAXN][MAXN],  chck[MAXN][MAXN][MAXN];
int p[MAXN];
long long ans[MaxX], counter = 0, maaxx = 0;
vector <int> vec[MAXN];

//ghalat
void T(int v)
{
	if(vec[v].size() < 2)
		return;
	for(int i = 0; i < vec[v].size(); i++)
		for(int j = 0; j < vec[v].size(); j++)
			if(arr[vec[v][i]][vec[v][j]] == 1 && i != j && chck[v][vec[v][i]][vec[v][j]] != 1)
			{
				//cerr<< "v: "<< v << ", i: "<< i << ", j: "<< j<< endl;
				ans[counter] = p[vec[v][i]] + p[vec[v][j]] + p[v];
				//cerr << ans[counter] << " " << p[vec[v][i]] << " " <<  p[vec[v][j]] << endl;
				maaxx = max(maaxx, ans[counter]);
				counter++;
				chck[v][vec[v][i]][vec[v][j]] = 1;
				chck[v][vec[v][j]][vec[v][i]] = 1;
				chck[vec[v][i]][v][vec[v][j]] = 1;
				chck[vec[v][j]][v][vec[v][i]] = 1;
				chck[vec[v][i]][vec[v][j]][v] = 1;
				chck[vec[v][j]][vec[v][i]][v] = 1;
			}

}

int main()
{
	int n, m;
	cin>> n >> m;
	for(int i = 0; i < n; i++)
		cin>> p[i];
	for(int i = 0; i < m; i++)
	{
		int a, b;
		cin>> a >> b;
		a--;
		b--;
		arr[a][b] = 1;
		arr[b][a] = 1;
		vec[a].push_back(b);
		vec[b].push_back(a);

	}

	for(int i = 0; i < n; i++)
		T(i);

	for(int i = 0; i < counter; i++)
		maaxx = min(ans[i], maaxx);

	if(maaxx == 0)
		cout<< -1 << endl;
	else
		cout<< maaxx<< endl;

}


#include<bits/stdc++.h>
#define MAX 10000
#define pb push_back
#define ellapse printf("Time : %0.3lf\n",clock()*1.0/CLOCKS_PER_SEC);
using namespace std;
typedef long long l64d;
typedef unsigned long int ud;
typedef vector<int> vi;
typedef vector<string> vs;
const l64d inf=10000000;
map<int,vi> match;
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n,a,b;
    l64d m,minprice=0;
    cin>>n>>m;
    l64d price[n];
    for(int i=1;i<n+1;++i) cin>>price[i];
    while(m--)
    {
        cin>>a>>b;
        match[a].pb(b);
        match[b].pb(a);
    }
    for(int i=1;i<n+1;++i)
    {
        //cout<<i<<"-->";
        for(int j=0;j<match[i].size();++j)
        {
            for(int k=0;k<match[match[i][j]].size();++k)
            {
                for(int l=0;l<match[match[match[i][j]][k]].size();++l)
                {
                    if(match[match[match[i][j]][k]][l]==i)
                    {
                        if(!minprice) minprice=price[i]+price[match[i][j]]+price[match[match[i][j]][k]];
                        else minprice=min(minprice,price[i]+price[match[i][j]]+price[match[match[i][j]][k]]);
                    }
                }
            }
        }
        /*if(match[match[match[i]]]==i)
        {
            if(!minprice) minprice=price[i]+price[match[i]]+price[match[match[i]]];
            else minprice=min(minprice,price[i]+price[match[i]]+price[match[match[i]]]);
        }*/
    }
    if(!minprice)
    {
        cout<<-1<<endl;
        return 0;
    }
    cout<<minprice<<endl;

}


#include<bits/stdc++.h>
#define MAX 10000
#define pb push_back
#define ellapse printf("Time : %0.3lf\n",clock()*1.0/CLOCKS_PER_SEC);
using namespace std;
typedef long long l64d;
typedef unsigned long int ud;
typedef vector<int> vi;
typedef vector<string> vs;
const l64d inf=10000000;
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n,a,b;
    l64d m,minprice=0;
    cin>>n>>m;
    l64d price[n+1];
    bool match[101][101]={0};
    for(int i=1;i<n+1;++i) cin>>price[i];
    while(m--)
    {
        cin>>a>>b;
        match[a][b]=1;
        match[b][a]=1;
    }
    for(int i=1;i<n+1;++i)
        for(int j=i+1;j<n+1;++j)
            for(int k=j+1;k<n+1;++k)
                if(match[i][j] && match[j][k] && match[k][i])
                {
                    if(!minprice) minprice=price[i]+price[j]+price[k];
                    else minprice=min(minprice,price[i]+price[j]+price[k]);
                }
    if(!minprice)
    {
        cout<<-1<<endl;
        return 0;
    }
    cout<<minprice<<endl;

}


#include <iostream>
using namespace std;
struct race
{
	int l,r,c,t;
};
race v[110];
int main()
{
	int n,m;
	cin >> n >> m;
	for(int i = 1;i <= m;i++)
		cin >> v[i].l >> v[i].r >> v[i].t >> v[i].c;

	int ans = 0;
	for(int j = 1;j <= n;j++)
	{
		int m1 = 999999;
		int p = 0;
		for(int i = 1;i <= m;i++)
		{
			if(v[i].l <= j && v[i].r >= j)
			{
				if(v[i].t < m1)
				{
					p = v[i].c;
					m1 = v[i].t;
				}
			}
		}
		ans += p;
	}
	cout << ans << endl;
}


#include <cmath>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<cctype>
#include <iomanip>
#include <string>
#include <sstream>
#include <functional>
#include <numeric>
#include <stack>
#include <climits>

using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define f(i,a,b) for(int i=a;i<b;i++)
#define F(i,a,b) for(int i=a;i>=    b;i--)
#define sz(a) ((int)a.size())
#define all(c) c.begin(), c.end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

typedef long long ll;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pair<int, int> > vpii;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef pair<int, int> pii;
typedef pair<string, int> psi;

template <class T> const T& max(const T& a, const T&b, const T&c) { return max(a, max(b, c)); }

template <class T> const T& min(const T& a, const T&b, const T&c) { return min(a, min(b, c)); }

ll stoi(string &str) { istringstream ss(str); ll i; ss >> i;  return i; }

string itos(ll i) { stringstream ss; ss << i;   return ss.str(); }

ll gcd(ll a, ll b){ if (b == 0) return a; return gcd(b, a % b); }

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

ll poww(ll a, ll b) { ll res = 1;   for (int i = 1; i <= b; i++) res *= a;  return res; }

ll sumOfDigs(string s) { ll sum = 0; for (int i = 0; i < s.length(); i++) sum += s[i] - '0'; return sum; }

ll sumOfDigs(ll n) { return (n < 10 ? n : n % 10 + sumOfDigs(n / 10)); }

ll mod = poww(10, 9) + 7;

using namespace std;

#define PI 3.14159265358979323846

#define N 1000000

pii a[N];

int main() {

	ios_base::sync_with_stdio(0); cin.tie(0);

#ifndef ONLINE_JUDGE
	freopen("test.txt", "r", stdin);
#endif

	int n, m;
	cin >> n >> m;
	vi v;
	v.pb(0);
	for (int i = 1; i <= n; i++) a[i].ff = INT_MAX;

	for (int i = 1; i <= m; i++) {

		int l, r, t, c;
		cin >> l >> r >> t >> c;
		v.pb(c);
		for (int j = l; j <= r; j++) {

			if (a[j].ff > t) { a[j].ss = i; a[j].ff = t; }
			//else if (a[j].ff == t && c > v[a[j].ss]) { a[j].ss = i }
		}

		//for (int j = 1; j <= n; j++) cout << a[j].ss << " ";
		//cout << endl;
	}

	ll sum = 0;
	for (int i = 1; i <= n; i++) sum += v[a[i].ss];
	cout << sum << endl;
	return 0;
}


#include <cmath>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<cctype>
#include <iomanip>
#include <string>
#include <sstream>
#include <functional>
#include <numeric>
#include <stack>
#include <climits>

using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define f(i,a,b) for(int i=a;i<b;i++)
#define F(i,a,b) for(int i=a;i>=    b;i--)
#define sz(a) ((int)a.size())
#define all(c) c.begin(), c.end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

typedef long long ll;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pair<int, int> > vpii;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef pair<int, int> pii;
typedef pair<string, int> psi;

template <class T> const T& max(const T& a, const T&b, const T&c) { return max(a, max(b, c)); }

template <class T> const T& min(const T& a, const T&b, const T&c) { return min(a, min(b, c)); }

ll stoi(string &str) { istringstream ss(str); ll i; ss >> i;  return i; }

string itos(ll i) { stringstream ss; ss << i;   return ss.str(); }

ll gcd(ll a, ll b){ if (b == 0) return a; return gcd(b, a % b); }

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

ll poww(ll a, ll b) { ll res = 1;   for (int i = 1; i <= b; i++) res *= a;  return res; }

ll sumOfDigs(string s) { ll sum = 0; for (int i = 0; i < s.length(); i++) sum += s[i] - '0'; return sum; }

ll sumOfDigs(ll n) { return (n < 10 ? n : n % 10 + sumOfDigs(n / 10)); }

ll mod = poww(10, 9) + 7;

using namespace std;

#define PI 3.14159265358979323846

#define N 1000000

pii a[N];

int main() {

	ios_base::sync_with_stdio(0); cin.tie(0);

#ifndef ONLINE_JUDGE
	freopen("test.txt", "r", stdin);
#endif

	int n, m;
	cin >> n >> m;
	vi v;
	v.pb(0);
	for (int i = 1; i <= n; i++) a[i].ff = INT_MAX;

	for (int i = 1; i <= m; i++) {

		int l, r, t, c;
		cin >> l >> r >> t >> c;
		v.pb(c);
		for (int j = l; j <= r; j++) {

			if (a[j].ff > t) { a[j].ss = i; a[j].ff = t; }
			else if (a[j].ff == t && c > v[a[j].ss]) { a[j].ss = i; }
		}

		//for (int j = 1; j <= n; j++) cout << a[j].ss << " ";
		//cout << endl;
	}

	ll sum = 0;
	for (int i = 1; i <= n; i++) sum += v[a[i].ss];
	cout << sum << endl;
	return 0;
}


#include <iostream>
using namespace std;

int main() {
	int n, m, l, r, t, c, i, k, ans = 0, time[1010], cost[1010], indx[1010];
	cin >> n >> m;

	for (i = 1; i <= n; ++i)
		time[i] = 1e9;

	for (i = 1; i <= m; ++i) {
		cin >> l >> r >> t >> c;

		for (k = l; k <= r; ++k) {
			if (time[k] > t) {
				time[k] = t;
				cost[k] = c;
				indx[k] = i;
			}
		}
	}

	for (i = 1; i <= n; ++i)
		ans += cost[i];

	cout << ans << endl;
	return 0;
}


#include <iostream>
using namespace std;

int main() {
	int n, m, l, r, t, c, i, k, ans = 0, time[1010], cost[1010], indx[1010];
	cin >> n >> m;

	for (i = 1; i <= n; ++i)
		time[i] = 1e9;

	for (i = 1; i <= m; ++i) {
		cin >> l >> r >> t >> c;

		for (k = l; k <= r; ++k) {
			if (time[k] > t) {
				time[k] = t;
				cost[k] = c;
				indx[k] = i;
			}
			else if (time[k] == t && cost[k] < c) {
				cost[k] = c;
				indx[k] = i;
			}
		}
	}

	for (i = 1; i <= n; ++i)
		ans += cost[i];

	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FAST std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define endl '\n'
#define pb push_back
signed main()
{
    FAST
    int n,m;
    cin>>n>>m;
    int col=n,row=m;

    int a[row][col];
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
            a[i][j]=1001;
    int left,right,time,paise;

    std::vector<int> pa;
    for(int i=0;i<m;i++)
    {
        cin>>left>>right>>time>>paise;
        pa.pb(paise);
        for(int j=left-1;j<=right-1;j++)
            a[i][j]=time;
    }
    int profit=0;
    for(int i=0;i<col;i++)
    {
        int mini=1001,rowneeded;
        for(int j=0;j<row;j++)
        {
            if(a[j][i]<mini) {mini=a[j][i];rowneeded=j;}
        }
        if(mini==1001) continue;
        profit+=pa[rowneeded];
    }
    cout<<profit;
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FastIO      ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0)
#define pb          push_back
#define siz         100009
#define inf         1000000
#define mp          make_pair
#define rep(i,n)    for(int i=0;i<n;i++)
#define repo(i,m,n) for(int i=m;i<=n;i++)
#define ll          long long
#define pii         pair<int,int >
#define sc(n)       scanf("%d",&n)
#define sc2(m,n)    scanf("%d%d",&m,&n);
#define MOD         1000000007
#define fileout     freopen("output.txt","w",stdout)
#define filein      freopen("input.txt","r",stdin)
#define mem(x,i)    memset(x,i,sizeof x)
#define PI          acos(-1.0)
#define ff          first
#define ss          second
#define all(x)      x.begin(),x.end()
#define Case(t)     for(int ks=1;ks<=t;ks++)
#define MAXN 100004
#define MAXLG 17
using namespace __gnu_pbds;
using namespace std;
template <typename T>  using orderedSet =
    tree<T, null_type, less<T>,
    rb_tree_tag, tree_order_statistics_node_update>;//find_by_order,order_of_key
/*------------------------------Graph Moves----------------------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

int main()
{
    int n,m;
    cin>>n>>m;
    int sec[n+1];
    rep(i,n+1) sec[i]=inf;
    int misec[n+1]={0};
    for(int i=0;i<m;i++)
    {
        int l,r,t,c;
        cin>>l>>r>>t>>c;
        for(int j=l;j<=r;j++)
        {
            if(sec[j]>t)
            {
                sec[j]=t;
                misec[j]=c;
            }
            if(sec[j]==t)
            {
                misec[j]=max(misec[j],c);
            }
        }
    }
    cout<<accumulate(misec,misec+n+1,0);

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FastIO      ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0)
#define pb          push_back
#define siz         100009
#define inf         1000000
#define mp          make_pair
#define rep(i,n)    for(int i=0;i<n;i++)
#define repo(i,m,n) for(int i=m;i<=n;i++)
#define ll          long long
#define pii         pair<int,int >
#define sc(n)       scanf("%d",&n)
#define sc2(m,n)    scanf("%d%d",&m,&n);
#define MOD         1000000007
#define fileout     freopen("output.txt","w",stdout)
#define filein      freopen("input.txt","r",stdin)
#define mem(x,i)    memset(x,i,sizeof x)
#define PI          acos(-1.0)
#define ff          first
#define ss          second
#define all(x)      x.begin(),x.end()
#define Case(t)     for(int ks=1;ks<=t;ks++)
#define MAXN 100004
#define MAXLG 17
using namespace __gnu_pbds;
using namespace std;
template <typename T>  using orderedSet =
    tree<T, null_type, less<T>,
    rb_tree_tag, tree_order_statistics_node_update>;//find_by_order,order_of_key
/*------------------------------Graph Moves----------------------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

int main()
{
    int n,m;
    cin>>n>>m;
    int sec[n+1];
    rep(i,n+1) sec[i]=inf;
    int misec[n+1]={0};
    for(int i=0;i<m;i++)
    {
        int l,r,t,c;
        cin>>l>>r>>t>>c;
        for(int j=l;j<=r;j++)
        {
            if(sec[j]>t)
            {
                sec[j]=t;
                misec[j]=c;
            }
            /*if(sec[j]==t)
            {
                misec[j]=max(misec[j],c);
            }*/
        }
    }
    cout<<accumulate(misec,misec+n+1,0);

}


#include <bits/stdc++.h>

#define endl "\n"
#define Ios ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#define fi first
#define se second
#define setp(a)  fixed << setprecision(a)
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin() , x.end()
#define run(x , y) set<int>::iterator y = x.begin(); y != x.end(); ++y

#define Vll vector < ll >
#define Vi vector < int >
#define ll long long
#define ull unsigned long long

using namespace std;

const double pi = 3.14159265358979323846;
const ll Mod = (ll) 1e9 + 7;
const int Mass = (int) 2e5;
const int pr = 37;

pair <int , int> ans[150];
int Ans;
int n , m;

int main(){ Ios
	#ifdef Home
		freopen(".in", "r", stdin);
    	freopen(".out", "w", stdout);
  	#endif
  	cin >> n >> m;
  	for (int i = 1; i <= n; i++) {
  	 	ans[i].fi = 9999;
  	}
  	for (int i = 1; i <= m; i++) {
  		int l , r , t , x;
  		cin >> l >> r >> t >> x;
  		for (int s = l; s <= r; s++) {
  			if (ans[s].fi > t) {
  				ans[s].fi = t;
  				ans[s].se = x;
  			}
  			else if (ans[s].fi == t) {
  			    if (ans[s].se < x) {
  			     	ans[s].se = x;
  			    }
  			}
  		}
   	}
   	for (int i = 1; i <= n; i++) {
   	 /*	if (ans[i].fi == 9999)
   	 		continue;
   	 */
   		Ans += ans[i].se;
   	}
   	cout << Ans;
  	return 0;
}


#include <bits/stdc++.h>

#define endl "\n"
#define Ios ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#define fi first
#define se second
#define setp(a)  fixed << setprecision(a)
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin() , x.end()
#define run(x , y) set<int>::iterator y = x.begin(); y != x.end(); ++y

#define Vll vector < ll >
#define Vi vector < int >
#define ll long long
#define ull unsigned long long

using namespace std;

const double pi = 3.14159265358979323846;
const ll Mod = (ll) 1e9 + 7;
const int Mass = (int) 2e5;
const int pr = 37;

pair <int , int> ans[150];
int Ans;
int n , m;

int main(){ Ios
	#ifdef Home
		freopen(".in", "r", stdin);
    	freopen(".out", "w", stdout);
  	#endif
  	cin >> n >> m;
  	for (int i = 1; i <= n; i++) {
  	 	ans[i].fi = 9999;
  	 	ans[i].se = -9999;
  	}
  	for (int i = 1; i <= m; i++) {
  		int l , r , t , x;
  		cin >> l >> r >> t >> x;
  		for (int s = l; s <= r; s++) {
  			if (ans[s].fi > t) {
  				ans[s].fi = t;
  				ans[s].se = x;
  			}
  			else if (ans[s].fi == t) {
  			    if (ans[s].se < x) {
  			     	ans[s].se = x;
  			    }
  			}
  		}
   	}
   	for (int i = 1; i <= n; i++) {
   	 	if (ans[i].fi == 9999)
   	 		continue;
   	 	Ans += ans[i].se;
   	}
   	cout << Ans;
  	return 0;
}


#include <bits/stdc++.h>

#define endl "\n"
#define Ios ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#define fi first
#define se second
#define setp(a)  fixed << setprecision(a)
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin() , x.end()
#define run(x , y) set<int>::iterator y = x.begin(); y != x.end(); ++y

#define Vll vector < ll >
#define Vi vector < int >
#define ll long long
#define ull unsigned long long

using namespace std;

const double pi = 3.14159265358979323846;
const ll Mod = (ll) 1e9 + 7;
const int Mass = (int) 2e5;
const int pr = 37;

pair <int , int> ans[150];
int Ans;
int n , m;

int main(){ Ios
	#ifdef Home
		freopen(".in", "r", stdin);
    	freopen(".out", "w", stdout);
  	#endif
  	cin >> n >> m;
  	for (int i = 1; i <= n; i++) {
  	 	ans[i].fi = 9999;
  	 	ans[i].se = -9999;
  	}
  	for (int i = 1; i <= m; i++) {
  		int l , r , t , x;
  		cin >> l >> r >> t >> x;
  		for (int s = l; s <= r; s++) {
  			if (ans[s].fi > t) {
  				ans[s].fi = t;
  				ans[s].se = x;
  			}
  			else if (ans[s].fi == t) {
  			    if (ans[s].se < x) {
  			     	ans[s].se = x;
  			    }
  			}
  		}
   	}
   	for (int i = 1; i <= n; i++) {
   	 /*	if (ans[i].fi == 9999)
   	 		continue;
   	 */
   		Ans += ans[i].se;
   	}
   	cout << Ans;
  	return 0;
}