#include <bits/stdc++.h>
using namespace std;
const int N = 7e5 + 9;
struct node {
  int p = 0, c[2] = {0, 0}, pp = 0;
  bool flip = 0;
  int sz = 0, ssz = 0, vsz = 0, v = 0;
  pair<int, int> mx = {0, 0}, val = {0, 0};
  node() {}
  node(int x, int w, int z) {
    val = {w, x};
    mx = {w, x};
    sz = v;
    v = z;
    ssz = v;
    vsz = 0;
  }
};
struct LCT {
  vector<node> t;
  LCT() {}
  LCT(int n) : t(n + 1) {}
  int dir(int x, int y) { return t[x].c[1] == y; }
  void set(int x, int d, int y) {
    if (x) t[x].c[d] = y, pull(x);
    if (y) t[y].p = x;
  }
  void pull(int x) {
    if (!x) return;
    int &l = t[x].c[0], &r = t[x].c[1];
    t[x].mx = max({t[l].mx, t[r].mx, t[x].val});
    t[x].sz = t[l].sz + t[r].sz + t[x].v;
    t[x].ssz = t[l].ssz + t[r].ssz + t[x].vsz + t[x].v;
  }
  void push(int x) {
    if (!x) return;
    int &l = t[x].c[0], &r = t[x].c[1];
    if (t[x].flip) {
      swap(l, r);
      if (l) t[l].flip ^= 1;
      if (r) t[r].flip ^= 1;
      t[x].flip = 0;
    }
  }
  void rotate(int x, int d) {
    int y = t[x].p, z = t[y].p, w = t[x].c[d];
    swap(t[x].pp, t[y].pp);
    set(y, !d, w);
    set(x, d, y);
    set(z, dir(z, y), x);
  }
  void splay(int x) {
    for (push(x); t[x].p;) {
      int y = t[x].p, z = t[y].p;
      push(z);
      push(y);
      push(x);
      int dx = dir(y, x), dy = dir(z, y);
      if (!z)
        rotate(x, !dx);
      else if (dx == dy)
        rotate(y, !dx), rotate(x, !dx);
      else
        rotate(x, dy), rotate(x, dx);
    }
  }
  void make_root(int u) {
    access(u);
    int l = t[u].c[0];
    t[l].flip ^= 1;
    swap(t[l].p, t[l].pp);
    t[u].vsz += t[l].ssz;
    set(u, 0, 0);
  }
  int access(int _u) {
    int last = _u;
    for (int v = 0, u = _u; u; u = t[v = u].pp) {
      splay(u);
      splay(v);
      t[u].vsz -= t[v].ssz;
      int r = t[u].c[1];
      t[u].vsz += t[r].ssz;
      t[v].pp = 0;
      swap(t[r].p, t[r].pp);
      set(u, 1, v);
      last = u;
    }
    splay(_u);
    return last;
  }
  void link(int u, int v) {
    make_root(v);
    access(u);
    splay(u);
    t[v].pp = u;
    t[u].vsz += t[v].ssz;
  }
  void cut(int u) {
    access(u);
    assert(t[u].c[0] != 0);
    t[t[u].c[0]].p = 0;
    t[u].c[0] = 0;
    pull(u);
  }
  int get_parent(int u) {
    access(u);
    return t[u].c[0];
  }
  int find_root(int u) {
    access(u);
    splay(u);
    push(u);
    while (t[u].c[0]) {
      u = t[u].c[0];
      push(u);
    }
    splay(u);
    return u;
  }
  bool connected(int u, int v) { return find_root(u) == find_root(v); }
  int is_root(int u) { return get_parent(u) == 0; }
  int component_size(int u) { return t[find_root(u)].ssz; }
  pair<int, int> query(int u, int v) {
    int cur = find_root(u);
    make_root(u);
    access(v);
    auto ans = t[v].mx;
    make_root(cur);
    return ans;
  }
} lct;
set<array<int, 4>> se;
int odd, L[N], R[N], W[N], T;
void add_edge(int u, int v, int w) {
  ++T;
  lct.t[T] = node(T, w, 0);
  if (lct.component_size(u) % 2 == 1 && lct.component_size(v) % 2 == 1) {
    odd -= 2;
  }
  se.insert({w, u, v, T});
  L[T] = u;
  R[T] = v;
  W[T] = w;
  lct.make_root(u);
  lct.make_root(v);
  lct.link(T, u);
  lct.link(T, v);
}
void remove_edge(int id) {
  lct.make_root(id);
  lct.cut(L[id]);
  lct.cut(R[id]);
  if (lct.component_size(L[id]) % 2 == 1 &&
      lct.component_size(R[id]) % 2 == 1) {
    odd += 2;
  }
  se.erase({W[id], L[id], R[id], id});
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  lct = LCT(n + m * 2 + 1);
  for (int i = 1; i <= n; i++) {
    lct.t[i] = node(i, 0, 1);
  }
  odd = n;
  T = n;
  while (m--) {
    int u, v, w;
    cin >> u >> v >> w;
    if (lct.connected(u, v)) {
      auto p = lct.query(u, v);
      if (p.first > w) {
        remove_edge(p.second);
        add_edge(u, v, w);
      }
    } else {
      add_edge(u, v, w);
    }
    if (odd) {
      cout << -1 << '\n';
    } else {
      while (!se.empty()) {
        auto x = *se.rbegin();
        remove_edge(x[3]);
        if (odd) {
          add_edge(x[1], x[2], x[0]);
          break;
        }
      }
      cout << (*se.rbegin())[0] << '\n';
    }
  }
  return 0;
}
