// 1 based Graph
  public void solve(InputReader in, PrintWriter out) {
    nodes = in.nextInt();
    int noOfEdges = in.nextInt();

    readGraph(noOfEdges, in);

    int minSpanCost = getMinSpanTreeCost();

    out.println(minSpanCost);
  }
  private void readGraph(int noOfEdges, InputReader in) {
    List<Node>[] adjacencyList = new ArrayList[nodes + 1];
    graph = new Graph(adjacencyList);

    for (int i = 0; i < noOfEdges; i++) {
      int fromVertex = in.nextInt();
      int toVertex = in.nextInt();
      int cost = in.nextInt();

      // this is undirected graph, so add for both the side.
      Node fromNode = new Node(fromVertex, cost);
      Node toNode = new Node(toVertex, cost);

      graph.addNeighbour(fromNode, toNode);
      graph.addNeighbour(toNode, fromNode);
    }
  }