#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-9, PI = acos(-1.);
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int N = 1e5 + 5;
int par[N], dist[N], dist2[N];
vector<int> adj[N];
pair<int, int> ini[N], fim[N], mn[N];
set<int> all, src;
void dfs(int s, int cnt) {
  par[s] = cnt;
  for (auto v : adj[s]) {
    if (!par[v]) dfs(v, cnt);
  }
}
void bfs() {
  memset((dist), (63), sizeof(dist));
  queue<int> q;
  for (auto s : src) {
    dist[s] = 0;
    q.push(s);
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : adj[u]) {
      if (dist[v] > dist[u] + 1) {
        dist[v] = dist[u] + 1;
        q.push(v);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (!par[i]) {
      dfs(i, i);
    }
  }
  for (int i = 1; i <= n; i++) all.insert(par[i]), src.insert(par[i]);
  bfs();
  for (int i = 1; i <= n; i++) {
    ini[par[i]] = max({{dist[i], i}, ini[par[i]]});
  }
  src.clear();
  for (auto p : all) src.insert(ini[p].second);
  bfs();
  for (int i = 1; i <= n; i++) {
    dist2[i] = dist[i];
    fim[par[i]] = max({{dist[i], i}, fim[par[i]]});
  }
  src.clear();
  for (auto p : all) src.insert(fim[p].second);
  bfs();
  memset((mn), (63), sizeof(mn));
  for (int i = 1; i <= n; i++) {
    int d = max(dist2[i], dist[i]);
    mn[par[i]] = min({mn[par[i]], {d, i}});
  }
  vector<pair<int, int> > ans;
  for (auto p : all) {
    mn[p].first = (fim[p].first + 1) / 2;
    ans.push_back(mn[p]);
  }
  sort(ans.begin(), ans.end());
  if (ans.size() == 1)
    cout << fim[par[1]].first << "\n";
  else {
    int tot = ans[ans.size() - 1].first + ans[ans.size() - 2].first + 1;
    if (ans.size() > 2) {
      if (ans[ans.size() - 1].first == ans[ans.size() - 2].first and
          ans[ans.size() - 1].first == ans[ans.size() - 3].first)
        tot++;
    }
    for (int i = 1; i <= n; i++) {
      tot = max(tot, fim[par[i]].first);
    }
    cout << tot << "\n";
    for (int i = 0; i < ans.size() - 1; i++) {
      cout << ans[i].second << " " << ans.back().second << "\n";
    }
  }
  return 0;
}
