#include <bits/stdc++.h>
using namespace std;
vector<int> g[1005];
vector<set<int> > gg(1005);
vector<int> vv(1005);
vector<int> dis(1005), vis(1005);
int n, m, ans1, ct;
vector<int> temp;
vector<pair<int, int> > ans2;
struct T {
  vector<int> nodes;
  int c, k, d;
  T() {
    k = 0;
    d = 0;
  }
  void f() {
    if (nodes.size() == 1) {
      c = nodes.front();
      d = 0;
      return;
    }
    vector<int> l;
    for (auto i : nodes) {
      if (g[i].size() == 1) {
        l.push_back(i);
      }
    }
    while (l.size() >= 2) {
      vector<int> ee;
      for (auto i : l) {
        int x = *gg[i].begin();
        gg[x].erase(i);
        gg[i].erase(x);
        if (gg[x].size() == 1) {
          ee.push_back(x);
        }
      }
      l = ee;
    }
    c = l.front();
    return;
  }
  void bfs() {
    queue<int> q;
    int s = nodes.front();
    q.push(s);
    dis[s] = 0;
    vis[s] = 1;
    while (not q.empty()) {
      int u = q.front();
      q.pop();
      for (auto v : g[u]) {
        if (not vis[v]) {
          vis[v] = 1;
          dis[v] = dis[u] + 1;
          q.push(v);
        }
      }
    }
    int z = dis[nodes.front()], ss = nodes.front();
    for (auto u : nodes) {
      if (dis[u] > z) {
        z = dis[u];
        ss = u;
      }
      dis[u] = vis[u] = 0;
    }
    q.push(ss);
    vis[ss] = 1;
    dis[ss] = 0;
    while (not q.empty()) {
      int u = q.front();
      q.pop();
      for (auto v : g[u]) {
        if (not vis[v]) {
          vis[v] = 1;
          dis[v] = dis[u] + 1;
          q.push(v);
        }
      }
    }
    for (auto u : nodes) {
      d = max(d, dis[u]);
    }
    return;
  }
} tre[1005];
void bfs(int s) {
  queue<int> q;
  q.push(s);
  vv[s] = 1;
  while (not q.empty()) {
    int u = q.front();
    q.pop();
    temp.push_back(u);
    for (auto v : g[u]) {
      if (not vv[v]) {
        vv[v] = 1;
        q.push(v);
      }
    }
  }
  return;
}
void bbfs() {
  queue<int> q;
  q.push(0);
  for (int i = 0; i < n; i++) {
    dis[i] = vv[i] = 0;
  }
  dis[0] = 0;
  vv[0] = 1;
  while (not q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : g[u]) {
      if (not vv[v]) {
        vv[v] = 1;
        dis[v] = dis[u] + 1;
        q.push(v);
      }
    }
  }
  int z = 0, ss = 0;
  for (int i = 0; i < n; i++) {
    if (dis[i] > z) {
      z = dis[i];
      ss = i;
    }
    dis[i] = vv[i] = 0;
  }
  q.push(ss);
  vv[ss] = 1;
  dis[ss] = 0;
  while (not q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : g[u]) {
      if (not vv[v]) {
        vv[v] = 1;
        dis[v] = dis[u] + 1;
        q.push(v);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    ans1 = max(ans1, dis[i]);
  }
  return;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    gg[u].insert(v);
    gg[v].insert(u);
    g[v].push_back(u);
  }
  for (int i = 0; i < n; i++) {
    if (not vv[i]) {
      bfs(i);
      tre[ct++].nodes = temp;
      temp.clear();
    }
  }
  for (int i = 0; i < ct; i++) {
    tre[i].f();
    tre[i].bfs();
  }
  int mx = 0, cen = 0;
  for (int i = 0; i < ct; i++) {
    if (tre[i].d > mx) {
      cen = i;
      mx = tre[i].d;
    }
  }
  for (int i = 0; i < ct; i++) {
    if (i != cen) {
      ans2.push_back({tre[cen].c, tre[i].c});
      g[tre[cen].c].push_back(tre[i].c);
      g[tre[i].c].push_back(tre[cen].c);
    }
  }
  bbfs();
  cout << ans1 << '\n';
  for (auto i : ans2) {
    cout << i.first + 1 << ' ' << i.second + 1 << '\n';
  }
  return 0;
}
