#include <bits/stdc++.h>
using namespace std;
const int M = 5e5 + 10;
struct node {
  int x, y, v, id;
  bool operator<(const node &a) const { return v < a.v; }
} e[M];
struct LCT_bao {
  int fa[M], siz[M], vsiz[M], son[M][2], rev[M], mxx_id[M], val[M];
  void pushup(int x) {
    siz[x] = vsiz[x] + siz[son[x][0]] + siz[son[x][1]] + 1;
    if (val[x] > val[mxx_id[son[x][0]]] && val[x] > val[mxx_id[son[x][1]]])
      mxx_id[x] = x;
    else if (val[mxx_id[son[x][0]]] > val[mxx_id[son[x][1]]])
      mxx_id[x] = mxx_id[son[x][0]];
    else
      mxx_id[x] = mxx_id[son[x][1]];
    return;
  }
  void pushdown(int x) {
    if (rev[x]) {
      if (son[x][0])
        swap(son[son[x][0]][0], son[son[x][0]][1]), rev[son[x][0]] ^= 1;
      if (son[x][1])
        swap(son[son[x][1]][0], son[son[x][1]][1]), rev[son[x][1]] ^= 1;
      rev[x] = 0;
    }
    return;
  }
  bool check(int x) { return son[fa[x]][0] == x || son[fa[x]][1] == x; }
  void rotate(int x) {
    int f1 = fa[x], f2 = fa[f1];
    int w1 = (son[f1][1] == x);
    int w2 = (son[f2][1] == f1);
    int w3 = son[x][!w1];
    if (check(f1)) son[f2][w2] = x;
    son[x][!w1] = f1;
    son[f1][w1] = w3;
    if (w3) fa[w3] = f1;
    fa[f1] = x;
    fa[x] = f2;
    pushup(f1);
    pushup(x);
    return;
  }
  int sta[M], top;
  void splay(int x) {
    sta[++top] = x;
    int x1 = x;
    while (check(x1)) {
      sta[++top] = fa[x1];
      x1 = fa[x1];
    }
    while (top) pushdown(sta[top]), top--;
    while (check(x)) {
      int f1 = fa[x];
      if (check(f1))
        if ((son[fa[f1]][0] == f1 && son[f1][0] == x) ||
            (son[fa[f1]][1] == f1 && son[f1][1] == x))
          rotate(f1);
        else
          rotate(x);
      rotate(x);
    }
    return;
  }
  void access(int x) {
    int last = 0;
    while (x) {
      splay(x);
      vsiz[x] += siz[son[x][1]];
      son[x][1] = last;
      vsiz[x] -= siz[last];
      pushup(x);
      last = x;
      x = fa[x];
    }
    return;
  }
  void makert(int x) {
    access(x);
    splay(x);
    rev[x] ^= 1;
    swap(son[x][0], son[x][1]);
    return;
  }
  int getrt(int x) {
    access(x);
    splay(x);
    while (son[x][0]) {
      pushdown(x);
      x = son[x][0];
    }
    splay(x);
    return x;
  }
  void split(int x, int y) {
    makert(x);
    access(y);
    splay(y);
    return;
  }
  void link(int x, int y) {
    makert(x);
    if (getrt(y) != x) {
      splay(y);
      fa[x] = y;
      vsiz[y] += siz[x];
      pushup(y);
    }
    return;
  }
  void cut(int x, int y) {
    split(x, y);
    if (getrt(y) == x && fa[y] == x && !son[y][0]) {
      fa[y] = son[x][1] = 0;
      siz[x] -= siz[y];
      pushup(x);
    }
    return;
  }
} lct;
int n, m, buc[2];
priority_queue<node> q;
bool del[M];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n + m; i++) lct.siz[i] = 1;
  buc[1] = n;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].v);
    e[i].id = i;
    lct.val[i + n] = e[i].v, lct.mxx_id[i + n] = i + n;
    int x = e[i].x, y = e[i].y, val = e[i].v;
    if (lct.getrt(x) != lct.getrt(y)) {
      lct.makert(x);
      lct.makert(y);
      buc[((lct.siz[x] + 1) / 2) & 1]--;
      buc[((lct.siz[y] + 1) / 2) & 1]--;
      buc[((lct.siz[x] + lct.siz[y] + 2) / 2) & 1]++;
      lct.link(x, i + n);
      lct.link(y, i + n);
      q.push(e[i]);
    } else {
      lct.split(x, y);
      int op = lct.mxx_id[y];
      if (lct.val[op] > val) {
        lct.cut(e[op - n].x, op);
        lct.cut(e[op - n].y, op);
        del[op - n] = 1;
        lct.link(x, i + n);
        lct.link(y, i + n);
        q.push(e[i]);
      }
    }
    if (buc[1])
      printf("-1\n");
    else {
      while (!q.empty()) {
        int x = q.top().x, y = q.top().y, val = q.top().v, id = q.top().id;
        q.pop();
        if (del[id]) continue;
        lct.cut(x, id + n);
        lct.cut(y, id + n);
        lct.makert(x);
        lct.makert(y);
        buc[((lct.siz[x] + 1) / 2) & 1]++;
        buc[((lct.siz[y] + 1) / 2) & 1]++;
        buc[((lct.siz[x] + lct.siz[y] + 2) / 2) & 1]--;
        if (buc[1]) {
          buc[((lct.siz[x] + 1) / 2) & 1]--;
          buc[((lct.siz[y] + 1) / 2) & 1]--;
          buc[((lct.siz[x] + lct.siz[y] + 2) / 2) & 1]++;
          lct.link(x, id + n);
          lct.link(y, id + n);
          q.push(e[id]);
          break;
        }
      }
      printf("%lld\n", q.top().v);
    }
  }
  return 0;
}
