#include <bits/stdc++.h>
using namespace std;
template <typename T>
void UpdateMax(T& a, T b) {
  if (a < b) a = b;
}
template <typename T>
void UpdateMin(T& a, T b) {
  if (a > b) a = b;
}
template <typename T>
inline void read(T& x) {
  x = 0;
  char c = getchar();
  int flag = 1;
  while (!isdigit(c)) {
    if (c == '-') flag = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= flag;
}
template <typename T>
void Unique(vector<T>& vec) {
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
}
template <typename T>
void Relabel(vector<T>& vec, T& value) {
  value = lower_bound(vec.begin(), vec.end(), value) - vec.begin() + 1;
}
template <typename T, typename V>
void Print(const pair<T, V>& x) {
  cout << x.first << " @ " << x.second << " | ";
}
template <typename T>
void Print(const T& x) {
  cout << x << " ";
}
template <typename T>
void Pcon(const T& x) {
  cout << "\nsize: " << x.size() << endl;
  for (const auto& item : x) Print(item);
}
std::vector<int> LABS;
template <typename T>
void Print(const T* x, int offset, int loop) {
  for (int i = 0; i <= loop; i++) {
    cout << "[";
    for (int item : LABS) cout << item - offset << ",";
    cout << i - offset << "]:";
    cout << x[i] << " ";
  }
}
template <typename T, typename... Args>
void Print(const T* f, int offset, int loop, Args... args) {
  for (int i = 0; i <= loop; i++) {
    LABS.push_back(i);
    Print(f[i], offset, args...);
    cout << endl;
    LABS.pop_back();
  }
}
template <typename T, typename V>
bool Has(T& cluster, const V& item) {
  return cluster.find(item) != cluster.end();
}
int Logn(long long x) {
  int cnt = 0;
  while (x) {
    ++cnt;
    x >>= 1;
  }
  return cnt;
}
const int MOD = 1e9 + 7;
const int N = 2e5 + 5;
const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {1, 0}};
struct Edg {
  int x, y, z;
  friend bool operator<(const Edg& lhs, const Edg& rhs) {
    return lhs.z < rhs.z;
  }
} edg[N];
bool vis[N];
clock_t start_time;
int t, n, m;
int tot, root[N];
int _size[N];
vector<int> G[N];
inline void SetClock() { start_time = clock(); }
bool TimeOut(size_t ms) {
  if (1000 * (clock() - start_time) >= ms * ((clock_t)1000)) return true;
  return false;
}
inline pair<int, int> DeSerialize(int c) { return {(c) / m, (c) % m}; }
template <typename T, typename V>
inline void Add(T& v, const V& delta) {
  v += delta;
  if (v >= MOD) v -= MOD;
}
void dfs(int u) {
  vis[u] = true;
  for (int v : G[u]) {
    if (vis[v]) continue;
    dfs(v);
  }
}
int Fetchdeepest(int u, int fa, int& ret) {
  int maxDepth = 1;
  ret = u;
  for (int v : G[u]) {
    if (v == fa) continue;
    int who;
    int retValue = Fetchdeepest(v, u, who);
    if (retValue + 1 > maxDepth) {
      maxDepth = retValue + 1;
      ret = who;
    }
  }
  return maxDepth;
}
void FetchAccessList(int u, int target, int fa, vector<int>& accessList) {
  accessList.emplace_back(u);
  if (u == target) return;
  for (const auto& v : G[u]) {
    if (v == fa) continue;
    FetchAccessList(v, target, u, accessList);
    if (accessList.back() == target) break;
  }
  if (accessList.back() != target) accessList.pop_back();
}
pair<int, int> FetchCenter(int u) {
  int v, v2;
  Fetchdeepest(u, 0, v);
  Fetchdeepest(v, 0, v2);
  vector<int> accessList;
  FetchAccessList(v, v2, 0, accessList);
  return {accessList[accessList.size() >> 1], accessList.size() - 1};
}
void Merge(int x, int y) {
  auto t1 = FetchCenter(x).first;
  auto t2 = FetchCenter(y).first;
  edg[tot++] = {t1, t2, 0};
  G[t1].push_back(t2);
  G[t2].push_back(t1);
}
int main() {
  srand(time(0));
  SetClock();
  cin.sync_with_stdio(false);
  cin >> n >> m;
  for (int(i) = 0; (i) < (m); ++(i)) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  int cnt = 0;
  for (int(i) = 1; (i) <= (n); ++(i)) {
    if (!vis[i]) {
      root[cnt++] = i;
      dfs(i);
      _size[i] = FetchCenter(i).second;
    }
  }
  auto comp = [&](int lhs, int rhs) -> bool { return _size[lhs] > _size[rhs]; };
  sort(root, root + cnt, comp);
  for (int i = 1; i < cnt; i++) {
    Merge(root[0], root[i]);
  }
  cout << FetchCenter(1).second << endl;
  for (int(i) = 0; (i) < (tot); ++(i)) {
    cout << edg[i].x << " " << edg[i].y << endl;
  }
}
