#include <bits/stdc++.h>
using Tiic = std::tuple<int, int, char>;
const int MAXN = 4000;
const int MAXS = 10;
int n;
char s1[MAXS], s2[MAXS], s3[MAXS], op;
int count = 0;
std::map<std::string, int> nameMap;
std::map<int, std::string> baseMap;
std::map<Tiic, int> operationMap;
std::map<int, Tiic> sourceMap;
bool isExist(const std::string& name) { return nameMap.count(name); }
bool isExist(int a, int b, char op) {
  return operationMap.count(Tiic(a, b, op));
}
int createVariable(const std::string& name) {
  nameMap[name] = count;
  baseMap[count] = name;
  return count++;
}
int createOperation(const std::string& name, int a, int b, char op) {
  nameMap[name] = count;
  baseMap[count] = "";
  sourceMap[count] = Tiic(a, b, op);
  operationMap[Tiic(a, b, op)] = count;
  return count++;
}
int nameCount;
std::string makeName() {
  while (true) {
    char name[] = {char(nameCount / (26 * 26 * 26) + 'a'),
                   char(nameCount / (26 * 26) % 26 + 'a'),
                   char(nameCount / 26 % 26 + 'a'), char(nameCount % 26 + 'a'),
                   0};
    ++nameCount;
    if (isExist(name)) continue;
    return name;
  }
}
int main() {
  std::scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    char e;
    std::scanf(" %[^=]%c%[^$^#&\n]%c", s1, &e, s2, &op);
    if (op == '\n') {
      if (!isExist(s2)) createVariable(s2);
      nameMap[s1] = nameMap[s2];
    } else {
      std::scanf("%s", s3);
      if (!isExist(s2)) createVariable(s2);
      if (!isExist(s3)) createVariable(s3);
      int a = nameMap[s2], b = nameMap[s3];
      if (!isExist(a, b, op))
        createOperation(s1, a, b, op);
      else
        nameMap[s1] = operationMap[Tiic(a, b, op)];
    }
  }
  if (isExist("res")) {
    int res = nameMap["res"];
    if (baseMap[res].length()) {
      if (baseMap[res] == "res")
        std::puts("0");
      else {
        std::puts("1");
        std::printf("res=%s\n", baseMap[res].data());
      }
    } else {
      std::set<int> used;
      std::map<int, std::string> outputMap;
      std::queue<int> q;
      used.insert(res);
      q.push(res);
      do {
        int p = q.front();
        q.pop();
        auto& source = sourceMap[p];
        int a = std::get<0>(source), b = std::get<1>(source);
        if (!used.count(a)) {
          if (baseMap[a].length())
            outputMap[a] = baseMap[a];
          else
            used.insert(a), q.push(a);
        }
        if (!used.count(b)) {
          if (baseMap[b].length())
            outputMap[b] = baseMap[b];
          else
            used.insert(b), q.push(b);
        }
      } while (!q.empty());
      std::vector<std::string> result;
      for (int p : used) {
        if (outputMap.count(p)) continue;
        outputMap[p] = p == res ? "res" : makeName();
        auto& source = sourceMap[p];
        int a = std::get<0>(source), b = std::get<1>(source);
        char op = std::get<2>(source);
        result.push_back(outputMap[p] + '=' + outputMap[a] + op + outputMap[b]);
      }
      std::printf("%d\n", int(result.size()));
      for (auto&& s : result) std::puts(s.data());
    }
  } else {
    std::puts("0");
  }
}
