#include <iostream>
#include <vector>
#include <climits>

int main(){
  int a[2];
  while(std::cin >> a[0] >> a[1]){
    int i,j;
    int city[a[0]][a[0]];
    int Tcity[a[0]];
    int cost = 0;
    for(i=0; i<a[0]; i++){
      Tcity[i] = 0;
      for(j=0; j<a[0]; j++){
        city[i][j] = INT_MAX;
      }
    }
    std::vector<std::vector<int> > v;
    for(i=0; i<a[1]; i++){
      std::vector<int> w;
      int s,g,c;
      std::cin >> s >> g >>c;
      city[s][g] = c;
      w.push_back(s);
      w.push_back(g);
      v.push_back(w);
    }
    Tcity[0] = 1;
    while(1){
      int min = INT_MAX;
      int min_s,min_g;
      for(i=0; i<a[0]; i++){
        if(Tcity[i] == 1){
          for(j=0; j<a[0]; j++){
            if(city[i][j] < min){
              min = city[i][j];
              min_s = i;
              min_g = j;
            }
          }
        }
      }
      Tcity[min_s] = 1;
      Tcity[min_g] = 1;
      cost += min;
      int loop = 1;
      for(j=0; j<a[0]; j++){
        if(Tcity[j] == 0) loop = 0;
      }
      if(loop){
        std::cout << cost << std::endl;
        break;
      }
    }
  }
  return 0;
}