#include <bits/stdc++.h>
using namespace std;
const int maxx = 600010;
const double eps = 1e-15;
const double pi = acos(-1.0);
vector<int> g[maxx], g1[maxx];
int t, n, m, u, v;
int ans = 0, sum = 0, cnt = 0, res = 0, root = 0, total = 0, maxn = 0, l = 1,
    r = 1;
int a[maxx], s[maxx], bj[maxx], fa[maxx], siz[maxx], vis[maxx];
int mk[3111][3111];
struct node {
  int x;
  int y;
} b[maxx];
int find(int x) {
  if (fa[x] == x) return x;
  return fa[x] = find(fa[x]);
}
void dfs1(int x, int father, int opt, int dep) {
  vis[x] = 1;
  int uu = 0;
  for (register int i = 0; i < g[x].size(); i++) {
    int to = g[x][i];
    if (to == father) continue;
    uu = 1;
    if (!opt) {
      int X = find(x), Y = find(to);
      if (X != Y) fa[Y] = X;
    }
    dfs1(to, x, opt, dep + 1);
  }
  if (!uu && res <= dep) res = dep, root = x;
}
void dfs2(int x, int father) {
  int uu = 0;
  s[++cnt] = x;
  for (register int i = 0; i < g[x].size(); i++) {
    int to = g[x][i];
    if (to == father) continue;
    uu = 1;
    dfs2(to, x);
  }
  if (!uu) {
    if (cnt > sum) {
      sum = cnt;
      bj[total] = s[(cnt + 1) / 2];
    }
  }
  cnt--;
}
void dfs3(int x, int father, int dep) {
  cnt = max(cnt, dep);
  for (register int i = 0; i < g[x].size(); i++) {
    int to = g[x][i];
    if (to == father) continue;
    dfs3(to, x, dep + 1);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (register int i = 1; i <= n; i++) fa[i] = i;
  for (register int i = 1; i <= m; i++) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v), g[v].push_back(u);
    g1[u].push_back(v), g1[v].push_back(u);
  }
  for (register int i = 1; i <= n; i++) {
    if (!vis[i]) {
      total++;
      cnt = sum = res = 0;
      dfs1(i, i, 0, 0);
      dfs2(root, root);
    }
  }
  ans = 1e8;
  for (register int i = 1; i <= n; i++) {
    int X = find(i);
    for (register int j = 1; j <= total; j++) {
      int Y = find(bj[j]);
      if (X != Y) g[i].push_back(bj[j]), g[bj[j]].push_back(i);
    }
    cnt = res = 0;
    dfs1(i, i, 1, 0);
    dfs3(root, root, 0);
    if (cnt < ans) {
      sum = 0;
      ans = cnt;
      for (register int j = 1; j <= total; j++) {
        int Y = find(bj[j]);
        if (X != Y) b[++sum].x = i, b[sum].y = bj[j];
      }
    }
    for (register int j = 1; j <= n; j++) {
      g[j].clear();
      g[j] = g1[j];
    }
  }
  printf("%d\n", ans);
  for (register int i = 1; i <= sum; i++) printf("%d %d\n", b[i].x, b[i].y);
  return 0;
}
