#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
const int N = 2e4 + 5;
int n, u, v, m, k, x, ans;
vector<int> g[N];
int pre_center;
vector<int> centers;
bool vis[N];
int sz = 0, subsz[N], l1[N], l2[N];
int mx = 0, farN;
void dfs1(int curr, int par, int dep) {
  vis[curr] = true;
  if (dep >= mx) {
    mx = dep;
    farN = curr;
  }
  for (int x : g[curr]) {
    if (x != par) {
      dfs1(x, curr, dep + 1);
    }
  }
}
vector<int> id;
vector<pair<int, int> > sols;
std::vector<pair<int, int> > ecen;
int ecccen = 0;
void dfs2(int curr, int par, int dep) {
  l1[curr] = dep;
  id.push_back(curr);
  if (mx <= dep) {
    mx = dep;
    farN = curr;
  }
  for (int x : g[curr]) {
    if (x != par) {
      dfs2(x, curr, dep + 1);
    }
  }
}
void dfs3(int curr, int par, int dep) {
  l2[curr] = dep;
  for (int x : g[curr]) {
    if (x != par) {
      dfs3(x, curr, dep + 1);
    }
  }
}
void computeCenter(int i) {
  id.clear();
  mx = 0;
  dfs1(i, 0, 0);
  mx = 0;
  dfs2(farN, 0, 0);
  mx = 0;
  dfs3(farN, 0, 0);
  int f = 0;
  for (int x : id) {
    if (l1[x] == l2[x]) {
      centers.push_back(x);
      pre_center = x;
      ecccen = l1[x];
      f = 1;
      break;
    }
  }
  if (!f) {
    for (int x : id) {
      if (l1[x] == l2[x] - 1 || l2[x] == l1[x] - 1) {
        centers.push_back(x);
        pre_center = x;
        ecccen = max(l1[x], l2[x]);
        f = 1;
        break;
      }
    }
  }
  ecen.push_back({ecccen, pre_center});
}
void diameter() {
  mx = 0;
  dfs1(1, 0, 0);
  mx = 0;
  dfs2(farN, 0, 0);
  ans = mx;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  if (m != n - 1) {
    for (int i = 1; i <= n; i++) {
      if (!vis[i]) {
        computeCenter(i);
      }
    }
    sort(ecen.begin(), ecen.end(), greater<pair<int, int> >());
    for (int i = 1; i < (int)ecen.size(); i++) {
      g[ecen[0].second].push_back(ecen[i].second);
      g[ecen[i].second].push_back(ecen[0].second);
      sols.push_back({ecen[0].second, ecen[i].second});
    }
  }
  diameter();
  cout << ans << '\n';
  for (auto it : sols) {
    cout << it.first << ' ' << it.second << '\n';
  }
  return 0;
}
