#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
struct Edge {
  int u, v, w;
  Edge(){};
};
struct LCT {
  int fa[N], root, tcnt;
  int son[N][2];
  pair<int, int> mx[N], val[N];
  int exsiz[N], si[N], siz[N];
  int rev[N];
  int cnt = 0;
  void pushup(int pos) {
    mx[pos] = max(val[pos], max(mx[son[pos][0]], mx[son[pos][1]]));
    siz[pos] = siz[son[pos][0]] + siz[son[pos][1]] + exsiz[pos] + si[pos];
  }
  bool isroot(int pos) {
    return pos != son[fa[pos]][0] && pos != son[fa[pos]][1];
  }
  void addrev(int pos) {
    rev[pos] ^= 1;
    swap(son[pos][0], son[pos][1]);
  }
  void pushdown(int pos) {
    if (rev[pos]) {
      if (son[pos][1]) addrev(son[pos][1]);
      if (son[pos][0]) addrev(son[pos][0]);
      rev[pos] = 0;
    }
  }
  bool isson(int pos) { return pos == son[fa[pos]][1]; }
  void conect(int x, int d, int y) {
    son[x][d] = y;
    fa[y] = x;
  }
  void rorate(int x) {
    int y = fa[x], z = fa[y], dx = isson(x), dy = isson(y);
    fa[x] = z;
    if (!isroot(y)) son[z][dy] = x;
    conect(y, dx, son[x][dx ^ 1]);
    conect(x, dx ^ 1, y);
    pushup(y), pushup(x);
  }
  int st[N];
  void splay(int x) {
    int top;
    st[top = 1] = x;
    int u = x;
    while (!isroot(u)) u = fa[u], st[++top] = u;
    while (top) pushdown(st[top--]);
    while (!isroot(x)) {
      int y = fa[x];
      if (isroot(y)) {
        rorate(x);
      } else if (isson(x) == isson(y))
        rorate(y), rorate(x);
      else
        rorate(x), rorate(x);
    }
    pushup(x);
  }
  void access(int x) {
    for (int i = 0; x; i = x, x = fa[x]) {
      splay(x), exsiz[x] -= siz[i], exsiz[x] += siz[son[x][1]], son[x][1] = i,
                                                                pushup(x);
    }
  }
  void makeroot(int x) { access(x), splay(x), addrev(x); }
  int findroot(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) {
    makeroot(x);
    access(y);
    splay(y);
  }
  void link(int x, int y) {
    split(x, y);
    cnt -= siz[x] & 1;
    cnt -= siz[y] & 1;
    fa[x] = y;
    exsiz[y] += siz[x];
    pushup(y);
    cnt += siz[y] & 1;
  }
  void cut(int x, int y) {
    split(x, y);
    fa[x] = 0;
    son[y][0] = 0;
    cnt -= siz[y] & 1;
    pushup(y);
    cnt += siz[x] & 1;
    cnt += siz[y] & 1;
  }
  void init(int n, int m, vector<Edge> tmp) {
    for (int i = 1; i <= n; i++) si[i] = 1;
    for (int i = 1; i <= m; i++) val[i + n] = mx[i + n] = {tmp[i].w, i};
  }
  void solve(int n, int m, vector<Edge> tmp) {
    cnt = n;
    vector<int> vis(m + 1);
    priority_queue<pair<int, int>> q;
    auto add = [&](int i) {
      int u = tmp[i].u, v = tmp[i].v, w = tmp[i].w;
      bool flag = 1;
      if (findroot(u) == findroot(v)) {
        split(u, v);
        auto o = mx[v];
        if (o.first > w) {
          cut(n + o.second, tmp[o.second].u);
          cut(n + o.second, tmp[o.second].v);
          vis[o.second] = 1;
        } else
          flag = 0;
      }
      if (flag) {
        link(i + n, u), link(i + n, v);
        q.push({tmp[i].w, i});
      }
      if (cnt) return -1;
      while (q.size()) {
        int o = q.top().second;
        q.pop();
        if (vis[o]) continue;
        cut(tmp[o].u, o + n);
        cut(tmp[o].v, o + n);
        if (cnt) {
          link(tmp[o].u, o + n), link(tmp[o].v, o + n);
          q.push({tmp[o].w, o});
          return tmp[o].w;
        }
      }
      return -1;
    };
    for (int i = 1; i <= m; i++) cout << add(i) << endl;
  }
} t;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<Edge> e(m + 1);
  for (int i = 1; i <= m; i++) cin >> e[i].u >> e[i].v >> e[i].w;
  t.init(n, m, e);
  t.solve(n, m, e);
}
