#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
const int MAX_N = 100011;
const int MAX_M = 300011;
class EDGE {
 public:
  EDGE() {}
  EDGE(int a, int b, int v, int i) : a(a), b(b), v(v), i(i) {}
  int a, b, v, i;
};
int n, m;
vector<EDGE> edges;
int vorder[MAX_M];
int sol[MAX_M];
int pr[MAX_N];
int cnt[MAX_N], ocnt;
typedef vector<pair<int *, int> > TR;
void reverse(TR &t) {
  for (int i = 0; i < (int)t.size(); i++) {
    *t[i].first = t[i].second;
  }
}
void reverse(vector<TR> &trs) {
  for (int i = ((int)trs.size()) - 1; i >= 0; i--) {
    reverse(trs[i]);
  }
  trs.clear();
}
void changeTR(TR &t, int &a, int b) {
  t.push_back(make_pair(&a, a));
  a = b;
}
int getRoot(int a) {
  while (pr[a] != -1) a = pr[a];
  return a;
}
void conn(TR &t, int x, int y) {
  x = getRoot(x);
  y = getRoot(y);
  if (x == y) return;
  if (cnt[x] < cnt[y]) {
    int t = x;
    x = y;
    y = t;
  }
  if (cnt[x] % 2 == 1 && cnt[y] % 2 == 1) {
    changeTR(t, ocnt, ocnt - 2);
  }
  changeTR(t, pr[y], x);
  changeTR(t, cnt[x], cnt[x] + cnt[y]);
}
TR moveA(int &a, int &b) {
  TR tr;
  if (vorder[a] < b) {
    conn(tr, edges[vorder[a]].a, edges[vorder[a]].b);
  }
  changeTR(tr, a, a + 1);
  return tr;
}
TR moveB(int &a, int &b) {
  TR tr;
  if (edges[b].i < a) {
    conn(tr, edges[b].a, edges[b].b);
  }
  changeTR(tr, b, b + 1);
  return tr;
}
void solve(int a, int b, int na, int nb) {
  int sb = -1;
  int ma = (a + na + 1) / 2;
  vector<TR> trs;
  while (a < ma) trs.push_back(moveA(a, b));
  if (a < na) {
    TR move = moveA(a, b);
    solve(a, b, na, nb);
    reverse(move);
  }
  while (b < nb) {
    if (ocnt == 0) break;
    trs.push_back(moveB(a, b));
  }
  if (ocnt == 0) {
    if (a > 0 && b > 0) {
      sol[a - 1] = edges[b - 1].v;
    }
    sb = b;
  }
  reverse(trs);
  if (a < ma && sb != -1) {
    while (b < sb) {
      trs.push_back(moveB(a, b));
    }
    solve(a, b, ma - 1, nb);
    reverse(trs);
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    edges.push_back(EDGE(a, b, c, i));
  }
  sort(edges.begin(), edges.end(),
       [](const EDGE &a, const EDGE &b) -> bool { return a.v < b.v; });
  for (int i = 0; i < m; i++) {
    vorder[edges[i].i] = i;
    sol[i] = -1;
  }
  for (int i = 0; i < n; i++) {
    cnt[i] = 1;
    ocnt++;
    pr[i] = -1;
  }
  solve(0, 0, m, m);
  for (int i = 0; i < m; i++) {
    printf("%d\n", sol[i]);
  }
  return 0;
}
