#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
int h[N], ne[N], e[N], idx;
int p[N];
int n, m;
void add(int a, int b) {
  e[idx] = b;
  ne[idx] = h[a];
  h[a] = idx++;
}
int find(int x) {
  if (p[x] != x) p[x] = find(p[x]);
  return p[x];
}
pair<int, int> BFS(int x, int &y) {
  int pre[N];
  queue<pair<int, int> > q;
  q.push({x, 0});
  pair<int, int> ans = {x, 0};
  bool st[N] = {0};
  while (q.size()) {
    auto k = q.front();
    q.pop();
    st[k.first] = true;
    if (k.second > ans.second) ans = k;
    for (int i = h[k.first]; i != -1; i = ne[i])
      if (!st[e[i]]) {
        q.push({e[i], k.second + 1});
        pre[e[i]] = k.first;
      }
  }
  int d = ans.second;
  d /= 2;
  int j = ans.first;
  for (int i = 1; i <= d; i++) j = pre[j];
  y = ans.first;
  return {ans.second, j};
}
pair<int, int> get(int x) {
  int y;
  BFS(x, y);
  return BFS(y, x);
}
void solve() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    h[i] = -1;
    p[i] = i;
  }
  while (m--) {
    int a, b;
    cin >> a >> b;
    add(a, b);
    add(b, a);
    int pa = find(a);
    int pb = find(b);
    if (pa != pb) p[pa] = pb;
  }
  priority_queue<pair<int, int> > q;
  for (int i = 1; i <= n; i++)
    if (p[i] == i) q.push({get(i).first, i});
  vector<pair<int, int> > ans;
  while (q.size() > 1) {
    int a = q.top().second;
    q.pop();
    int b = q.top().second;
    q.pop();
    int x = get(a).second;
    int y = get(b).second;
    add(x, y);
    add(y, x);
    ans.push_back({x, y});
    int pa = find(x);
    int pb = find(y);
    p[pa] = pb;
    q.push({get(pb).first, pb});
  }
  cout << get(1).first << endl;
  for (auto k : ans) cout << k.first << " " << k.second << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int T = 1;
  while (T--) solve();
  return 0;
}
