#include <bits/stdc++.h>
using namespace std;
int cost[100005] , parent[100005];
int find1( int a )
{
//a and parent of a are not equal that means that a's is not root and we
//need to find the root parent of a and assign a to it's root parent inorder
//decrease the height of the tree.
if( a!=parent[a])
{
//we will find the root parent and assign it the parent of a
parent[a]=find1(parent[a]);
}
return parent[a];
}
void union1( int a , int b , int rank[])
{
int pa , pb;
pa=find1(a); //getting root parent of a
pb=find1(b); //getting root parent of b
if( pa==pb )
{
//if both the element are in the same tree/ set.
return;
}
if( rank[pa] > rank[pb] )
{
//if rank of one tree is more than the rank of other we attach the smaller
//tree to the bigger tree and there is no change in the rank because there
//is no increase in the height of the tree.
parent[pb]=pa;
}
else
{
parent[pa]=pb;
if( rank[pa]==rank[pb] )
{
// if rank of both the trees are equal there is change in height of the
//tree so we increase the rank by 1.
rank[pb]++;
}
}
}
void find2( int x )
{
int root_parent;
//finding the root parent of x and also making ever other element in the way
// to point to root parent
root_parent=find1(x);
// value of root parent is more than an element we interchange the value only
//if the value of element is >=0 OR if the value of root parent is <0 and
//value of an element is >=0 then we interchange the value
if( (cost[x]<cost[root_parent] && cost[x]>=0) || (cost[root_parent]<0 && cost[x]>=0))
{
cost[root_parent]=cost[x];
}
}
int main() {
// your code goes here
std::ios_base::sync_with_stdio(false);
int n , m , a , b, min=100005 , sum=0 , count=0 , j=1;
cin>>n>>m; //taking n and m as input
int rank[100005];
bool vis[n+1];
for( int i=1 ; i<=n ; i++)
{
//assigning initial value to rank , parent and visited array
rank[i]=0;
parent[i]=i;
vis[i]=false;
}
for( int i=1 ; i<=m ; i++)
{
cin>>a>>b;
//making a and b in the same set / tree.
union1(a,b,rank);
}
for( int i=1 ; i<=n ; i++)
{
//taking cost of each portal
cin>>cost[i];
}
for( int i=1 ; i<=n ; i++)
{
//making the each element to point to it's root parent and no other element
//so that we can have a uniform parent array pointing only to it's root
//parent(reason is ahead)
find2(i);
}
for( int i=1 ; i<=n ; i++)
{
//i m visiting root parent of n different disjoint sets once and
//checking the minimum value of the root parent if any of the root
//value is <0 and more than 1 disjoint set is formed then the output
//will be -1 otherwise the output can 0 or sum+(count-2)*min .
if(vis[parent[i]] == false)
{
//cout<<1;
if( cost[parent[i]]<0 )
{
j=0;
}
sum+=cost[parent[i]];
vis[parent[i]]=true;
if(cost[parent[i]]<min )
{
min=cost[parent[i]];
}
count++;
}
}
//cout<<count<<" ";
if(count!=1)
{
if(j==0)
{
cout<<"-1\n";
}
else
{
count=(count-2)*min;
cout<<sum+count<<"\n";
}
}
else
{
cout<<"0\n";
}
return 0;
}