#include <bits/stdc++.h>
using namespace std;
const int N = 400100;
const int M = 300100;
inline int read() {
  char ch;
  while ((ch = getchar()) < '0' || ch > '9')
    ;
  int res = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9') res = res * 10 + ch - '0';
  return res;
}
int vis[M], n, m;
int a[N], b[N], c[N];
struct node {
  int w, id;
};
bool operator<(node a, node b) { return a.w < b.w; }
priority_queue<node> q;
struct Lca {
  int rev[N], ch[N][2], w[N], siz[N], f[N], s[N], v[N];
  int cnt;
  inline int Max(int a, int b) { return w[a] > w[b] ? a : b; }
  void pushup(int k) {
    if (!k) return;
    siz[k] = siz[ch[k][0]] + siz[ch[k][1]] + s[k] + (k <= n);
    v[k] = Max(k, Max(v[ch[k][0]], v[ch[k][1]]));
  }
  void reverse(int k) {
    rev[k] ^= 1;
    swap(ch[k][0], ch[k][1]);
  }
  void pushdown(int k) {
    if (rev[k]) {
      if (ch[k][0]) reverse(ch[k][0]);
      if (ch[k][1]) reverse(ch[k][1]);
      rev[k] = 0;
    }
  }
  bool wth(int x) { return ch[f[x]][1] == x; }
  bool nrt(int x) { return f[x] ? (ch[f[x]][1] == x || ch[f[x]][0] == x) : 0; }
  void rotate(int x) {
    int y = f[x], z = f[y];
    int k = wth(x), w = ch[x][k ^ 1];
    if (nrt(y)) ch[z][wth(y)] = x;
    f[x] = z;
    ch[y][k] = w;
    if (w) f[w] = y;
    ch[x][k ^ 1] = y;
    f[y] = x;
    pushup(y);
    pushup(x);
  }
  void pre(int x) {
    if (nrt(x)) pre(f[x]);
    pushdown(x);
  }
  void splay(int x) {
    pre(x);
    while (nrt(x)) {
      int y = f[x];
      if (nrt(y)) rotate(wth(x) == wth(y) ? y : x);
      rotate(x);
    }
  }
  void acess(int x) {
    for (int y = 0; x; x = f[y = x]) {
      splay(x);
      s[x] += siz[ch[x][1]];
      s[x] -= siz[ch[x][1] = y];
      pushup(x);
    }
  }
  void makert(int x) {
    acess(x);
    splay(x);
    reverse(x);
  }
  int findrt(int x) {
    acess(x);
    splay(x);
    while (ch[x][0]) pushdown(x), x = ch[x][0];
    splay(x);
    return x;
  }
  void split(int x, int y) {
    makert(x);
    acess(y);
    splay(y);
  }
  void link(int x, int y) {
    split(x, y);
    cnt -= siz[x] & 1;
    cnt -= siz[y] & 1;
    f[x] = y;
    s[y] += siz[x];
    pushup(y);
    cnt += siz[y] & 1;
  }
  void cut(int x, int y) {
    split(x, y);
    cnt -= siz[y] & 1;
    f[x] = ch[y][0] = 0;
    pushup(y);
    cnt += siz[x] & 1;
    cnt += siz[y] & 1;
  }
  void init() {
    for (int i = 1; i <= n; i++) siz[i] = 1;
    cnt = n;
  }
  int add(int e) {
    int x = a[e], y = b[e], z = c[e];
    int flag = 1;
    if (findrt(x) == findrt(y)) {
      split(x, y);
      int ce = v[y] - n;
      if (c[ce] > z)
        cut(a[ce], ce + n), cut(b[ce], ce + n), vis[ce] = 1;
      else
        flag = 0;
    }
    if (flag) {
      v[e + n] = e + n;
      w[e + n] = z;
      link(x, e + n);
      link(y, e + n);
      q.push((node){z, e});
    }
    if (cnt) return -1;
    while (!q.empty()) {
      node tmp = q.top();
      q.pop();
      int u = tmp.id;
      if (vis[u]) continue;
      cut(a[u], u + n);
      cut(b[u], u + n);
      if (cnt) {
        link(a[u], u + n);
        link(b[u], u + n);
        q.push((node){c[u], u});
        return c[u];
      }
    }
    return 0;
  }
} lct;
int main() {
  n = read();
  m = read();
  lct.init();
  for (int i = 1; i <= m; i++) {
    a[i] = read();
    b[i] = read();
    c[i] = read();
    printf("%d\n", lct.add(i));
  }
  return 0;
}
