#include <bits/stdc++.h>
using namespace std;
int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int N = 1e3 + 5;
vector<int> G[N];
int len[N];
int fir[N], sec[N];
bool vis[N];
int n, m;
int MIN, x, y, z;
struct pa {
  int x, y, z;
};
struct cmp {
  bool operator()(pa a, pa b) {
    if (a.x == b.x) return a.y > b.y;
    return a.x > b.x;
  }
};
void dfs1(int now, int fa) {
  vis[now] = true;
  for (int i = 0; i < int(G[now].size()); i++) {
    int to = G[now][i];
    if (to == fa) continue;
    dfs1(to, now);
    len[now] = max(len[now], len[to] + 1);
  }
}
void dfs2(int now, int fa, int las) {
  vis[now] = true;
  multiset<int> st;
  st.insert(las + 1);
  for (int i = 0; i < int(G[now].size()); i++) {
    int to = G[now][i];
    if (to == fa) continue;
    st.insert(len[to] + 1);
  }
  for (int i = 0; i < int(G[now].size()); i++) {
    int to = G[now][i];
    if (to == fa) continue;
    st.erase(st.find(len[to] + 1));
    dfs2(to, now, *st.rbegin());
    st.insert(len[to] + 1);
  }
  fir[now] = *st.rbegin();
  multiset<int>::iterator it;
  if (st.size() == 1)
    sec[now] = 0;
  else {
    it = st.end();
    --it, --it, sec[now] = *it;
  }
  if (MIN > fir[now]) MIN = x = fir[now], y = sec[now], z = now;
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; i++) {
    int x = read(), y = read();
    G[x].push_back(y), G[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) dfs1(i, 0);
  }
  memset(vis, 0, sizeof(vis));
  multiset<pa, cmp> st;
  for (int i = 1; i <= n; i++) {
    MIN = 2e9;
    if (!vis[i]) dfs2(i, 0, -1), st.insert((pa){x, y, z});
  }
  multiset<pa>::iterator it = st.begin();
  multiset<int> st2;
  st2.insert(st.begin()->x);
  st2.insert(st.begin()->y);
  ++it;
  for (; it != st.end(); ++it) {
    st2.insert(it->x + 1);
  }
  int ans = 0;
  multiset<int>::iterator it2 = st2.end();
  --it2, ans += *it2;
  --it2, ans += *it2;
  printf("%d\n", ans);
  it = st.begin();
  int x = it->z;
  ++it;
  for (; it != st.end(); ++it) {
    printf("%d %d\n", x, it->z);
  }
  return 0;
}
