#include <bits/stdc++.h>
using namespace std;
int N, M;
int a[1000][1000];
int used[1000];
bool ok;
int p = -1;
void dfs(int v, int k) {
  used[v] = 1;
  p = max(p, k);
  for (int i = 0; i < N; i++)
    if (a[v][i] && used[i] == 0) dfs(i, k + 1);
}
bool check(int x) {
  int cnt = 0;
  for (int i = 0; i < N; i++)
    if (a[x][i]) cnt++;
  if (cnt > 1)
    return 0;
  else
    return 1;
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    --x, --y;
    a[x][y] = a[y][x] = 1;
  }
  for (int i = 0; i < N; i++) {
    int cnt = 0;
    for (int j = 0; j < N; j++)
      if (a[i][j]) cnt++;
    if (cnt > 2) {
      printf("-1\n");
      return 0;
    }
  }
  for (int i = 0; i < N; i++) {
    memset(used, 0, sizeof(used));
    p = 1;
    dfs(i, 1);
    if (p > 3) {
      printf("-1\n");
      return 0;
    }
  }
  vector<vector<int> > res;
  memset(used, 0, sizeof(used));
  for (int i = 0; i < N; i++) {
    vector<int> ans;
    bool ok = true;
    if (used[i]) continue;
    for (int j = 0; j < N && ok; j++)
      if (a[i][j] && !used[j]) {
        ans.push_back(i);
        ans.push_back(j);
        used[i] = 1;
        used[j] = 1;
        for (int k = 0; k < N && ok; k++)
          if ((a[k][i] && !used[k])) {
            used[k] = 1;
            ans.push_back(k);
            ok = false;
            break;
          }
        if (ans.size() < 3) {
          for (int k = 0; k < N; k++)
            if ((a[k][i] || a[k][j]) && check(k) && !used[k]) {
              used[k] = 1;
              ans.push_back(k);
              ok = false;
              break;
            }
        }
        if (ok) {
          bool ok1 = true;
          for (int k = 0; k < N && ok1; k++) {
            if (used[k]) continue;
            int cnt = 0;
            for (int q = 0; q < N; q++)
              if (a[k][q]) cnt++;
            if (cnt == 0) {
              used[k] = 1;
              ans.push_back(k);
              ok1 = false;
              break;
            }
          }
        }
      }
    if (ans.size() == 3) res.push_back(ans);
  }
  vector<int> ans;
  for (int i = 0; i < N; i++)
    if (used[i] == 0) {
      ans.push_back(i);
      if (ans.size() == 3) {
        res.push_back(ans);
        ans.clear();
      }
    }
  if (res.size() == 0 || res.size() != N / 3) {
    printf("-1\n");
    return 0;
  }
  for (int i = 0; i < res.size(); i++) {
    sort(res[i].begin(), res[i].end());
    for (int j = 0; j < res[i].size(); j++) printf("%d ", res[i][j] + 1);
    printf("\n");
  }
  return 0;
}
