#include <bits/stdc++.h>
using namespace std;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};
const int dl[] = {0, 0, 0, -1};
const int dr[] = {0, 0, -1, 0};
long long Inverse(long long a, long long MOD) {
  long long m = a, n = MOD;
  long long xm = 1, xn = 0;
  while (n != 0) {
    long long q = m / n;
    long long r = m - q * n;
    long long xr = xm - q * xn;
    m = n;
    xm = xn;
    n = r;
    xn = xr;
  }
  return (xm % MOD + MOD) % MOD;
}
int lg(long long x) {
  int res = 0;
  for (int i = 0; i < x; i++) {
    long long GT = (1ll << i);
    if (GT <= x) {
      res = i;
    } else {
      return res;
    }
  }
  return res;
}
long long GCD(long long a, long long b) {
  if (b == 0) {
    return abs(a);
  }
  return GCD(b, a % b);
}
long long sqr(long long x) { return x * x; }
long long nhan(long long a, long long b, long long module) {
  a %= module;
  b %= module;
  long long q = (long double)a * b / module;
  long long r = (a * b - q * module) % module;
  if (r < 0) {
    r += module;
  }
  return r;
}
long long MU(long long a, long long n, long long module) {
  long long res = 1;
  for (; n; n = n >> 1, a = nhan(a, a, module)) {
    if (n & 1) {
      res = nhan(res, a, module);
    }
  }
  return res;
}
void hamZ(string S, int z[]) {
  int n = S.size();
  for (int i = 0; i <= n; i++) {
    z[i] = 0;
  }
  int L = 1, R = 1;
  for (int i = 2; i <= n; i++) {
    if (i <= R) {
      z[i] = min(z[i - L + 1], R - i + 1);
    }
    while (i + z[i] <= n && S[z[i]] == S[i + z[i] - 1]) {
      z[i]++;
    }
    if (i + z[i] - 1 > R) {
      R = i + z[i] - 1;
      L = i;
    }
  }
}
void Sang(int N, vector<int> Uoc[]) {
  for (int i = 1; i <= N; i++) {
    for (int j = i; j <= N; j += i) Uoc[j].push_back(i);
  }
}
const int N = 2e5 + 10;
const double sodu = 1e-9;
const long long oo = 1e18;
const int module = 998244353;
const int M = 1e9 + 7;
const int NN = (1 << 16);
int n, ts[N];
vector<int> a[N];
int KT[N], d[N];
int H[N];
int d1[N], m, Count = 0;
vector<int> stk[N];
void nhap() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    a[u].push_back(v);
    a[v].push_back(u);
  }
}
void DFS(int u) {
  if (u == 0) return;
  KT[u] = 1;
  stk[Count].push_back(u);
  for (int v : a[u]) {
    if (KT[v] == 0) {
      d[v] = d[u] + 1;
      DFS(v);
    }
  }
}
void rs() {
  for (int i = 1; i <= n; i++) KT[i] = d[i] = 0;
}
int Find_Center(int root) {
  stk[Count].clear();
  rs();
  DFS(root);
  int Left = root;
  for (int v : stk[Count]) {
    if (d[v] > d[Left]) Left = v;
  }
  for (int v : stk[Count]) KT[v] = d[v] = 0;
  stk[Count].clear();
  DFS(Left);
  int Right = root;
  for (int v : stk[Count]) {
    if (d[v] > d[Right]) Right = v;
    d1[v] = d[v];
  }
  for (int v : stk[Count]) KT[v] = d[v] = 0;
  stk[Count].clear();
  DFS(Right);
  for (int u : stk[Count]) {
    if (d[u] + d1[u] == d[Left] && abs(d[u] - d1[u]) <= 1) return u;
  }
}
void solve() {
  vector<pair<int, int> > KQ;
  Count = 0;
  int u = Find_Center(1);
  KQ.push_back(pair<int, int>(max(d1[u], d[u]), u));
  for (int i = 1; i <= n; i++) {
    if (KT[i] == 0) {
      Count++;
      int v = Find_Center(i);
      KQ.push_back(pair<int, int>(max(d1[v], d[v]), v));
    }
  }
  sort(KQ.begin(), KQ.end());
  reverse(KQ.begin(), KQ.end());
  vector<pair<int, int> > Noi;
  for (int i = 1; i < KQ.size(); i++) {
    int u = KQ[0].second;
    int v = KQ[i].second;
    Noi.push_back(pair<int, int>(KQ[0].second, KQ[i].second));
    a[u].push_back(v);
    a[v].push_back(u);
  }
  Count = 0;
  for (int i = 1; i <= n; i++) d1[i] = 0;
  Find_Center(1);
  int Res = 0;
  for (int i = 1; i <= n; i++) Res = max(Res, d[i]);
  cout << Res << '\n';
  for (pair<int, int> VC : Noi) {
    cout << VC.first << " " << VC.second << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  nhap();
  solve();
}
