#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > v[2];
int used[100];
int diam(int n, const vector<vector<int> >& v) {
  int res = 0;
  vector<int> dist;
  for (int i = 0; i < n; i++) {
    dist.clear();
    dist.resize(n, 1000000);
    queue<int> q;
    q.push(i);
    dist[i] = 0;
    while (!q.empty()) {
      int cur = q.front();
      q.pop();
      for (int j : v[cur]) {
        if (dist[j] > dist[cur] + 1) {
          dist[j] = dist[cur] + 1;
          q.push(j);
        }
      }
    }
    int maxdist = *max_element(dist.begin(), dist.end());
    if (maxdist == 1000000) return -1;
    res = max(res, maxdist);
  }
  return res;
}
void print_res(int n, const vector<vector<int> >& v) {
  int ecnt = 0;
  for (int i = 0; i < n; i++)
    for (int j : v[i])
      if (i < j) ecnt++;
  printf("%d\n", ecnt);
  for (int i = 0; i < n; i++)
    for (int j : v[i])
      if (i < j) printf("%d %d\n", i + 1, j + 1);
}
bool solve(int n, int k, int v0, int v1) {
  if (v0 == n) {
    if (k == min(diam(n, v[0]), diam(n, v[1]))) {
      if (!used[k]) {
        used[k] = true;
        print_res(n, v[1]);
      }
      return true;
    }
    return false;
  }
  if (v1 == n) {
    return solve(n, k, v0 + 1, v0 + 2);
  }
  v[0][v0].push_back(v1);
  v[0][v1].push_back(v0);
  if (solve(n, k, v0, v1 + 1)) return true;
  v[0][v0].pop_back();
  v[0][v1].pop_back();
  v[1][v0].push_back(v1);
  v[1][v1].push_back(v0);
  if (solve(n, k, v0, v1 + 1)) return true;
  v[1][v0].pop_back();
  v[1][v1].pop_back();
  return false;
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  v[0].resize(n);
  v[1].resize(n);
  if (n < 7) {
    if (!solve(n, k, 0, 1)) printf("-1\n");
  } else {
    if (k == 2) {
      for (int i = 0; i < n - 4; i++) {
        v[1][i].push_back(n - 1);
        v[1][n - 1].push_back(i);
      }
      v[1][n - 4].push_back(n - 2);
      v[1][n - 2].push_back(n - 4);
      v[1][n - 2].push_back(n - 3);
      v[1][n - 3].push_back(n - 2);
      v[1][n - 3].push_back(n - 1);
      v[1][n - 1].push_back(n - 3);
      print_res(n, v[1]);
    } else if (k == 3) {
      for (int i = 0; i < n - 3; i++) {
        v[1][i].push_back(n - 1);
        v[1][n - 1].push_back(i);
      }
      v[1][n - 3].push_back(n - 2);
      v[1][n - 2].push_back(n - 3);
      v[1][n - 1].push_back(n - 2);
      v[1][n - 2].push_back(n - 1);
      print_res(n, v[1]);
    } else
      printf("-1\n");
  }
  return 0;
}
