#include <bits/stdc++.h>
using namespace std;
int const maxn = 1001;
int n, m, cnt, H[maxn], father[maxn], d[maxn], Dep[maxn], dist[maxn],
    dist2[maxn], middle_point[maxn], length[maxn];
int max_dep, max_dist;
struct Edge {
  int next, to;
} E[maxn << 1];
inline void add(int a, int b) {
  E[++cnt] = (Edge){H[a], b};
  H[a] = cnt;
}
inline int find(int x) {
  if (father[x] != x) father[x] = find(father[x]);
  return father[x];
}
inline void merge(int x, int y) {
  int r1 = find(x), r2 = find(y);
  if (r1 != r2) father[r1] = r2;
}
void getdep(int x, int fa) {
  Dep[x] = Dep[fa] + 1;
  for (int i = H[x]; i; i = E[i].next)
    if (E[i].to != fa) getdep(E[i].to, x);
}
void getdist(int x, int fa) {
  dist[x] = dist[fa] + 1;
  for (int i = H[x]; i; i = E[i].next)
    if (E[i].to != fa) getdist(E[i].to, x);
}
void getdist2(int x, int fa) {
  dist2[x] = dist2[fa] + 1;
  for (int i = H[x]; i; i = E[i].next)
    if (E[i].to != fa) getdist2(E[i].to, x);
}
inline void work(int x) {
  getdep(x, 0);
  int max_dep = 0;
  for (int i = 1; i <= n; i++)
    if (father[i] == x && Dep[i] > Dep[max_dep]) max_dep = i;
  getdist(max_dep, 0);
  int max_dist = 0;
  for (int i = 1; i <= n; i++)
    if (father[i] == x && dist[i] > dist[max_dist]) max_dist = i;
  getdist2(max_dist, 0);
  middle_point[x] = max_dep;
  for (int i = 1; i <= n; i++)
    if (father[i] == x && dist[i] + dist2[i] == dist[max_dist] &&
        min(dist[i], dist2[i]) >
            min(dist[middle_point[x]], dist2[middle_point[x]]))
      middle_point[x] = i;
  length[x] = dist[max_dist];
}
int main() {
  Dep[0] = dist[0] = dist2[0] = -1;
  for (int i = 1; i <= n; i++) middle_point[i] = i;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) father[i] = i;
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y), add(y, x);
    merge(x, y);
  }
  for (int i = 1; i <= n; i++) father[i] = find(father[i]);
  for (int i = 1; i <= n; i++)
    if (father[i] == i) work(i);
  int first = 0;
  for (int i = 1; i <= n; i++)
    if (father[i] == i && length[i] >= length[first]) first = i;
  int ans = length[first];
  for (int i = 1; i <= n; i++) {
    if (father[i] != i) continue;
    if (i == first) continue;
    if (ans == 1 && length[i] == 1)
      ans += 2;
    else if (abs(ans - length[i]) <= 1)
      ans++;
    else if ((ans & 1) && (length[i] & 1) && abs(ans - length[i] == 2))
      ans++;
  }
  printf("%d\n", ans);
  for (int i = 1; i <= n; i++)
    if (father[i] == i && i != first)
      printf("%d %d\n", middle_point[i], middle_point[first]);
  return 0;
}
