#include <bits/stdc++.h>
using namespace std;
const int sz = 1e4 + 4;
const int oo = 1e9 + 4;
int n, m, k;
vector<int> g[sz];
vector<pair<int, int> > add, vv;
int clr[sz], path[sz], col[sz];
bool us[sz];
void dfs(int v, int c) {
  if (clr[v]) return;
  clr[v] = c;
  col[c] = v;
  for (auto to : g[v]) dfs(to, c);
}
pair<int, int> large(int v, int c, bool one) {
  if (us[v]) return {0, 0};
  us[v] = 1;
  pair<int, int> k = {0, v};
  for (auto to : g[v]) {
    pair<int, int> q = large(to, c, one);
    if (k.first < q.first) k = q;
  }
  k.first++;
  return k;
}
bool dfsmake(int v, int d, int c, int dd, bool one) {
  if (us[v]) return 0;
  us[v] = 1;
  bool yes = 0;
  if (d == dd)
    yes = 1;
  else
    for (auto to : g[v]) yes |= dfsmake(to, d + 1, c, dd, one);
  if (yes) {
    path[d] = v;
  }
  return yes;
}
int fndD(int v, int c, bool one) {
  fill(us, us + n, 0);
  pair<int, int> a, b;
  a = large(v, c, one);
  fill(us, us + n, 0);
  b = large(a.second, c, one);
  fill(us, us + n, 0);
  dfsmake(b.second, 0, c, b.first - 1, one);
  return b.first;
}
void solve() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int f, t;
    cin >> f >> t;
    f--;
    t--;
    g[f].push_back(t);
    g[t].push_back(f);
  }
  for (int i = 0; i < n; i++)
    if (!clr[i]) {
      dfs(i, ++k);
      int d = fndD(i, k, 1);
      vv.push_back({d, k});
    }
  sort(vv.rbegin(), vv.rend());
  for (int i = 0; i < vv.size() - 1; i++) {
    int d1 = fndD(col[vv[i].second], vv[i].second, 0), a = path[d1 / 2],
        d2 = fndD(col[vv[i + 1].second], vv[i + 1].second, 1), b = path[d2 / 2];
    add.push_back({a + 1, b + 1});
    g[a].push_back(b);
    g[b].push_back(a);
  }
  cout << fndD(0, oo, 0) - 1 << endl;
  for (auto i : add) cout << i.first << ' ' << i.second << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
