#include<iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Edge
{
int a;
int b;
int cost;
};
vector<Edge> g;
vector <int> arr;
int comp(Edge a, Edge b)
{
return a.cost < b.cost;
}
int find(int v)
{
if (arr[v] == v)
return v;
else
{
arr[v] = find(arr[v]);
return arr[v];
}
}
void unite(int a, int b)
{
if (rand() % 2 == 0)
swap(a, b);
int pA = find(a);
int pB = find(b);
if (pA != pB)
{
arr[pA] = pB;
}
}
int main()
{
int n, m;
cin >> n >> m;
int a, b, cost;
for (int i = 0; i < m; ++i)
{
cin >> a >> b >> cost;
Edge temp;
a--;
b--;
temp.a = a;
temp.b = b;
temp.cost = cost;
g.push_back(temp);
}
vector<Edge> ans;
for (int i = 0; i < n; ++i)
{
arr.push_back(i);
}
sort(g.begin(), g.end(), comp);
int c = 0;
for (int i = 0; i < m; ++i)
{
if (find(g[i].a) != find(g[i].b))
{
ans.push_back(g[i]);
c += g[i].cost;
unite(g[i].a, g[i].b);
}
}
int c2 = 0;
int s = 600000;
for (int j = 0; j < n - 1; ++j)
{
for (int i = 0; i < n; ++i)
{
arr[i] = i;
}
int k = 0;
c2 = 0;
for (int i = 0; i < m; ++i)
{
if (ans[j].a != g[i].a && ans[j].b != g[i].b)
{
if (find(g[i].a) != find(g[i].b))
{
c2 += g[i].cost;
unite(g[i].a, g[i].b);
k++;
}
if (c2 < s && k == n - 1)
s = c2;
}
}
}
cout << c << endl;
if (s != 600000)
cout << c2;
return 0;
}