#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
int read() {
  int out = 0;
  char c;
  while (!isdigit(c = getchar()))
    ;
  for (; isdigit(c); c = getchar()) out = out * 10 + c - '0';
  return out;
}
struct Edge {
  int u, v, w, id;
  bool operator<(const Edge &b) const { return w < b.w; }
};
struct LCT {
  struct Node {
    vector<int> ch;
    int pa;
    bool node, odd, vir, rev;
    Edge e, mx;
    Node()
        : ch(2, 0),
          pa(0),
          node(true),
          odd(true),
          vir(false),
          rev(false),
          e({0, 0, 0, 0}),
          mx({0, 0, 0, 0}) {}
    Node(int u, int v, int w, int id)
        : ch(2, 0),
          pa(0),
          node(false),
          odd(false),
          vir(false),
          rev(false),
          e({u, v, w, id}),
          mx({u, v, w, id}) {}
  };
  int odd;
  vector<Node> t;
  vector<int> stk;
  LCT(int n, int m) {
    t.resize(n + 1);
    t[0] = Node(0, 0, 0, 0);
    t.reserve(n + m + 1);
    stk.reserve(n);
    odd = n;
  }
  bool nroot(int x) { return x == t[t[x].pa].ch[0] || x == t[t[x].pa].ch[1]; }
  void pushup(int x) {
    t[x].mx = max(t[x].e, max(t[t[x].ch[0]].mx, t[t[x].ch[1]].mx));
    t[x].odd = t[t[x].ch[0]].odd ^ t[t[x].ch[1]].odd ^ t[x].vir ^ t[x].node;
  }
  void rotate(int x) {
    int y = t[x].pa;
    int z = t[y].pa;
    int k = x == t[y].ch[1];
    if (nroot(y)) t[z].ch[y == t[z].ch[1]] = x;
    t[x].pa = z;
    t[y].ch[k] = t[x].ch[k ^ 1];
    t[t[x].ch[k ^ 1]].pa = y;
    t[x].ch[k ^ 1] = y;
    t[y].pa = x;
    pushup(y);
    pushup(x);
  }
  void reverse(int x) {
    swap(t[x].ch[0], t[x].ch[1]);
    t[x].rev ^= 1;
  }
  void pushdown(int x) {
    if (t[x].rev) {
      reverse(t[x].ch[0]);
      reverse(t[x].ch[1]);
      t[x].rev = false;
    }
  }
  void Splay(int x) {
    int u = x;
    stk.push_back(x);
    while (nroot(u)) stk.push_back(u = t[u].pa);
    while (!stk.empty()) {
      pushdown(stk.back());
      stk.pop_back();
    }
    while (nroot(x)) rotate(x);
  }
  void access(int x) {
    for (int y = 0; x; x = t[y = x].pa) {
      Splay(x);
      t[x].vir ^= t[y].odd ^ t[t[x].ch[1]].odd;
      t[x].ch[1] = y;
      pushup(x);
    }
  }
  void makeroot(int x) {
    access(x);
    Splay(x);
    reverse(x);
  }
  int findroot(int x) {
    access(x);
    Splay(x);
    while (t[x].ch[0]) {
      pushdown(x);
      x = t[x].ch[0];
    }
    Splay(x);
    return x;
  }
  void link(int x, int y, int w, int id) {
    int e = t.size();
    t.emplace_back(x, y, w, id);
    makeroot(x);
    makeroot(y);
    odd -= t[x].odd + t[y].odd;
    t[x].pa = t[y].pa = e;
    t[e].vir = t[x].odd ^ t[y].odd;
    pushup(e);
    odd += t[e].odd;
  }
  void cut(int x, int y, int id) {
    makeroot(x);
    if (findroot(y) == x) {
      if (t[t[x].ch[1]].e.id == id || t[t[t[x].ch[1]].ch[0]].e.id == id) {
        odd -= t[x].odd;
        t[x].ch[1] = t[y].pa = t[y].ch[0] = 0;
        pushup(x);
        pushup(y);
        odd += t[x].odd + t[y].odd;
      }
    }
  }
};
int main() {
  int n = read();
  int m = read();
  LCT t(n, m);
  priority_queue<Edge> q;
  int ans = INF;
  for (int i = 1; i <= m; ++i) {
    int u = read();
    int v = read();
    int w = read();
    q.push({u, v, w, i});
    t.makeroot(u);
    if (t.findroot(v) == u) {
      Edge e = t.t[u].mx;
      if (e.w > w) {
        t.cut(e.u, e.v, e.id);
        t.link(u, v, w, i);
      } else if (!t.odd)
        ans = min(ans, w);
    } else
      t.link(u, v, w, i);
    while (!t.odd) {
      Edge e = q.top();
      q.pop();
      ans = min(ans, e.w);
      t.cut(e.u, e.v, e.id);
    }
    printf("%d\n", ans == INF ? -1 : ans);
  }
  return 0;
}
