#include <bits/stdc++.h>
#pragma optimize("", off)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/STACK:36777216")
const long long inf = 0x3f3f3f3f3f3f3f3LL;
const long long mod = 1e9 + 7;
using namespace std;
template <class T>
void smin(T& a, T val) {
  if (a > val) a = val;
}
template <class T>
void smax(T& a, T val) {
  if (a < val) a = val;
}
template <typename T>
inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
  bool first = true;
  os << "[";
  for (auto i : v) {
    if (!first) os << ", ";
    os << i;
    first = false;
  }
  return os << "]";
}
const long long N = 1e3 + 10;
long long n, m;
std::vector<long long> v[N];
long long parent[N];
bool vis[N];
long long ma, first;
void dfs(long long s, long long p, long long lvl) {
  parent[s] = p;
  vis[s] = 1;
  if (ma < lvl) {
    ma = lvl;
    first = s;
  }
  for (auto i : v[s]) {
    if (i != p) dfs(i, s, lvl + 1);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (long long i = 0; i < m; ++i) {
    long long x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  std::vector<long long> centers;
  long long bst = -1, root = -1;
  for (long long i = 1; i <= n; ++i) {
    if (!vis[i]) {
      ma = -1;
      first = i;
      dfs(i, 0, 0);
      ma = -1;
      dfs(first, 0, 0);
      long long cur = first;
      for (long long i = 0; i < ma / 2; ++i) cur = parent[cur];
      centers.push_back(cur);
      if (ma > bst) {
        bst = ma;
        root = cur;
      }
    }
  }
  if ((long long)(centers.size()) == 1) {
    cout << bst << '\n';
    return 0;
  }
  std::vector<pair<long long, long long> > ans;
  for (auto i : centers) {
    if (i == root) continue;
    v[root].push_back(i);
    v[i].push_back(root);
    ans.push_back({root, i});
  }
  ma = -1;
  dfs(centers[0], 0, 0);
  ma = -1;
  dfs(first, 0, 0);
  cout << ma << '\n';
  for (auto i : ans) cout << i.first << ' ' << i.second << '\n';
  return 0;
}
