#include <bits/stdc++.h>
using namespace std;
int N, M, a, b, dist1[1010] = {}, dist2[1010] = {}, dist3[1010] = {},
                visited[1010] = {};
int len_id, len = 0, diameter = 0, d_id, center_id, ans = 0;
vector<vector<int>> v(1010);
vector<pair<int, int>> centers, edges;
void dfs1(int n) {
  if (len < dist1[n]) {
    len_id = n;
    len = dist1[n];
  }
  for (auto x : v[n]) {
    if (dist1[x] == -1) {
      dist1[x] = dist1[n] + 1;
      dfs1(x);
    }
  }
}
void dfs2(int n) {
  if (diameter < dist2[n]) {
    d_id = n;
    diameter = dist2[n];
  }
  for (auto x : v[n]) {
    if (dist2[x] == -1) {
      dist2[x] = dist2[n] + 1;
      dfs2(x);
    }
  }
}
void dfs3(int n) {
  for (auto x : v[n]) {
    if (dist3[x] == -1) {
      dist3[x] = dist3[n] + 1;
      dfs3(x);
    }
  }
}
void dfs4(int n) {
  visited[n] = 1;
  if (diameter % 2 == 0) {
    if (dist2[n] == diameter / 2 && dist3[n] == diameter / 2) {
      center_id = n;
      return;
    }
  } else {
    if (dist2[n] == (diameter + 1) / 2 && dist3[n] == diameter / 2) {
      center_id = n;
      return;
    }
  }
  for (auto x : v[n]) {
    if (visited[x] == -1) {
      dfs4(x);
    }
  }
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  for (int i = 1; i <= N; i++) {
    dist1[i] = -1;
    dist2[i] = -1;
    dist3[i] = -1;
    visited[i] = -1;
  }
  for (int i = 1; i <= N; i++) {
    if (dist1[i] == -1) {
      len = -1;
      diameter = -1;
      dist1[i] = 0;
      dfs1(i);
      dist2[len_id] = 0;
      dfs2(len_id);
      dist3[d_id] = 0;
      dfs3(d_id);
      dfs4(i);
      centers.push_back({diameter, center_id});
    }
  }
  sort(centers.begin(), centers.end());
  int n = centers.size();
  if (n == 1) {
    cout << centers[0].first << endl;
    return 0;
  }
  ans = centers[n - 1].first;
  for (int i = n - 2; i >= 0; i--) {
    edges.push_back({centers[n - 1].second, centers[i].second});
    ans = max(max(ans, centers[i].first),
              (ans + 1) / 2 + (centers[i].first + 1) / 2 + 1);
  }
  cout << ans << endl;
  for (int i = 0; i < edges.size(); i++) {
    cout << edges[i].first << " " << edges[i].second << endl;
  }
}
