#include <bits/stdc++.h>
using namespace std;
struct edge {
  int x, y, cst;
} e[300005];
struct version {
  int root, osz, ch, dc;
};
int n, m, odd;
int ans[300005], dad[300005], sz[300005], es[300005], ind[300005];
vector<version> ver;
void restore(int upto) {
  while (((int)ver.size()) > upto) {
    version cur = ver.back();
    ver.pop_back();
    dad[cur.root] = cur.root;
    sz[cur.root] = cur.osz;
    dad[cur.ch] = cur.ch;
    odd += cur.dc;
  }
}
void add(int ed) {
  int a = e[ed].x;
  int b = e[ed].y;
  while (a != dad[a]) a = dad[a];
  while (b != dad[b]) b = dad[b];
  if (a == b) return;
  if (sz[a] < sz[b]) swap(a, b);
  int dc = (sz[a] % 2 && sz[b] % 2) << 1;
  odd -= dc;
  ver.push_back({a, sz[a], b, dc});
  sz[a] += sz[b];
  dad[b] = a;
}
void go(int l, int r, int lmost, int rmost) {
  if (l > r) return;
  int mid = (l + r) >> 1;
  int res = -1;
  int last = ((int)ver.size());
  for (int i = l; i <= mid; i++) {
    if (ind[i] < lmost) add(i);
  }
  for (int i = lmost; i <= rmost; i++) {
    if (es[i] <= mid) add(es[i]);
    if (odd == 0) {
      res = i;
      break;
    }
  }
  restore(last);
  if (res == -1) {
    for (int i = l; i <= mid; i++) ans[i] = -1;
    for (int i = l; i <= mid; i++) {
      if (ind[i] < lmost) add(i);
    }
    go(mid + 1, r, lmost, rmost);
    restore(last);
    return;
  }
  ans[mid] = e[es[res]].cst;
  for (int i = l; i <= mid; i++) {
    if (ind[i] < lmost) add(i);
  }
  go(mid + 1, r, lmost, res);
  restore(last);
  for (int i = lmost; i < res; i++) {
    if (es[i] <= l) add(es[i]);
  }
  go(l, mid - 1, res, rmost);
  restore(last);
}
int main(int argc, char* argv[]) {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d %d", &e[i].x, &e[i].y, &e[i].cst);
    es[i] = i;
  }
  sort(es + 1, es + 1 + m, [](int x, int y) { return e[x].cst < e[y].cst; });
  for (int i = 1; i <= m; i++) ind[es[i]] = i;
  for (int i = 1; i <= n; i++) dad[i] = i, sz[i] = 1;
  odd = n;
  go(1, m, 1, m);
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
